【问题标题】:Laravel Accessor on demand on relationshipLaravel 按需访问关系
【发布时间】:2018-07-04 18:01:02
【问题描述】:

问题: 我在 Size.php 模型中有访问器,它与 Item.php 模型相关,在 API 中我需要访问器才能工作,但在其他控制器中我想禁用访问器.我已从所有文件中删除了不必要/不相关的代码。

我想做什么:

在 ItemControllerAPI 中我需要访问器才能工作,但我想禁用其他控制器中的访问器。

我已经看过了: 我已经看过这些链接,但对我不起作用。

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(模型)

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(模型)

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(控制器)

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);
    }
}

【问题讨论】:

  • 使用getOriginal 方法。
  • 我已经尝试过了,它工作正常,当直接应用于像 $model->getOriginal() 这样的模型时,但是当我将它应用于关系时,它会抛出错误“方法 Illuminate\Database\Query\ Builder::getOriginal 不存在。”我像这样使用它 "$query = Item::with(['sizes' => function($query){ $query->getOriginal(); }])->get();"
  • 你能举例说明一下吗?
  • 大小是通过关系访问的。
  • 你的 Laravel 版本是什么?

标签: php laravel accessor


【解决方案1】:

我已经找到了如何做到这一点:

在获得整个集合后,我们需要在每个模型中调用setAppends(),其中包含附加,以便在序列化为数组或 JSON 之前添加或删除它们。

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

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

【讨论】:

    【解决方案2】:

    我建议你在需要时设置$appends

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2020-07-03
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多