【问题标题】:Laravel 4.2 eloquent relationsLaravel 4.2 雄辩的关系
【发布时间】:2018-01-29 07:58:09
【问题描述】:

我是 laravel 的新手,并且一直在旧服务器上使用 laravel 4.2。我必须加入三个表才能获得所需的数据。结构如下:

排队表:

|编号 |患者 ID | shift_id |日期|

病人表:

|编号 |姓名 |年龄 |性别 |

班次表:

|编号 |时间 |

我需要我的控制器返回带有患者姓名、轮班时间和日期的对象数据。我有每张桌子的三个模型。我怎样才能得到这个?我一直在尝试一对多关系,但无法完成这项任务。

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    雄辩的方式

    class Queue  extends Model
    {
       protected $appends = ['patient_name','shift_date'];
    
       public function patient()
       {
          return $this->blongsTo(Patient::class,'patient_id'.'id');
       } 
    
       public function shift()
       {
          return $this->blongsTo(Shift::class,'shift_id','id');
       } 
    
       public function getPatiantNameAttribute()
       {
          return $this->patient->name;
       }
    
        public function getShiftDateAttribute()
       {
          return $this->shift->time;
       }
    }
    

    然后

    $queue = Queue::find(1);  
    
    $queue->patient_name;
    $queue->shift_date;
    

    这种方法使用eager loading

    $queue = Queue::with('patient','shift')->find(1);  
    
    $queue->patient->name;
    $queue->shift->time;
    

    阅读docs

    【讨论】:

    • 两种情况下结果都返回null。如何指定要在队列表中查找的 patient_id 列和 shift_id 列?
    • @szskdgi 第二个和第三个参数可以指定外键和本地键
    • @szskdgi 在 find 函数 1 中是硬编码的,你甚至有一个 id 为 1 的队列吗?请改变它男人
    • 是的,我确实有。使用参数,结果返回包含表中所有列的对象。我只想将患者姓名作为单个变量而不是对象
    • 我想要像 Juene Guerrier 的答案所产生的结果
    【解决方案2】:

    你可以的

    查询生成器

           $data = DB::table('queue')
            ->join('patient', 'queue.parent_id', '=', 'patient.id')
            ->join('shift', 'queue.shift_id', '=', 'shift.id')
            ->select('patient.name', 'shift.time', 'queue.date')
            ->get();
    
           dd($data);
    

    文档解释更多here

    【讨论】:

    • 工作就像一个魅力,但我正在寻找一种更有说服力的方法来实现这一点。这对学习也有好处。
    【解决方案3】:

    如果你使用 eloquent,你需要有模型。 对于每个数据库表,您可以生成如下模型:

    php artisan make:model Queue
    php artisan make:model Patient
    php artisan make:model Shift
    

    之后该模型需要一些关系。像这样定义关系。

    class Queue extends Model
    {
        /**
         * So now the Queue has a relationship with patient.
         */
        public function patient()
        {
            return $this->hasMany('App\Patient','patient_id','id');
        }
    }
    

    此代码块允许您对数据库进行一些更改。

    所以你可以像这样让队列中有多少病人。

    $queue= Queue::find(1);
    foreach($queue as $item)
    {
    echo $item->patient;
    }
    

    如果您想了解有关关系和模型的更多信息,请参阅此处:

    Laravel Eloquent Relationships

    【讨论】:

    • 这个返回更像是一个对象内的对象!队列中不能只添加一列吗?
    • @szskdgi 我不认为 eloquent 是这样工作的。模型之间的关系必须如图所示分配
    【解决方案4】:
    $result = Queue::join('patient', 'queue.parent_id', '=', 'patient.id')
               ->join('shift', 'queue.shift_id', '=', 'shift.id')
               ->select('patient.name', 'shift.time', 'queue.date')
               ->get();
    echo "<pre>"; print_r($result); die();
    

    这里队列是你的模型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-14
      • 2018-11-28
      • 2015-01-11
      • 2021-06-03
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多