user16160492
user16160492

Reputation:

Attempt to read property "price" on null in laravel?

I am trying to make order management system. for that i make 2 tables orders and orderitems for order.

I want to get price of selected product through relationship.

This is my OrderController

public function store(Request $request)
{
    $product = Product::all();

    $order = Order::create([
        'user_id' => $request->input('user_id'),
        'total' => 1,
    ]);

    $size = count(collect($request)->get('quantity'));

    for ($i = 0; $i < $size; $i++) {

        $orderitem = Orderitem::create([

            'order_id' => $order->id,
            'product_id' => $request->get('product_id')[$i],
            'quantity' => $request->get('quantity')[$i],
            'price' => $order->$product->price,
            'total' => 3,''
        ]);

    }

    return redirect()->route('orders.index');
}

and this is my order model.

protected $table = "orders";

protected $fillable = [
    'user_id',
    'total',
];

public function user() {
    return $this->belongsTo(User::class);
}

public function product() {
    return $this->belongsTo(Product::class);
}

public function orderitem() {
    return $this->hasMany(Orderitem::class);
}

This is my Product model

use HasFactory;

protected $table = "products";

protected $fillable = [
    'name',
    'price',
];

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

and this is my products table

enter image description here

this is my orders table

enter image description here

this is my orderitems table

enter image description here

Upvotes: 0

Views: 6270

Answers (1)

John Lobo
John Lobo

Reputation: 15319

You are accessing price wrongly. 'price' => $order->$product->price,It should be

'price' => $order->product->price,

Updated I think you can modify relationship to belongsToMany

Order Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasFactory;

    protected $guarded=['id'];
    public function product(){

        return $this->belongsToMany(Product::class,'order_items')->withPivot('quantity', 'price','total')->withTimestamps();
    }
}

Product Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;
    protected $guarded=['id'];
}

In your controller method

 $order = Order::create([
        'user_id' => 1,
        'total' => 1,
    ]);

    $order->product()->sync([
        1=>['quantity'=>2, 'price'=>20,'total'=>40],
        2=>['quantity'=>1, 'price'=>10,'total'=>10],
    ]);

first you build array like multidimensional array structure like

[ 
productid=>['quantity'=>value, 'price'=>value,'total'=>value],
productid=>['quantity'=>value, 'price'=>value,'total'=>value],
]

Not sure how your $request data will be i guess you can do the following

$size = count(collect($request)->get('quantity'));
 $orderitem=[];
    for ($i = 0; $i < $size; $i++) {

        $orderitem[$request->get('product_id')[$i]] = [
        
            'quantity' => $request->get('quantity')[$i],
            'price' => Product::find($request->get('product_id')[$i])->price,
            'total' => 3,
        ]);

    }
    

then you can pass like

 $order = Order::create([
        'user_id' => 1,
        'total' => 1,
    ]);

    $order->product()->sync($orderitem);

Note:This is just snippet to guide you .You can improve logics better than this

Upvotes: 1

Related Questions