【发布时间】:2016-11-21 18:49:10
【问题描述】:
我有两个名为 products_description 的表,主键 products_id 和第二个表 orders_products,外键 products_id 链接这两个表。我们可以说一个产品可以有很多订单。
我为这两个表创建了以下模型。
namespace App;
use Illuminate\Database\Eloquent\Model;
class products_description extends Model
{
protected $table = "products_description";
protected $primaryKey = "products_id";
public function orders_products()
{
return $this->belongsTo('App\Orders_product','products_id','products_id');
return $this->hasMany(Orders_product::class);
}
}
和
namespace App;
use Illuminate\Database\Eloquent\Model;
class Orders_product extends Model
{
protected $primaryKey = "orders_products_id";
}
我的控制器类中的以下代码
class products_controller extends Controller
{
public function show1(Products_description $Products_description)
{
return view('products.show',compact('Products_description'));
}
}
以下代码在我的 show.blade.php 文件中
@extends('layout')
@section('content')
{{ $Products_description->products_name }}
@foreach($Products_description->orders_products as $Orders_product)
{{ $Orders_product->orders_id }}
@endforeach
@stop
我想先显示产品名称,然后是该产品所在的订单 ID。但我收到以下错误。没有 foreach 循环,产品名称显示正常。
试图获取非对象的属性(查看:C:\wamp\www\laravel1\resources\views\products\show.blade.php)
in 0bb3f93ed324818ac22ad70d47add00a1c4f8a7c.php line 11
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\wamp\www\laravel1\storage\framework\views/0bb3f93ed324818ac22ad70d47add00a1c4f8a7c.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'Products_description' => object(products_description))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\wamp\www\laravel1\resources\views/products/show.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'Products_description' => object(products_description))) in View.php line 149
【问题讨论】: