Laravel 方法Illumb\Support\Collection::客户端不存在
我想在客户和房间之间建立N-N关系。我有这个方法来实现这一点:Laravel 方法Illumb\Support\Collection::客户端不存在,laravel,Laravel,我想在客户和房间之间建立N-N关系。我有这个方法来实现这一点: public function booking(Request $request, Client $client) { $room = DB::table('rooms')->where('id', '=', $request->roomname)->get(); //$client = DB::table('clients')->where('id', '=', $client)->
public function booking(Request $request, Client $client)
{
$room = DB::table('rooms')->where('id', '=', $request->roomname)->get();
//$client = DB::table('clients')->where('id', '=', $client)->get();
dd($client);
//$client = Client::find(10);
$clients = Client::query();
//$clients = Client::all();
$room->clients()->attach($client->id);
return redirect(route('reservation.index'));
}
但我得到的方法是illumb\Support\Collection::clients不存在。我试图从数据库和查询中获取所有数据,但没有成功。当我使用$clients=Client::all()时;和$room->clients->attach($client->id);我得到了这个信息:
财产[[{“id”:1,“姓名”:“Mounir El-imlahi”,“电话”:“516421515”,“电子邮件”:info@elimlahi.com“,”已创建位置“:”2020-05-13T08:09:49.000000 Z“,”已更新位置“:”2020-05-13T08:09:49.000000 Z“}]”在此集合实例上不存在
这是错误的,因为get()返回一个集合实例 试试这个
public function booking(Request $request, Client $client)
{
$rooms = DB::table('rooms')->where('id', '=', $request->roomname)->get();
foreach ($rooms as $room) {
$room->clients()->attach($client->id);
}
return redirect(route('reservation.index'));
}
我尝试了$clients=Client::all();而且它不起作用。我不想使用查询,我只想建立关系。谢谢Kent。我试试看