获取 parent parents 数据到 Laravel 中的数据透视表 table

Getting parent parents data in a pivot table in Laravel

您会使用哪种 Eloquent 模型:

获取其Company品牌颜色为red/slug的所有Pages

其中品牌颜色是枢轴 table 并作为 ID 存储在 Company table 上。

所以你有 brand_colors id, name, slug, hex 有 company 它有 brand_color_id Pages 只有公司 ID。

我想抓取公司品牌颜色为 X 的所有页面。

你会怎么做?

这个 table 有 company_id

class WebsitePage extends Model
{
    use HasFactory;

    public function scopeFilter($query, array $filters) {

        // Get all pages with a color that the company has
        $query->when($filters['color'] ?? false, fn($query, $color) =>
        $query->whereHas('color', fn ($query) =>
                $query->where('name', $color)
            )
        );

    }

    // Where URL slug 'red' color, associated with company brand_color_id matches brandColor slug
    // or just simpley call $company->color() ?
    public function color()
    {
        // return $this->with(Company::class, 'brand_color_id');
        // return $this->belongsTo(Company::class, 'company_id');
        // dd($this->with())

        // return Company::with('color.red');
    }

这个table有brand_color_id

class Company extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'name',
        'slug',
        'description'
    ];


    public function pages() {
        return $this->hasMany(WebsitePage::class);
    }

    public function color() {
        return $this->belongsTo(BrandColor::class, 'brand_color_id');
    }
}

这个 table 有:id |姓名 |鼻涕虫 |十六进制

class BrandColor extends Model
{
    use HasFactory;
}

那么知道这一点后,您将如何获得公司颜色为过滤页面的所有页面?

在页面控制器方面,这就是我正在做的:

class PageController extends Controller
{
    public function index(WebsitePage $websitePage)
    {
        // return view('web.inspiration.pages.index', [
        //     'pages' => WebsitePage::latest()->paginate(30)
        // ]);
        return view('web.inspiration.pages.index', [
            'pages' => WebsitePage::latest()->filter(
                request(['color'])
            )->paginate(30)->withQueryString()
        ]);
    }
}

您的模型布局描述得不太好,但我认为您只是想定义 a HasOneThrough relationship 以将 WebsitePage 加入 BrandColor:

class WebsitePage extends Model
{
    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function color(): HasOneThrough
    {
        return $this->hasOneThrough(BrandColor::class, Company::class);
    }
}

class Company extends Model
{
    public function color(): BelongsTo
    {
        return $this->belongsTo(BrandColor::class);
    }
}

class BrandColor extends Model
{
    public function company(): HasOne
    {
        return $this->hasOne(Company::class);
    }
}

然后在您的控制器中,只需在您的 whereHas()with() 调用上使用一个条件:

class PageController extends Controller
{
    public function index(): View
    {
        $color = request("color");
        $pages = WebsitePage
            ::whereHas("color", fn ($q) => $q->where("color", $color))
            ->with(["color" => fn ($q) => $q->where("color", $color)])
            ->paginate(30)
            ->withQueryString();
        return view("web.inspiration.pages.index", compact($pages));