du8442 2016-10-28 10:02
浏览 48
已采纳

Laravel 5.2 @foreach - 提供的参数无效

I'm trying to show data from 2 related database tables and I get the @foreach - invalid argument supplied error. does anyone know what I did wrong and know how to fix it?

Here is my Controller function

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

as u can see i have a model "voorraad" and "producten" its a 1:n relation in the database "producten" beeing the 1 side and "voorraad" beeing the n side of the relation between them,

This is my "voorraad" model

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

This is my "Producten" model

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

and this is the view I want to display my database models on:

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

and this is my migrations for the producten table

 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');
    });
}
  • 写回答

1条回答 默认 最新

  • douba2011 2016-10-28 13:29
    关注

    The problem lies within the Voorraad model, where you defined the relation:

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

    But in the view you try to access the relation by calling

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

    If you change the method in the model it should work

    public function producten()
    {
       return $this->HasMany('App\Producten', 'Producten_ProductenId');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料