【问题标题】:I don’t know how to get data in laravel correctly by linking the data in this database我不知道如何通过链接这个数据库中的数据来正确获取laravel中的数据
【发布时间】:2022-01-25 18:19:36
【问题描述】:

在此图中,其余表链接到 datainfo 表。我需要从 datainfo 表中检索整个表数据。 In this picture I have shown the tables themselves

【问题讨论】:

标签: php mysql laravel database relationship


【解决方案1】:

使用 laravel eloquent join 子句,https://laravel.com/docs/8.x/queries#joins 当天总金额 您可以根据日期使用 eloquent sum 和 group by 方法

【讨论】:

    【解决方案2】:

    您可以为每个表创建 Eloquent 模型并定义它们之间的关系。这是正确的 Laravel 方式。

    假设您的 datainfo 表代表您的 Datainfo 模型, 您的cars 表代表汽车型号。与washtypesboxes 相同。

    然后根据您的关系类型在 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();
    

    【讨论】:

    • 非常感谢。我做得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多