是否尝试读取laravel中空值的属性&价格&? [英] Attempt to read property "price" on null in laravel?

查看:24
本文介绍了是否尝试读取laravel中空值的属性&价格&?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在试着做订单管理系统。为此,我为订单创建了两个表OrdersorderItems

我想通过关系获取所选产品的价格。

这是我的订单控制器

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

这是我的订单模型。

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

这是我的产品型号

use HasFactory;

protected $table = "products";

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

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

这是我的产品表

这是我的订单表

这是我的orderItems表

推荐答案

您取价错误。'price' => $order->$product->price,应为

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

更新 我认为您可以将关系修改为belongsToMany

订单模型

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Order extends Model
{
    use HasFactory;

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

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

产品型号

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

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

在您的控制器方法中

 $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],
    ]);

首先构建类似于

的多维数组结构的数组
[ 
productid=>['quantity'=>value, 'price'=>value,'total'=>value],
productid=>['quantity'=>value, 'price'=>value,'total'=>value],
]

不确定您的$Request数据如何,我想您可以执行以下操作

$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,
        ]);

    }
    

然后您可以像

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

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

Note:这只是指导您的片断。您可以更好地改进逻辑

这篇关于是否尝试读取laravel中空值的属性&价格&?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆