当前位置: 首页 > 知识库问答 >
问题:

一对多和一对多关系laravel

邴奇逸
2023-03-14

这是项目迁移

    Schema::create('projects', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('start_date');
        $table->string('end_date');
        $table->string('con');
        $table->timestamps();
    });

这是时间表

    Schema::create('timesheets', function (Blueprint $table) {
        $table->increments('id');
        $table->string('user_id');
        $table->string('project_id');
        $table->string('day');
        $table->string('month');
        $table->string('year');
        $table->string('jalali');
        $table->string('timesheet_h');
        $table->string('timesheet_m');
        $table->timestamps();
    });

这样用户就可以迁移了

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('mobile');
        $table->string('salary_base');
        $table->string('salary_base_h');
        $table->string('start_contract');
        $table->string('end_contract');
        $table->string('start_insurance');
        $table->string('end_insurance')->nullable();
        $table->string('first_salary');
        $table->string('date_birth');
        $table->string('melli_code');
        $table->string('s_number');
        $table->string('nda');
        $table->string('work_rules');
        $table->string('end_work')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

这是我的项目模型

public function timesheets()
{
    return $this->hasMany(timesheet::class,'project_id');
}

这是我的时间表模型:

public function projects()
{
    return $this->belongsTo(project::class,'project_id');
}

public function users()
{
    return $this->belongsTo(User::class,'user_id','id');
}

这是我的用户模型

public function project_peoples()
{
    return $this->hasMany('App\project_people');
}
public function timesheets()
{
    return $this->belongsTo(timesheet::class);
}

public function projects()
{
    return $this->belongsTo(project::class);
}

现在,我从项目返回我的查询

public function allProject()
{
    $projects=project::with(['timesheets','users'])->get();

    return $projects;
  }

这是可以的,但user_id用户在timesheets.user_id我不能得到它的时间表,并得到它

此控制器按时间表中的项目id返回项目和时间表,但时间表中的用户id我不知道如何将其输入系统

共有2个答案

有玄天
2023-03-14
[{"id":1,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0627\u06cc\u0645 \u0634\u06cc\u062a","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 10:54:11","updated_at":"2018-01-02 10:54:11","timesheets":[{"id":7,"user_id":"2","project_id":"1","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"5","timesheet_m":"24","created_at":"2018-01-02 12:40:12","updated_at":"2018-01-02 13:47:09","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":8,"user_id":"1","project_id":"1","day":"13","month":"10","year":"1396","jalali":"1396\/10\/13","timesheet_h":"10","timesheet_m":"10","created_at":"2018-01-03 05:59:13","updated_at":"2018-01-03 05:59:13","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":9,"user_id":"2","project_id":"1","day":"14","month":"10","year":"1396","jalali":"1396\/10\/14","timesheet_h":"10","timesheet_m":"15","created_at":"2018-01-04 07:17:44","updated_at":"2018-01-04 07:17:44","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":10,"user_id":"2","project_id":"1","day":"16","month":"10","year":"1396","jalali":"1396\/10\/16","timesheet_h":"10","timesheet_m":"60","created_at":"2018-01-06 07:17:21","updated_at":"2018-01-06 07:17:21","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}}]},{"id":2,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0633\u062a\u06cc","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 11:28:03","updated_at":"2018-01-02 11:28:03","timesheets":[{"id":3,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/11","timesheet_h":"8","timesheet_m":"12","created_at":"2018-01-02 11:39:46","updated_at":"2018-01-02 11:39:46","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":4,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/10","timesheet_h":"4","timesheet_m":"11","created_at":"2018-01-02 11:40:41","updated_at":"2018-01-02 13:49:45","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":6,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"1","timesheet_m":"12","created_at":"2018-01-02 12:06:31","updated_at":"2018-01-02 13:49:37","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}}]}]

这是我的回报,我有2个用户,但重复我的2个用户:(

鲁望
2023-03-14

使用点语法加载嵌套关系:

project::with('timesheets.users')->get();
 类似资料:
  • 我正在学习冬眠,只是有点困惑。

  • 本文向大家介绍mybatis关系映射之一对多和多对一,包括了mybatis关系映射之一对多和多对一的使用技巧和注意事项,需要的朋友参考一下 本实例使用用户和订单的例子做说明: 一个用户可以有多个订单, 一个订单只对应一个用户。(其中应用到注释) 1.代码的结构 2. 建表语句: 3. 用户实体: 4. 订单实体: 5.写PersonMapper.java的接口 6. 一对多实体配置: Person

  • 问题内容: 在我的elasticsearch服务器中,我只有一个索引。 (博客)索引包含多种类型。 如:,。 在标签类型中,我创建了1000多个标签,并在帖子类型中创建了10个帖子。 例如:帖子 例如:标签 我想将现有标签分配给博客帖子(即,关系=>映射)。 如何将标签分配给帖子映射? 问题答案: 您可以在Elasticsearch中使用4种方法来管理关系。在Elasticsearch博客文章-E

  • 问题内容: 一对多和多对一关系的真正区别是什么?它只是颠倒的,是什么样的? 除了本主题之外,我找不到关于此主题的任何“易懂”教程:初学者使用的SQL:第3部分- 数据库关系 问题答案: 是的,反之亦然。它取决于实体存在于关系的哪一侧。 例如,如果一个部门可以雇用多名员工,则部门与员工之间是一对多的关系(1个部门雇用许多员工),而员工与部门之间的关系则是多对一的关系(许多员工在一个部门中工作)。 有

  • 我对Dynamodb和非关系型数据库世界非常陌生。我正在练习AWS的和。我使用的是无服务器框架。我拆分我的处理程序多个lambda函数。我想创建我的数据,在那里我可以看到所有的餐馆和他们有什么类型的啤酒的价格。我还想查询所有没有价格的啤酒。我成功创建了餐厅,它的分区键是。我可以得到所有餐馆的数据。但是我坚持啤酒逻辑。我为啤酒创建了像这样的apiendpoint餐厅/{id}/createBeers