samedi 15 juin 2019

How to display orders of buyer and seller from database to the dashboard?

I'm working on a small platform where user can sell also buy products. So if a buyer buys product he should see his orders information like(quantity, total etc.) in the dashboard, and also a seller that sold the product should see the order information like(shipping info, products etc.)

Here is Phpmyadmin database tables

https://imgur.com/a/fvxo1YZ

Here is my code

Seller view blade(loop)

 @foreach($sells as $sell) 
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td>
        <a href="">View Order Details</a>
    </td>
  </tr>
 @endforeach

Buyer View blade loop

  @foreach($orders as $order) 
  <tr>
    @foreach($order->cart->products as $item)
    <td></td>
    <td></td>
    <td>$</td>
    @endforeach
  </tr>
 @endforeach

Seller Orders Function

    // Seller Orders 
  public function viewOrders(User $user)
  {

  // $products = Products_model::where('seller_id', '=', $user->id)->get();
  // all sells
  $sells = $user->orderFromSellers();
  //dd($sells);
  return view('orders')->with(compact('sells'));

  }

Buyer Orders Function

   //Buyer Orders
  public function myOrders()
  {
  $cart = session();
  $orders = Auth::user()->orders;
  $orders->transform(function($order, $key){
    dd($orders);
      $order->cart = unserialize($order->cart);
      return $order;
  });
  return view('myOrders', ['orders' => $orders]);
  }

Order.php

  <?php
 namespace App;
 use Illuminate\Database\Eloquent\Model;
 class Order extends Model
{
//protected $table = 'orders';
protected $fillable =  [
    'shipping_email', 'shipping_name', 'shipping_city', 'shipping_phone', 'billing_subtotal', 'billing_total',
];

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

public function products()
 {
    return $this->belongsToMany('App\Products_model')->withPivot('quantity');
 }

 public function orders(){
     return $this->hasMany('App\OrderProduct', 'order_id');
 }

 }

OrderProduct.php

  <?php
  namespace App;
 use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'buyer_id', 'seller_id','product_id', 'quantity'];

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

public function buyer()
{
    return $this->belongsTo(User::class, 'id', 'buyer_id');
}

public function seller()
{
    return $this->belongsTo(User::class, 'id', 'seller_id');
}

public function order()
{
  return $this->belongsTo(Order::class);
 }
 }

User.php

  <?php
 namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'Seller'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token', 
];

//public function isSeller() {
 //   return $this->seller;
//}

public function products()
{
  return $this->hasMany(Products_model::class);
}
/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function orders()
{
   return $this->hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
}

public function orderFromBuyers()
{
  return $this->hasManyThrough(OrderProduct::class, Products_model::class, 'buyer_id', 'product_id');
}

public function orderFromSellers()
{
    return $this->hasManyThrough(OrderProduct::class, Products_model::class, 'seller_id', 'product_id');
}
}

My code is now showing empty data in dashboard but in database it shows all the orders. Any help will be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire