【问题标题】:eloquent relationship not working returning Trying to get property of non-object (Edited)雄辩的关系不起作用返回试图获取非对象的属性(已编辑)
【发布时间】:2019-12-05 20:58:54
【问题描述】:

产品名称/标题表列与代码中使用的 My eloquent 关系不匹配

@if(is_null($order->orderid)) 没有产品 @else {{$order->products- >title}} @endif

    <tbody>
   @foreach($orders as $order)                     
       <tr class="">               
        @endif
         <td>{{$order->customer_email}}</td>
         <td>{{$order->customer_name}}</td>
         <td>{{array_sum($order->quantities)}}</td>
         <td>{{$order->customer_city}}</td>
         <td>@if(is_null($order->orderid)) No Product @else {{$order->products->title}} @endif</td>

         <td>{{$order->method}}</td>
        </tr>
   @endforeach
</tbody>

这是我的产品型号和![数据库结构]https://i.postimg.cc/fT4t8xFv/products.png [![产品型号截图][1]][1]

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'title', 'category', 'tags', 'description', 'sizes', 'price', 'previous_price', 'stock',
        'feature_image', 'policy', 'featured', 'views', 'created_at', 'updated_at', 'status'
    ];

    public static $withoutAppends = false;

    public function vendor()
    {
        return $this->belongsTo('App\Vendors', 'vendorid', 'id');
    }

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

这是我的订单模型和![数据库结构]https://i.postimg.cc/yN5YkKCT/orders.png [![订单模型截图][2]][2]


namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $fillable = [
        'customerid', 'products', 'quantities', 'method', 'shipping', 'pickup_location', 'pay_amount', 
        'txnid', 'charge_id', 'order_number', 'payment_status', 'customer_email', 'customer_name', 
        'customer_phone', 'customer_address', 'customer_city', 'customer_zip', 'shipping_name', 
        'shipping_email', 'shipping_phone', 'shipping_address', 'shipping_city', 'shipping_zip', 
        'order_note', 'booking_date', 'status'
    ];

    public $timestamps = false;

    public static $withoutAppends = false;

    public function products()
    {
        return $this->hasMany('App\Product', 'orderid', 'id');
    }
}

试图获取非对象的属性

【问题讨论】:

  • orders表的外键添加到products表中。
  • 你不能直接访问$order-&gt;products-&gt;title它在hasMany关系中。 public function order() { return $this-&gt;belongsTo('App\Order', 'here should be foreign key not primary key', 'id'); } 或者在关系中您提到与许多产品相关的订单,但在产品表的屏幕截图中没有产品 ID。我认为你误解了关系。
  • 你搞砸了关系。如果您想使用 hasMany 关系,请使用数据透视表或为订单中的每个产品创建一个新行并使用 belongsTo 关系。
  • 已经添加了order_id外键

标签: laravel eloquent laravel-blade eloquent-relationship


【解决方案1】:

你的products()是一个对象的集合,你不能得到其中的title,如果你想展示订单的产品,我建议这样:

<td>
    @if(is_null($order->products)) 
    No Product 
    @else
        @foreach($order->products()->get() as $product)
             {{$product->title}} - 
        @endforeach
    @endif
</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2021-06-30
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多