如何从阵列输入中删除不需要的值?

我只是想问一下我们如何避免这种来自阵列输入的输出。每次我更新它时,这些符号 ["\"[ 都会不断增加。我将向您展示问题和下面的代码。谢谢你未来的答案。

http://img3.mukewang.com/62c8e08b0001163c07650069.jpg

http://img.mukewang.com/62c8e0930001584d04620358.jpg

Route::resource('setups','SetupController');


public function index()

    {

       $data = DB::table('setups')->first();

        if (!empty($data)) {        

            $socials = explode(',',$data -> social);

        }else{

            $socials = [];

        }

        return view ('adminpanel.setup.index',['data' => $data,'socials' => $socials]);

    }

index.blade.php


<form action="{{ route('setups.edit',$data->id) }}">

<div class="row">

  <div class="col-md-12" id="socialGroup">

    @foreach($socials as $social)

    <div class="form-group socialField">

    <label class="bmd-label-floating">Social Links</label>

      <input type="text" name="social[]" value="{{$social}}" class="form-control" disabled>

      <a href="#" class="addField"><i class="fa fa-plus"></i></a>

    </div>

    @endforeach

    <div class="alert alert-danger" id="socialError">

    <p><strong>Sorry! </strong>You've reached the max number for social links form.</p>

    </div>

  </div>

</div>

<form>

.


public function edit($id)

    {

        $data = DB::table('setups')->first();

        $setup = DB::table('setups')->where('id', $id)->first();

        if (!empty($data)) {        

            $socials = explode(',',$data -> social);

        }else{

            $socials = [];

        }


        if($setup){

        return view ('adminpanel.setup.edit',['data' => $data,'socials' => $socials]);

        }else{

            return redirect('setups');

        }

    }

.


http://img.mukewang.com/62c8e0a90001f16c05740057.jpg

慕神8447489
浏览 170回答 2
2回答

慕雪6442864

laravel 默认转义数据。每当您从数据库中检索数据以放入刀片视图时,它都没有给出任何错误。数据库数据转义是一种很好的做法。当您显示数据时,有一些不需要的数据。在尝试保存数据之前,您可以trim($yourString)从字符串的开头和结尾删除不需要的空格。并且您不能让空白或空字符串在您的刀片中查看。那么,您可能会使用blank($var)它来检查它是否为空白?<form method="POST" action="{{ route('setups.update', $data->id) }}">&nbsp;<div class="row">&nbsp; <div class="col-md-12" id="socialGroup">&nbsp; &nbsp; @foreach($socials as $social)&nbsp; &nbsp; &nbsp; @if(!blank($social))&nbsp; &nbsp; &nbsp; &nbsp;<div class="form-group socialField">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<label class="bmd-label-floating">Social Links</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="text" name="social[]" value="{{ $social }}" class="form-control">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="#" class="addField"><i class="fa fa-plus"></i></a>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; @endforeach&nbsp; &nbsp; <div class="alert alert-danger" id="socialError">&nbsp; &nbsp; &nbsp;<p><strong>Sorry! </strong>You've reached the max number for social links form.</p>&nbsp; &nbsp; </div>&nbsp; </div></div>

天涯尽头无女友

解决了!我刚刚在我的 SetupController@update 中添加了这段代码if (Input::has('social')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['social'] = implode(',',$data['social']);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP