gpt4 book ai didi

php - 一对多和一对多关系 laravel

转载 作者:行者123 更新时间:2023-11-30 21:49:00 24 4
gpt4 key购买 nike

这是项目迁移

    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 中,我无法从时间表中获取它

这个 Controller 在时间表中按 project_id 返回项目和时间表,但在时间表中按 user_id 返回项目和时间表我不知道如何将其放入系统

最佳答案

使用dot syntax加载嵌套关系:

project::with('timesheets.users')->get();

关于php - 一对多和一对多关系 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com