dty97501 2017-08-24 13:29
浏览 95
已采纳

获取我从多个选择框中选择的所有ID并更新另一个表 - laravel

I have a form where a user can select multiple tags from a select box. I then need to create the shipment, and after I do that, I need to get all the ID's the user selected from the tags and update another table where the ID matches the tag IDs the user selected.

Here is the select box:

<select name="bins[]" class="form-control" id="shipping_bin_labels_select" multiple="multiple">
     @foreach(\App\Bin::all() as $bin)
          <option value="{{ $bin->id }}"  {{ old('bins') }}>{{ $bin->label }}</option>
     @endforeach
</select>

Here is how it looks:

enter image description here

Then I create the shipment:

  public function store(Request $request)
    {
        $this->validate($request, [
            'truck'      => 'required',
            'bins'       => 'required',
            'stand_id'   => 'required',
        ]);

        $standName = request('stand_id');
        $standName = Stand::where('name', '=', $standName)->first();

        Shipment::create([
            'truck' => request('truck'),
            'stand_id' => $standName->id
        ])->save();

        ...........

After that, I need to loop through all the bin tags, get their IDs and update the 'bins' table with the shipment ID I just made. I don't know how to get all the bins from the bins table with the bin IDs I just selected.

This is what I tried so far:

$bins = $request->input('bins');
$bins = implode(',', $bins);

// Tried this, but get empty collection. [$bins] (brackets) dont help either.
$bins = Bin::whereIn('id', $bins)->get();

// Tried this, but get "Call to a member function all() on string"
$bins = Bin::whereIn('id', $bins->all())->get();


// This is what I sort of need
$bins = Bin::whereIn('id', [3, 5, 6])->get();

For reference, when I DD this:

$bins = $request->input('bins');
$bins = implode(',', $bins);
dd($bins);

I get this:

enter image description here

Those are my bin IDs from the select box. Don't worry about the long ID string, thats how my ID's are formatted and stored in the bins table.

  • 写回答

2条回答 默认 最新

  • doujiao1180 2017-08-24 13:39
    关注

    WhereIn accepts an array

    ->whereIn('id', [1, 2, 3])
    

    If you remove the implode you can pass that array into the query.

    so it would become

    ->whereIn('id', $bins)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?