2016-12-10 208 views
0

我的車模型有:的hasMany關係

public function product() { 
    return $this->hasMany('App\Product'); 
} 

我的產品型號有:

public function cart() { 
    return $this->belongsTo('App\Cart'); 
} 

我的數據庫看起來像:

車表:

enter image description here

產品表:

enter image description here

我的購物車遷移包含:

$table->integer('product_id')->unsigned(); 
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); 

我控制器包含:

public function index() 
{ 
    if(Auth::check())  
    { 
     $user = Auth::user(); 
     $cart = Cart::where('user_id',$user->id)->get(); 
     return view('products.cart')->withCart($cart); 
    } 
    else { 
    return view('products.cart'); 
    } 
} 

它檢查是否用戶在隨後登錄其得到所有細節購物車的用戶名與當前用戶相匹配。這是行得通的。

但是當我去product.cart頁我嘗試:

@foreach($cart->product as $p) 
    {{dd($p->name)}} 
@endforeach 

我得到錯誤:

Undefined property: Illuminate\Database\Eloquent\Collection::$product (View: D:\wamp64\www\ecommerce\resources\views\products\cart.blade.php) 

你能告訴我如何與獲得從產品表中的值這種關係。

謝謝!

編輯:

嘗試不同的方法後,我得到了:

車型號:

public function product() { 

     return $this->hasMany('App\Product','id','product_id'); 
    } 

車控制器:

public function index() 
    { 
     if(Auth::check())  
     { 
      $user = Auth::user(); 
      $cart = Cart::where('user_id',$user->id)->first(); 

      return view('products.cart')->withCart($cart); 
     } 
     else { 
      return view('products.cart'); 
     } 
    } 

現在我能得到第一個產品與id匹配。

有沒有辦法讓符合

+0

將產品存儲在購物車表中並不是一個真正的好方法。我會在會話中存儲購物車的內容。 –

+0

'hasMany'的反轉是'belongsTo'。你需要使用它。請參閱[文檔](https://laravel.com/docs/5.3/eloquent-relationships)。我的猜測是,它應該是_product屬於cart_。 – linuxartisan

+0

@linuxartisan更改爲'belongsTo'後出現相同的錯誤 – Phoenix

回答

0

所有的產品可以使用$...->get()回報集合。改爲使用first()

$cart = Cart::where('user_id',$user->id)->first(); 
if($cart) { 
    return view('products.cart')->withCart($cart); 
} else { 
    abort(404); 
} 
+0

我收到錯誤'SQLSTATE [42S22]:未找到列:1054未知列products.cart_id在where子句中(SQL:select * from products where products.cart_id = 1且products.cart_id不爲null)' – Phoenix

+0

嗯,你應該多到很多而不是很多。或者保留你的舊代碼並用一對一替換hasMany(hasOne)。我將使用多到很多(而不是使用購物車表,直接使用訂單表來存儲新列'is_paid'或'is_accepted'的訂單) – trinvh

0

進入用戶的模式:

public function products() 
{ 
    return $this->hasManyThrough('App\Product', 'App\Cart'); 
} 

現在,在你的控制,你會獲取登錄的用戶的購物車。並傳遞給視圖。例如

return view('cart.show', ['cart' => Auth::user()->cart]); 

然後您的foreach將按預期工作。

PS:如果不使用數據庫表的約定命名,則可能必須在hasManyThrough關係上指定ids。即模型單數(產品)複數表(產品)。

0

試試下面的代碼。

在你的車型號

public function product(){ 
    return $this->belongsTo('App\Product','id','product_id') 
} 

在控制器

$cart = Cart::where('user_id',$user->id)->first(); 
+0

我收到錯誤SQLSTATE [42S22]:未找到列:1054未知列'產品'product'中的'where子句'(SQL:select * from'products' where'products'.'product_id' = 1 limit 1) – Phoenix

0

在你的車模型,你需要指定產品的關係:

public function products(){ 
    //return $this->hasMany('App\Product'); should be changed to 
    return return $this->belongsToMany('App\Product','carts_products', 'cart_id', 'product_id'); // see link to tutorial below. 
} 

有了這種關係,您可以啓用延遲加載產品時,你得到Cart對象,供用戶在你的控制器中實現這一點EED:

public function index() 
{ 
    if(Auth::check())  
    { 
     $user = Auth::user(); 
     $cart = Cart::with('products')->where('user_id',$user->id)->first(); 
     //we get cart via first() as we need single instance of cart not an array of carts, 
     //even there's only one cart per user get() will return an array of one item 
     // with('products') lazyloads linked products to cart as you get cart instance 
     return view('products.cart')->with('cart',$cart); 
    } 
    else { 
    return view('products.cart'); 
    } 
} 

在你看來以後你就可以遍歷延遲加載的產品陣列,如:在您將如何在訪問這些方面建立模型之間的關係

@foreach($cart->products as $p) 
{{dd($p->name)}} 
@endforeach 

思考將來,在大多數情況下,你不需要過分指定對象之間的所有關係,因爲只有其中一些你會真正使用代碼。 希望它能幫助你。

編輯

變化關係類型belongsToMany,它應該工作,因爲你的車有很多的產品。您仍然需要使用first()方法獲取一個Cart實例,相關產品將根據您的需要進行加載。

重要:

看看這個教程https://scotch.io/tutorials/a-guide-to-using-eloquent-orm-in-laravel,因爲你需要改變你的數據庫架構來實現這一目標。目前您的模式允許您每個購物車只有一個產品。看看這個例子,他們創造野餐熊表bears_picnics - 在你的情況下,你會有產品到購物車表carts_products在你的情況下通過教程案例cartbearpicnicsproducts

carts_products模式:

Schema::create('carts_products', function(Blueprint $table) 
    { 
     $table->increments('id'); 

     $table->integer('cart_id'); // the id of cart 
     $table->integer('product_id'); // the id of product in cart 

     $table->timestamps(); 
    }); 

刪除product_id場從你推車模式,因爲它是在這種情況下無用。如果您需要級聯刪除,請在carts_products表中指定它。

快樂編碼!感謝您提出一個很好的問題:)使我們深入研究。

+0

我收到錯誤SQLSTATE [42S22]:找不到列:1054未知列的產品。 cart_id'in'where clause'(SQL:select * from'products' where'products'.'cart_id' in(1)) – Phoenix