Laravel pluck 数组转字符串

Laravel pluck Array to string conversion

我正在创建投诉 为此,在为他们的投诉插入部门时,我正在使用 pluck() 方法从部门 table 检索数据并在 select 下拉列表中显示投诉作为数组,但问题是它没有像它所说的那样工作

Array to string conversion (View: C:\xampp\htdocs\test\resources\views\complaint\create.blade.php)

投诉控制器

 $department = Department::pluck('name','id')->all();

    return view('complaint.create',compact('department'));

create.blade.php

<strong>Department : </strong>
{!! Form::select('dep_id',$department,null,['class'=>'form-control']) !!}

请帮忙!

尝试将其更改为 $department = Department::all()->pluck('name','id')->toArray();

如果它不起作用试试这个:-

$department = Department::select('id','name')->get();
return view('complaint.create')->with(compact('department'));

现在你的观点是这样的:-

<strong>Department : </strong>
<select class="form-control" name="any-name">
@foreach($department as $dept)
 <option value="{{$dept->id}}">{{$dept->name}}</option>
@endforeach

希望对您有所帮助!

{!! Form::select('dep_id',$department,old('dep_id'),['class'=>'form-control', 'placeholder'=>'Select Any name']) !!}