簡體   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