Search code examples
laravelmodeleloquentmany-to-manylaravel-5.3

Attach() method for multiple tables in one function (laravel)


I think the title is a little bit confusing so I'll try my best to explain this.

What I have

Car model :

public function users()
{
    return $this->hasMany('User','car_user');
}

User model

public function cars()
{
    return $this->belongsToMany('App\Models\Access\Car','car_user');
}

Reservation model

public function cars()
{
    return $this->belongsToMany('App\Models\Access\Car','car_user');
}

Car migration :

        $table->increments('id');
        $table->string('name')->nullable();
        $table->string('age')->nullable();
        $table->string('model')->nullable();

User migration :

      $table->string('street')->nullable();
        $table->string('niehgborhood')->nullable();
        $table->string('city')->nullable();
         $table->increments('id');
        $table->string('name')->nullable();

car_user :

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');

        $table->integer('car_id')->unsigned();
        $table->foreign('car_id')->references('id')->on('cars');

Reservation :

        $table->increments('id');
        $table->string('from_date')->nullable();
        $table->string('to_date')->nullable();

        $table->string('from_time')->nullable();
        $table->string('to_time')->nullable();

Controller: method to save cars and link them to user id:

public function store(Request $request)
{
    $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();

    $id = $new_cars->id;
    $user->cars()->attach($id);
}

What I'm trying to understand/do

So in my Controller above I could attach user id with car id when the user wants to buy the car.

Now, can I do the same thing if the user wants to rent the car for a few hours. Say I have a form with a dropdown menu that shows some time the user wants the car. Can I save the user and the car he/she wants to rent including the time all in this function ?

Will this attach the time to the car the user interested in and be able to be called by $user->id to show all the cars the user either wants to buy and rent?

Thank you!

Update

thanks to Artur Subotkevič I now can save the id of both Cars and Reservations in car_reservation pivot table based on his answer. I tried doing this to show all cars with time associated with by this function

public function hour($id)
{

    $time = Car::with('reservations')->where('id',$id)->get();
    dd($time);
}

but I get this view

Collection {#364 ▼
   #items: []

} I'm sure I have some cars linked to reservation and my reservation model is now

  public function cars()
{
    return $this->belongsTo('Car','car_reservation');
}

Solution

  • No this will not work correctly.

    But there's a lot of ways to handle that kind of problems.

    You should explain what you really want to do, and we can give you correct answer.


    Edit

    $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);
    

    Make sure that you have set correct relationships in your Models: you need to have Reservation relationship in your car model.