【问题标题】:Laravel Relations and Foreign Key ConstraintsLaravel 关系和外键约束
【发布时间】:2019-03-27 07:35:37
【问题描述】:

我有两个模型和迁移表。

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

房间 -> id,平方米,guest_id

guest -> id、Name、last_name、email、电话、room_id(必填)

Room hasOne Guest -> 在模型中实现

Guest hasMany Rooms -> 在模型中实现

如果我创建一个客人,我必须添加一个房间号。 room_table 中对应 id 的房间应该会自动更新成 guest_id。

示例 new Guest: id(2), Name, last_name, phone, email, room_id(3) -> id 为 3 的房间应该显示给 id 为 2 的 Guest。

我该如何实现?

【问题讨论】:

  • 你能分享你所有相关的模型吗
  • 你到底想完成什么?
  • 如果我的理解是正确的,一个房间一次只能住一位客人,一位客人可以同时预订多间客房?
  • @Mysteryos 是正确的
  • @ShaielndraGupta 我有两个模型:房间 -> id、平方米、guest_id guest -> id、Name、last_name、email、phone、room_id(required) Room hasOne Guest -> 在模型中实现 Guest hasMany Rooms -> 在模型中实现

标签: laravel foreign-keys migration relationship


【解决方案1】:

在您的控制器中添加此功能:

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

}

现在您可以随时调用上面的函数来更改客人的房间,如下所示:

CheckInUser(2,3);

【讨论】:

  • 我必须在迁移表中添加任何外键约束吗?如果我这样做会有什么好处?
  • 您无需在客人和房间之间建立任何关系即可使用上述功能。因此不需要外键。
  • 虽然代码可以工作,但最好使用内置的 eloquent 方法来保存您的关系。这符合您的实现类必须不知道模型的列名的关注点的分离
【解决方案2】:

房间类

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

来宾班

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

用于保存上面定义的关系的代码:

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

【讨论】:

    【解决方案3】:

    模特嘉宾

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Guest extends Model
    {
        public function room()
        {
            return $this->hasMany(Room::class);
        }
    }
    

    房间模型

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Room extends Model
    {
        public function guest()
        {
            return $this->hasOne(Guest::class);
        }
    }
    

    在你的控制器中加载所有模型在构造函数中

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

    让你保存客人数据后你有多个房间,你必须保存它

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

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 2019-06-20
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2016-08-17
      • 2017-11-24
      相关资源
      最近更新 更多