【问题标题】:Attempt to read property "price" on null in laravel?尝试在 laravel 中读取 null 上的属性“价格”?
【发布时间】:2021-08-27 09:19:55
【问题描述】:

我正在尝试制作订单管理系统。为此,我为订单制作了 2 个表 orders 和 orderitems。

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

这是我的订单控制器

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

这是我的产品表

这是我的订单表

这是我的订单项表

【问题讨论】:

  • 所以,错误消息的有趣之处在于,它们可以准确地告诉您出了什么问题。因此,您在一个值为null 的变量上调用-&gt;price,而不是一个对象。所以从那里向后工作:如果一个变量为空,但你期望一个值,看看那个变量的值是在哪里分配的。

标签: laravel laravel-8


【解决方案1】:

您访问价格错误。 'price' =&gt; $order-&gt;$product-&gt;price,应该是

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

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

订单模式

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

产品型号

<?php

namespace App\Models;

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

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:这只是sn-p来指导你。你可以比这更好地改进逻辑

【讨论】:

  • 检查产品表中是否有产品。可能没有记录。所以最好检查一下
  • 在产品订单表中也显示关系
  • 在 products 表中有 2 列名为“name”和“price”
  • 显示模型和模型的表结构
  • 感谢您的帮助,您为我投入了宝贵的时间,非常感谢
猜你喜欢
  • 2022-10-12
  • 2022-11-16
  • 2023-01-25
  • 2022-01-04
  • 2021-06-25
  • 2023-01-28
  • 2021-05-25
  • 2022-06-10
  • 2021-08-08
相关资源
最近更新 更多