0

I'm building a platform where a user can sign up and add products to sell. Also another user can buy products listed. When a buyer place an order, i want it to be seen by a seller(user) who listed the product. Right now all users can see all orders even if they are not sellers.

I have no idea how to proceed but here is my

order function in product controller

 //Orders View Function
public function viewOrders(){
    $orders = Order::with('orders')->orderBy('id','Desc')->get();
    return view('orders')->with(compact('orders'));
 }

Any help will be appreciated.

joh
  • 238
  • 1
  • 5
  • 25
  • it all depends on your database structure how you have designed it. if you can show you db structure and what you have tried so far – Salman Zafar Jun 13 '19 at 20:28
  • In my database there are two tables `orders table` which has product_id, user_id, shipping_name,and shipping_city, also another table `order_product table` which has order_id, product_id, and quantity. @Salman – joh Jun 13 '19 at 20:37

3 Answers3

0

Your viewOrders action should be hidden behind authentication process (obviously, user has to first sign in before he can view his orders). Once you do that (using auth middleware for it's route), Laravel can resolve authenticated user for you - you can simply hint it as a parameter of your viewOrders action:

public function viewOrders(User $user)
{
    $orders = $user->orders()->orderBy('id', 'desc')->get();

    return view('orders')->with(compact('orders'));
}

As you notice, here I've modified your query a little bit. Instead of selecting all orders we are now selecting them through authenticated user's relation to it's orders. Orders obviously belong to user, but through products (as an intermediate) - one user can have many products, one product can have many orders, thus one user can have many orders. In Laravel this kind of relationship between eloquent models is called HasManyThrough. Therefore you should declare it in your User model:

class User extends Model {
    ...

    public function orders()
    {
        return $this->hasManyThrough(Order::class, Product::class);
    }
}

Note that your Product will probably also have orders() relation, but it will be of HasMany type instead since it's a direct one without any intermediates.

d3jn
  • 1,392
  • 3
  • 13
  • 21
  • Now am getting this`SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.products_model_id' in 'on clause' (SQL: select `orders`.*, `products`.`user_id` as `laravel_throu` @d3jn – joh Jun 13 '19 at 22:16
  • In my products model It is renamed as "Products_model" so I had to change `Product::class` to `Products_model::class` – joh Jun 13 '19 at 22:17
  • @joh if your names differ from Laravel's naming convention then you will have to specify correct table and foreign key names for your relations. In your case it will be `$this->hasManyThrough(Order::class, Product_model::class, 'user_id', 'product_id');` (Laravel tries to guess your foreign key column names based of your model names, so for `Product_model` it was expecting `product_model_id` to exist). – d3jn Jun 13 '19 at 22:42
  • It gives this `Call to a member function orderBy() on null` @d3jn – joh Jun 13 '19 at 22:52
  • @joh `hasManyThrough()` can not under any circumstances return null value. It always returns instance of `HasManyThrough` relation object. So make sure you actually return it from your relation `orders()` function in `User` model. – d3jn Jun 14 '19 at 07:01
0

Filter your query base on your needs

Example:

Model Relationship

public function orders(){
    return $this->hasMany(Order::class);
}
public function getMyOrders(User $user){
    $orders = Order::with('orders')->orderBy('id','Desc')->where('user_id','=', $user->id)->get();
return view('orders')->with(compact('orders'));
}

To get sellers Orders

public function getSellerOrders(User $user){
    $products = Product::where('user_id', '=', $user->id)->get();
    $orders = [];
    foreach($products as $product){
        array_merge($orders, $product->order);
    }
    return view('orders')->with(compact('orders'));
}
0

In your DB table for orders you are suggested to have 2 IDs, one which denotes Buyer and Other which denotes Seller, i.e., Whenever a user places an order, then it must be a buyer then insert it's(buyer) ID into buyer_id in Order's table, similarly the product_id for denoting produc from Products' Table should be there and from Product's Table you can get seller_id which is suggested to be there for denoting which seller owns a particular product. Insert seller_id into Order's Table and use it to your query as:

$seller = Order::where('seller_id',$variable_for_product_id)->with('orders')->get();

Do this for all sellers in your Order's Table

  • what does this mean $variable_for_product_id @Sumit – helloworld Jul 04 '19 at 14:58
  • @helloworld `$variable_for_product_id` is the variable which have the ID of seller from `orders` table i.e., `seller_id` as a foreign key from Seller's Table to Order's Table, which is inserted with the help of `seller_id` in product's table – Sumit Advani Jul 05 '19 at 01:32