class 集合的对象无法转换为 int
Object of class Collection could not be converted to int
我没有从控制器
获得 select 选项
这是我的编辑页面
{!! Form::select('channel_id',[''=>'Select A Channel'] + $channels,null,['class'=>'form-control']) !!}
下面是我的控制器
public function edit($id)
{
$channels = Channel::all() ;
$d = Discussion::findOrFail($id);
return view('discussion.edit',compact('channels','d'));
}
Form::select
只是为了获取一组键值对来设置选项。
你可以做的是使用pluck()方法:
[''=>'Select A Channel'] + $channels->pluck('name', 'id')->toArray()
或
$channels->pluck('name', 'id')->prepend('Select A Channel', '')->toArray() // I'm not sure if you will need `->toArray()` here
如果您不打算将 $channels
用于页面上的任何其他内容,那么您可以在控制器中使用 pluck()
:
$channels = Channel::pluck('name', 'id');
然后在你的blade
文件中你可以做:
[''=>'Select A Channel'] + $channels->toArray()
或
$channels->prepend('Select A Channel', '')->toArray()
您需要更改我在 pluck()
中使用的 name
和 id
占位符,以匹配您的 channels
table 中的列名称.
最后,根据您希望 <select>
的占位符的行为,您实际上可以通过以下方式设置它:
{!! Form::select('channel_id', $channels->pluck('name', 'id'), null, ['class'=>'form-control', 'placeholder' => 'Select A Channel']) !!}
注意最后一个数组中的 'placeholder' => 'Select A Channel']
:
我没有从控制器
获得 select 选项
这是我的编辑页面
{!! Form::select('channel_id',[''=>'Select A Channel'] + $channels,null,['class'=>'form-control']) !!}
下面是我的控制器
public function edit($id)
{
$channels = Channel::all() ;
$d = Discussion::findOrFail($id);
return view('discussion.edit',compact('channels','d'));
}
Form::select
只是为了获取一组键值对来设置选项。
你可以做的是使用pluck()方法:
[''=>'Select A Channel'] + $channels->pluck('name', 'id')->toArray()
或
$channels->pluck('name', 'id')->prepend('Select A Channel', '')->toArray() // I'm not sure if you will need `->toArray()` here
如果您不打算将 $channels
用于页面上的任何其他内容,那么您可以在控制器中使用 pluck()
:
$channels = Channel::pluck('name', 'id');
然后在你的blade
文件中你可以做:
[''=>'Select A Channel'] + $channels->toArray()
或
$channels->prepend('Select A Channel', '')->toArray()
您需要更改我在 pluck()
中使用的 name
和 id
占位符,以匹配您的 channels
table 中的列名称.
最后,根据您希望 <select>
的占位符的行为,您实际上可以通过以下方式设置它:
{!! Form::select('channel_id', $channels->pluck('name', 'id'), null, ['class'=>'form-control', 'placeholder' => 'Select A Channel']) !!}
注意最后一个数组中的 'placeholder' => 'Select A Channel']
: