【问题标题】:Argument 1 passed to App\Http\Controllers\ApiController::showAll() must be an instance of Illuminate\Database\Eloquent\Collection传递给 App\Http\Controllers\ApiController::showAll() 的参数 1 必须是 Illuminate\Database\Eloquent\Collection 的实例
【发布时间】:2020-02-06 05:17:46
【问题描述】:

我想检索特定卖家的所有买家。当我删除 pluck 和 get 方法后链接的其他方法时,它正在工作。但不是我想要的确切的东西。我该如何解决这个问题?

<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\ApiController;
use App\Seller;
use Illuminate\Http\Request;

class SellerBuyerController extends ApiController
{

    public function index(Seller $seller)
    {
        $buyers = $seller->products()
                ->whereHas('transactions')
                ->with('transactions.buyer')
                ->get()->pluck('transactions')
                ->collapse()->pluck('buyer')
                ->unique('id')
                ->values();

        return $this->showAll($buyers);
    }

    protected function showAll(Collection $collection, $code = 200)
    {
        return $this->successResponse($collection, $code);
    }

    protected function successResponse($data, $code)
    {
        return response()->json($data, $code);
    }

}

卖家模型与产品有很多关系

<?php

namespace App;

use App\Scopes\SellerScope;

class Seller extends User
{

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

产品模型与交易有很多关系

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;


    protected $fillable = [
        'name', 'description', 'quantity', 'status', 'image', 'seller_id',
    ];

    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }

}

交易模式和与买家的关系

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Transaction extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'quantity', 'buyer_id', 'product_id'
    ];

    public function buyer()
    {
        return $this->belongsTo(Buyer::class);
    }

}

【问题讨论】:

  • Laravel 6.0 版
  • 你能展示你的模型的关系方法吗?
  • @ShahadatHossain 编辑您的问题并将代码放在那里,而不是在评论中!
  • 看看下面的答案怎么样?

标签: laravel collections eloquent


【解决方案1】:

您应该将use Illuminate\Support\Collection; 放在 apiResponser.php 上的文件夹特征中

【讨论】:

    【解决方案2】:

    如果您的关系从 BuyerTransaction,您可以从另一个方向进行此操作以吸引买家。您还需要确保存在从 ProductSeller 的关系(确保每个关系设置都相反)

    Buyer::whereHas('transactions.products.seller', function ($query) use ($seller) {
        $query->where('id', $seller->id); // might have to be a more specific key name
    })->get();
    

    您最终会得到一个 Eloquent 买家集合,这些买家的交易包括来自特定卖家的产品。

    【讨论】:

      【解决方案3】:

      您在顶部缺少导入:

      use Illuminate\Support\Collection;
      

      否则假定使用Illuminate\Database\Eloquent\Collection

      values() 显然返回了支持集合,而不是一个雄辩的集合。

      【讨论】:

      • 得到同样的错误是没有意义的。只需尝试并分享另一个错误,因为它不可能是相同的 100%
      • 使用 Illuminate\Database\Eloquent\Collection;如果使用 Illuminate\Support\Collection,我将其导入;
      • 非常感谢@nakov
      • @ShahadatHossain 有一个选项可以接受答案,因为它也有效。
      猜你喜欢
      • 1970-01-01
      • 2020-11-26
      • 2020-07-14
      • 2023-03-09
      • 2017-09-26
      • 1970-01-01
      • 2017-12-24
      • 2020-02-23
      • 1970-01-01
      相关资源
      最近更新 更多