3

i dont have a idea what is use of this method i saw something on someone code like this.

$user = JWTAuth::parseToken()->authenticate();

$new_car = new Car();
$new_car->name = $request->name;
$new_car->age = $request->age;
$new_car->model = $request->model;
$new_car->save();

$time = new Reservation();
$time->from_date = $request->from_date;
$time->to_date = $request->to_date;
$time->from_time= $request->from_time;
$time->to_time = $request->to_time;
$time->save();

// Attach the reservation to the car's reservations
$new_car->Reservation()->attach($time->id);

// Attach the car to the user cars
$user->cars()->attach($new_car->id);

hopefully someone can explain me well.

Ray Paras
  • 185
  • 11

1 Answers1

5

Attaching / Detaching

Attach is used mainly Eloquent Relationship in Many To Many Relationships. It mainly uses intermediate table data insert or update. For example, let's imagine a user can have many roles and a role can have many users. You may use the attach method to attach a role to a user by inserting a record in the relationship's intermediate table:

For more details

A.A Noman
  • 5,244
  • 9
  • 24
  • 46