php - A shopping cart ORM check in Laravel -


this table structure

users

 id username password 

orders

 id user_id 

order_details

 id product_id order_id 

and relationship set

user.php

function orders() {     return $this->hasmany('order'); } 

order.php

function details() {     return $this->hasmany('orderdetail'); } 

orderdetail.php

i got user id , product id, want check if user bought product

user::find($userid)->orders()->details()->where('product_id', $productid)->count() wrong, because orders return more 1 data, how check?

you can't 1 swipe eloquent yet. can write function check:

public function hasboughtproduct($product_id, $user_id) {  $orders = orders::with('details')->where('user_id','=',$user_id)->get();  foreach($orders $order) {     $details = $order->details()->where('product_id', '=', $product_id)->count();     if($details > 0)     {           return true;     }     } return false; } 

if not efficient enough needs, might consider using fluent write query or override newquery() method pre-join tables. if suits more, can refer answer more details on how it: https://stackoverflow.com/a/19183957/385402


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -