laravel雄辩、多维关系访问

sqougxex  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(211)

我一共有4个型号,
项目
董事会,
任务
子任务
项目->有许多->板
project->hasmanythrough->task
板->有许多->任务
板->hasmanythrough->子任务
任务->有许多->子任务
这是我的项目模型,

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Project extends Model
{
    use HasFactory, SoftDeletes;

    public function priority()
    {
        return $this->belongsTo(Priority::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class);
    }

    public function boards()
    {
        return $this->hasMany(Board::class);
    }

    public function tasks()
    {
        return $this->hasManyThrough(Task::class, Board::class);
    }

    public function subTasks()
    {

    }
}

董事会模型,

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Board extends Model
{
    use HasFactory, SoftDeletes;

    public function tasks()
    {
        return $this->hasMany(Task::class);
    }

    public function subTasks()
    {
        return $this->hasManyThrough(SubTask::class, Task::class);
    }
}

任务模型,

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Task extends Model
{
    use HasFactory, SoftDeletes;

    public function subTasks()
    {
        return $this->hasMany(SubTask::class);
    }
}

如何访问具有雄辩关系的项目子任务?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题