Wcan
Wcan

Reputation: 878

Laravel Accessor on demand on relationship

Problem: I have accessor in Size.php model which is called in relationship with Item.php Model, in API i need the accessor to work, but in other controllers i want to disable the accessor. I have removed unnecessary/unrelated code from all files.

What i want to do:

In ItemControllerAPI i need accessor to work, but i want to disable accessor in other controllers.

I have already seen: I have already seen these links but didn't work for me.

1: https://laracasts.com/discuss/channels/eloquent/accessor-on-demand-but-not-on-every-results

2: Create dynamic Laravel accessor

3: https://laracasts.com/discuss/channels/general-discussion/eager-load-accessors

Size.php (Model)

class Size extends Model
{
    protected $appends = ['addons'];

    public function items()
    {
        return $this->belongsToMany('App\Item', 'items_sizes')->withPivot('price');//->withTimestamps();
    }

    public function getAddonsAttribute($value)
    {
        $addon = Addon::where('addons.category_id', $this->category_id)
            ->where('size_id', $this->id)
            ->get();
        return $addon;
    }
}

Item.php (Model)

class Item extends Model
{
    public function options()
    {
        return $this->belongsToMany('App\Option', 'items_options')->withTimestamps();
    }

    public function sizes()
    {
        return $this->belongsToMany('App\Size', 'items_sizes')->withPivot('price');//->withTimestamps();
    }
}

ItemControllerAPI.php (Controller)

class ItemControllerAPI extends BaseControllerAPI
{
    public function show($id)
    {
        // If i call the Size model directly by using setAppends([]) its working fine, 
        // its removing the appended array from Size model

        // $size = Size::all();
        // $size->each->setAppends([]);
        // return $size;

        // If i use it with relationship it won't work.

        // $itemSingleQuery = Item::with(['sizes' => function($query)
        // {
              // Doesn't work
              // $query->setAppends([]);
              // Doesn't work
              // $query->each->setAppends([]);
         // }])
         // ->with('options')
         // ->where('id', $id)
         // ->get();

        // query for getting data with relationships
        $itemSingleQuery = Item::with('sizes')
            ->with('options')
            ->where('id', $id)
            ->get();

         return $this->respondSuccess('content found', $itemSingleQuery);
    }
}

Upvotes: 0

Views: 2031

Answers (2)

Władysław Mostowicz
Władysław Mostowicz

Reputation: 21

I've found out how to do this:

After getting the entire collection we need to call setAppends() in each model, which contains appends, to add or remove them before serialization to array or JSON.

$itemSingleQuery = Item::with('sizes')->get();

$itemSingleQuery->each(function ($item) {
    $item->sizes->each->setAppends(['addons']);
});

Upvotes: 2

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

I would suggest that you set $appends when you need it:

$itemSingleQuery->pluck('sizes')->collapse()->each->setAppends(['addons']);

Upvotes: 1

Related Questions