vendredi 14 juin 2019

Eloquent relationships in laravel Problems

I'm having issues with how to deal with tables relationships in laravel. i have three tables Orders table, Order_product table and User table. A User can either be described as a seller or a buyer depending on if they listed or bought something. Now when a user submit order form i get an error

"General error: 1364 Field 'seller_id' doesn't have a default value (SQL: insert into order_product (order_id, product_id, quantity, `up ▶"

Here is how those tables look like in phpmyAdmin

https://imgur.com/a/fvxo1YZ

And below are the models

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');
  }
  }

Products_model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class products_model extends Model
{
protected $table='products';
protected $primaryKey='id';
protected $fillable= ['seller_id','pro_name','pro_price','pro_info','image','stock','category_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);
 }
 }

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');
 }

 }

Here is my store Function

    public function store(Request $request)
 {
    //Insert into orders table
    $order = Order::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

    //Insert into order product table
    if ($order) {
        foreach(session('cart')  as $productId =>$item) {
           if (empty($item)) {
               continue;
           }
           OrderProduct::create([
            'order_id' => $order->id ?? null,
            'product_id' => $productId,
           // $products=DB::table('products')->where('id',$id)->get();
            'quantity' => $item['quantity'],
            //dd($item)
        ]);
       }
    }

   //Empty Cart After  order created
    $cart = session()->remove('cart');
     return redirect()->route('confirmation.index')->with('success_message', 'Thank you! Your payment has been successfully accepted!');
   }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire