0
votes

I want to make a N-N relationship between clients and rooms. I have this method to make that happen:

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'));
}

But I am getting Method Illuminate\Support\Collection::clients does not exist. I tried to get all data from DataBase and query but i did'nt work. When I use $clients = Client::all(); and $room->clients->attach($client->id); I got this message: Property [[{"id":1,"name":"Mounir El imlahi","phone":"516421515","email":"[email protected]","created_at":"2020-05-13T08:09:49.000000Z","updated_at":"2020-05-13T08:09:49.000000Z"}]] does not exist on this collection instance.

enter image description here

1
I tried with $clients = Client::all(); and it did'nt work. I don't want to use a query I just want to get the relationship made.Mounir El imlahi

1 Answers

1
votes

This would be wrong because get () returns a collection instance

Try this

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'));
}