繁体   English   中英

停留在Laravel 5.4上的EmiratesToMany关系上的自定义枢轴模型

[英]Stuck with Custom Pivot Model on belongsToMany relationship on Laravel 5.4

我试图弄清楚如何使用自定义中间模型(数据透视表)实现多对多关系。 这是我的模型:

banners

 - id
 - title
 - description


banner_regions (Pivot)

 - id
 - region_id
 - banner_id
 - active


regions

 - id
 - name
 - slug

雄辩的模特代码:

class Banner extends Model
{
    /**
    * Get all of the regions for the banner.
    */
    public function regions()
    {
        return $this->belongsToMany('App\Region', 'banner_regions')
            ->withPivot('active')
            ->using(BannerRegion::class);
    }
}

class BannerRegion extends Model
{

}


class Region extends Model
{
    /**
    * Get all of the banners for the region.
    */
    public function banners()
    {
        return $this->belongsToMany('App\Banner', 'banner_regions')
            ->withPivot('active')
            ->using(BannerRegion::class);
    }
}

横幅广告代码:

class BannerController extends Controller
{
    protected $model;

    public function __construct(Banner $model)
    {
        $this->model = $model;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $region = $request->region; // region model instance
        // ??
    }
}

因此,我的问题是如何检索特定区域的横幅广告?

我已经更改了代码,现在可以正常使用了。

我将BannerRegion枢轴模型更改为Pivot而不是Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class BannerRegion extends Pivot
{
    // It is just for casting int values into boolean values.
    // Useful for JSON responses.
    protected $casts = [
        'active' => 'boolean',
        'for_customers' => 'boolean',
    ];
}

横幅模型。 这里没有什么可添加的,但是我做了一些更改以改善JSON响应,例如$appends

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $hidden = ['id', 'regions'];

    // Add data from the pivot model
    protected $appends = ['for_customers'];

    public function getForCustomersAttribute()
    {
        // Get the attribute 'for_customers' from the pivot model 
        return $this->regions
            ->keyBy('pivot.banner_id')
            ->get($this->id)
            ->pivot
            ->for_customers;
    }

    /**
    * Get all of the regions for the banner.
    *
    */
    public function regions()
    {
        return $this->belongsToMany('App\Region', 'banner_regions')
            ->withPivot('for_customers', 'active')
            ->using('App\BannerRegion');
    }

    /**
    * Scope a query to only include active banners for a specific region.
    *
    * @param \Illuminate\Database\Eloquent\Builder $query
    * @param App\Region $region
    * @return \Illuminate\Database\Eloquent\Builder
    */
    public function scopeFindbyRegionAndActive($query, Region $region)
    {
        return $query->whereHas('regions', function($query) use ($region) {
            return $query->whereRegionId($region->id)->whereActive(true);
        });
    }
}

在我的横幅控制器中,我刚刚添加了:

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Region $region)
    {
        return $this->model->findbyRegionAndActive($region)->get();
    }

区域参数由依赖注入(laravel.com/docs/5.4/routing#route-model-binding)解决。

最后,我的路线:

Route::group(['prefix' => '/regions/{region}'], function()
{
   // Banners
   Route::resource('banners', 'BannerController', ['only' => 'index']);
});

端点:

/regions/my-region/banners

JSON响应:

[
  {
    "title": "a title...",
    "description": "a descritpion...",
    "link": "http://localhost",
    "for_customers": true
  }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM