2016-10-28 133 views
0

我试图显示来自2个相关数据库表的数据,并得到了@foreach - 无效参数提供的错误。有谁知道我做错了什么,并知道如何解决它?Laravel 5.2 @foreach - 提供的参数无效

这里是我的控制器功能

public function index() 
{ 
    return view('Voorraad.index', [ 
    'voorraads' => Voorraad::all(), 
    'Products' => Producten::all() 
    ]); 
} 

就像你看到的,我有一个模型“voorraad”和“producten”它是一个1:n的数据库关系“producten” beeing 1个侧和“voorraad “beeing它们之间的关系的N侧,

这是我的 ”voorraad“ 模式

class Voorraad extends Model 

// Model Voorraad has the attributes, Aantal and Id; 

{ 
    protected $fillable = ['voorraadId','aantal']; 
    protected $table = 'Voorraad'; 

public $timestamps = false; 


/* 
* Voorraad can have multiple products; 
* 
*/ 

public function Product() 
{ 
    return $this->HasMany('App\Producten', 'Producten_ProductenId'); 
} 
} 

这是我的 ”Producten“ 模式

class Producten extends Model 
{ 

// model producten holdes the attribues naam, inkoopprijs, verkoopprijs, 

protected $fillable = ['productId','naam','inkoopprijs','verkoopprijs']; 
protected $table = 'Producten'; 

public $timestamps = false; 


//producten Belongs to "voorraad" 

public function voorraad() 
{ 
    return $this->belongsTo('App\Voorraad', 'producten_productenId'); 
} 
} 

,这是我想上显示我的数据库模型的观点:

@foreach($voorraads as $voorraad) 

    <tr> 
     <td>{{$voorraad->voorraadId}}</td> 
     <td>{{$voorraad->aantal }}</td> 
      @foreach($voorraad->producten as $products) 
      <td>{{ $products->Naam }}</td> 
      <td>{{ $products->InkoopPrijs }}</td> 
      <td>{{ $products->VerkoopPrijs }}</td> 
      @endforeach 
     <td></td> 
     <td></td> 
    </tr> 
    @endforeach 

This is the dd of $voorraads

This is my migration for the voorraad Table 

public function up() 
{ 
    Schema::create('voorraad', function(blueprint $table) 
    { 
      $table->increments('id'); 
      $table->string('naam'); 
      $table->foreign('Locaties_Id') 
      ->reference('Id') 
      ->on('locaties') 
      ->onDelete('cascasde'); 
      $table->foreign('Producten_Id') 
      ->reference('Id') 
      ->on('producten') 
      ->onDelete('cascade'); 
    }); 
} 

,这是我的producten表

public function up() 
{ 
    Schema::create('producten', function(blueprint $table) 
    { 
     $table->increments('id'); 
     $table->decimal('inkoopprijs', '10, 2'); 
     $table->decimal('verkoopprijs', '10, 2'); 
     $table->foreign('Fabrieken_Id') 
     ->reference('Id') 
     ->on('fabrieken') 
     ->onDelete('cascasde'); 
    }); 
} 
+0

这是几乎不可能知道foreach中出现了什么问题,除非提供了循环的实际数据。换句话说,转储'$ voorraads'并将其添加到问题中。 – Andrew

回答

1
迁移

问题出在你身上的Voorraad模型中定义的关系:

public function Product() 
{ 
    return $this->HasMany('App\Producten', 'Producten_ProductenId'); 
} 

但在视图中,尝试通过调用

@foreach($voorraad->producten as $products)

如果更改模型的方法来访问的关系,它应该工作

public function producten() 
{ 
    return $this->HasMany('App\Producten', 'Producten_ProductenId'); 
} 
+0

感谢@trommelaap为这个响应,但是当我改变方法名称时,我得到了另一个错误,但这次我得到这个错误:'代码'SQLSTATE [42S22]:未找到列:1054未知列'Producten.Producten_Id'在'哪里子句'(SQL:select * from'Producten',其中'Producten'.'Producten_Id'为空,'Producten'.'Producten_Id'不为空)你知道我需要更改到我的数据库吗? –

+0

这意味着您已将关系设置为错误。 – trommelaap

+0

Voorraad模型中producten方法的第二个参数应该是producten表中与Voorraad表相关的外键列的名称。也许voorrraad_id?请发布2个表的迁移。 – trommelaap