Php Laravel 4.2雄辩的关系
我是laravel的新手,一直在一台旧服务器上使用Laravel4.2。我必须连接三个表才能获得所需的数据。结构如下: 队列表: |id |患者id |轮班id |日期| 病人表: |id |姓名|年龄|性别| 轮班表: |id |时间| 我需要控制器返回带有患者姓名、轮班时间和日期的对象数据。每张桌子我有三种型号。我怎样才能得到这个?我一直在尝试处理关系,但未能完成此任务。你可以这样做 查询生成器 文档对它进行了更多的解释 查询生成器Php Laravel 4.2雄辩的关系,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我是laravel的新手,一直在一台旧服务器上使用Laravel4.2。我必须连接三个表才能获得所需的数据。结构如下: 队列表: |id |患者id |轮班id |日期| 病人表: |id |姓名|年龄|性别| 轮班表: |id |时间| 我需要控制器返回带有患者姓名、轮班时间和日期的对象数据。每张桌子我有三种型号。我怎样才能得到这个?我一直在尝试处理关系,但未能完成此任务。你可以这样做 查询生成器 文档对它进行了更多的解释 查询生成器 文档对此进行了更详细的解释如果您使用的是雄辩型,则需要有模
文档对此进行了更详细的解释如果您使用的是雄辩型,则需要有模型。 对于每个数据库表,您可以生成如下模型:
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;
}
此代码块允许您在数据库中进行一些更改
所以你可以得到像这样排队的病人数量
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;
}
如果您想获得有关关系和模型的更多信息,请参阅此处:
如果你使用的是雄辩的,你需要有模型。 对于每个数据库表,您可以生成如下模型:
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;
}
此代码块允许您在数据库中进行一些更改
所以你可以得到像这样排队的病人数量
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;
}
如果您想获得有关关系和模型的更多信息,请参阅此处:
雄辩的方式
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;
}
}
然后
这种方法使用
以雄辩的方式阅读
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;
}
}
然后
这种方法使用
阅读$result=Queue::join('patient'、'Queue.parent_id'、'='、'patient.id'))
->join('shift','queue.shift_id','=','shift.id')
->选择('patient.name'、'shift.time'、'queue.date')
->get();
回声“;打印(结果);模具();
这里的队列是您的模型$result=Queue::join('patient','Queue.parent_id','=','patient.id'))
->join('shift','queue.shift_id','=','shift.id')
->选择('patient.name'、'shift.time'、'queue.date')
->get();
回声“;打印(结果);模具();
这是您的模型很有魅力,但我正在寻找一种更具说服力的方法来实现这一点。这对学习也有好处。它很有魅力,但我一直在寻找一种更有说服力的方法来达到这个目的。这对学习也有好处。在这两种情况下,结果都返回null。如何指定要在队列表中查找的patient_id列和shift_id列?@szskdgi您可以在第二个和第三个列中指定外键和本地键parameter@szskdgi在find函数
1
中,是否有硬编码的内容,您是否有id为1的队列?请把它换了,是的,我有。使用这些参数,结果返回包含表中所有列的对象。我只希望患者姓名作为一个变量,而不是目标。我希望结果像Juene Guerrier的答案生成的结果一样。在这两种情况下,结果都返回null。如何指定要在队列表中查找的patient_id列和shift_id列?@szskdgi您可以在第二个和第三个列中指定外键和本地键parameter@szskdgi在find函数1
中,是否有硬编码的内容,您是否有id为1的队列?请把它换了,是的,我有。使用这些参数,结果返回包含表中所有列的对象。我只希望患者姓名作为一个变量,而不是对象我希望结果像Juene Guerrier的答案生成的那样这个返回更像对象中的一个对象!不能只将一列添加到队列中吗?@szskdgi我认为eloquent不是这样工作的。模型之间的关系必须指定为show。此返回更像对象中的对象!不能只将一列添加到队列中吗?@szskdgi我不认为eloquent是这样工作的。模型之间的关系必须如图所示进行分配