Laravel与雄辩的关系

时间:2016-07-19 03:10:22

标签: laravel-5 eloquent

我有两个名为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

2 个答案:

答案 0 :(得分:0)

首先这个函数有两个返回值,应该删除第一个:

public function orders_products()
{
    return $this->belongsTo('App\Orders_product','products_id','products_id');
    return $this->hasMany(Orders_product::class);
}

然后在你的控制器功能中:

public function show1(Products_description $Products_description)
{
    return view('products.show',compact('Products_description'));   
}

你不能只是compact('Products_description'),因为它是一个实例。

public function show1(Products_description $Products_description)
{
    $products = $Products_description->all();
    return view('products.show',compact('products'));   
}

以上是您所需要的,或仅$products = $Products_description->find($productID);

答案 1 :(得分:0)

我发现我在belongsTo模型中使用了hasManyproducts_description两种关系,而我的foreign_keylocal_key没有使用hasManybelongsTo {1}}关系。这实际上是问题所在。我删除了foreign_key关系并将local_keyhasMany添加到stage "1" stage "other job call" node{ def job retry(3) { job = build job: 'Called job', parameters: [ [$class: 'StringParameterValue', name: 'Branch', value: "Branch"], ]; } } } stage "2" 关系,它就像一个魅力。