Laravel 5.7其中+ diff

时间:2018-12-04 02:28:58

标签: php laravel eloquent

  

问题是我想使用diff,因为在我的show.blade.php中,我已经拥有该id=1项目。然后如何跳过该项目并接受。

 public function show($id){
 $product = Product::findOrFail($id);
 $collection = Product::where('request_register',$product->request_register)->diff('request_register',$product->request_register)->get();
    return view('products.show', compact('product','collection'));
}

错误是:

  

调用未定义的方法Illuminate \ Database \ Eloquent \ Builder :: diff()

1 个答案:

答案 0 :(得分:2)

diff()是在Illuminate\Database\Eloquent\Collection中定义的,在调用get()方法之前,需要先调用方法diff()

public function show($id){
    $product = Product::findOrFail($id);
    $collection = Product::where('request_register', $product->request_register)
        // Call get() to return Illuminate\Database\Eloquent\Collection.
        ->get()
        ->diff('request_register',$product->request_register);
    return view('products.show', compact('product','collection'));
}

请参阅:

Illuminate\Database\Eloquent\Builder | Laravel API

Illuminate\Database\Eloquent\Collection | Laravel API