J. Doe
J. Doe

Reputation: 1

Laravel Relations and Foreign Key Constraints

I have two models and migrations tables.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    public function guest()
    {
        return $this->hasOne(Guest::class);
    }
}

-

namespace App;

use Illuminate\Database\Eloquent\Model;

class Guest extends Model
{
    public function room()
    {
        return $this->hasMany(Room::class);
    }
}

room -> id, squaremeters, guest_id

guest -> id, Name, last_name, email, phone, room_id(required)

Room hasOne Guest -> implemented in the model

Guest hasMany Rooms -> implemented in the model

If I create a guest, I have to add a room Number. The room in room_table with the corresponding id should automatically be updated with the guest_id.

Example new Guest: id(2), Name, last_name, phone, email, room_id(3) -> The room with id 3 should show to the Guest with id 2.

How do i implement this?

Upvotes: 0

Views: 1755

Answers (3)

Shailendra Gupta
Shailendra Gupta

Reputation: 1118

Model Guest

namespace App;

use Illuminate\Database\Eloquent\Model;

class Guest extends Model
{
    public function room()
    {
        return $this->hasMany(Room::class);
    }
}

and room model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    public function guest()
    {
        return $this->hasOne(Guest::class);
    }
}

in your Controller loadd all model in constructer

 $gust_id=$request->get('guest_id');
 $room_id=$request->get('count_of_rooms');
 $squrmtr=$request->get('squaremeters');

let you after saving guest data you have multiple rooms and you have to save it

for($i = 0; $i < count($request->get('count_of_rooms')); $i++)
{
    $rooms[] = new Room([
        'guset_id'  => $guest->id,
        'squaremeters' => $squrmtr[$i],
    ]);
}
$guest->room()->saveMany($rooms);

Upvotes: 0

Mysteryos
Mysteryos

Reputation: 5791

Room class

class Room extends \Illuminate\Database\Eloquent\Model {
    /**
     * @var string
     */
    protected $table = 'room';

    /**
     * Relationship - Belongs To - Guest
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function guest()
    {
        return $this->belongsTo(Guest::class,'guest_id');
    }
}

Guest class

class Guest extends \Illuminate\Database\Eloquent\Model {
    /**
     * @var string
     */
    protected $table='guest';

    /**
     * Relationship - Has Many - Rooms
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function rooms()
    {
        return $this->hasMany(Room::class,'guest_id');
    }
}

Code for saving the relationships defined above:

$room = Room::create();
$room->save();

$guest = Guest::create();
$guest->save();
//Add guest to room - 1st method
$guest->rooms()->save($room);

//Add guest to room - 2nd method
$room->guest()->associate($guest);

Upvotes: 0

Amir
Amir

Reputation: 721

In your controller add this function:

public function CheckInUser($guest_id,$room_id)

 $guest = Guest::where('id',$guest_id)->firstOrFail();
 $room = Room::where('id',$room_id)->firstOrFail();

 $guest['room_id'] = $room_id;
 $guest->save();

 $room['guest_id'] = $guest_id;
 $room->save();

}

Now you can change a guest's room whenever you want by calling the function above as shown below:

CheckInUser(2,3);

Upvotes: 1

Related Questions