【发布时间】:2022-01-25 18:19:36
【问题描述】:
在此图中,其余表链接到 datainfo 表。我需要从 datainfo 表中检索整个表数据。 In this picture I have shown the tables themselves
【问题讨论】:
-
这个链接帮了我谢谢,但是我如何确定同一天的总金额?
标签: php mysql laravel database relationship
在此图中,其余表链接到 datainfo 表。我需要从 datainfo 表中检索整个表数据。 In this picture I have shown the tables themselves
【问题讨论】:
标签: php mysql laravel database relationship
使用 laravel eloquent join 子句,https://laravel.com/docs/8.x/queries#joins 当天总金额 您可以根据日期使用 eloquent sum 和 group by 方法
【讨论】:
您可以为每个表创建 Eloquent 模型并定义它们之间的关系。这是正确的 Laravel 方式。
假设您的 datainfo 表代表您的 Datainfo 模型,
您的cars 表代表汽车型号。与washtypes 和boxes 相同。
然后根据您的关系类型在 Datainfo 模型中定义关系。
class Datainfo extends Model
{
public function cars()
{
return $this->hasMany(Car::class);
}
}
您也可以使用hasOne 代替hasMany 进行一对一关系
同样,创建关系定义函数为washtypes() 和boxes()。
然后使用控制器中的想法来获取包含所有相关数据的 Datainfo
return Datainfo::with('cars','washtypes','boxes')->get();
或者,您可以获取计数
return Datainfo::with('cars','washtypes','boxes')->count();
统计日期
return Datainfo::with('cars','washtypes','boxes')->where('created_at',$date_var)->count();
如果您只想要与汽车、洗车类型或盒子有关的数据信息:
return Datainfo::has('cars','washtypes','boxes')->where('created_at',$date_var)->count();
【讨论】: