samedi 11 novembre 2017

Laravel 5.2 Invalid argument supplied for foreach()

I am trying to show my data from two database tables and I get the error message Invalid argument supplied for foreach(). Anyone know how I solve this get error?

NB: I am new in laravel

Here is my Controller:

$post = PostModel::find($post_id);
$comment = new CommentModel;

My PostModel:

public function comments() {
    return $this->belongsTo('App\CommentModel');
}

my CommentModel:

 public function post() {
         return $this->belongsTo('App\PostModel');
    }

My view page where I want to display my database models on:

 @foreach($post->comments as $comment)  
<div class="comment"> 
    <div class="author-info">
        <div class="author-name">
            <h4></h4>
        </div>      
    </div>      
    <div class="comment-content">
             
    </div>  
</div>
@endforeach

Here is my comment table

public function up() 
{
    Schema::create('comments', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email');
    $table->text('comment');
    $table->boolean('approved');
    $table->integer('post_id')->unsigned();
    $table->timestamps();
  });

    Schema::table('comments', function ($table){
          $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
         });
     }


     public function down()
     {
         Schema::dropForeign(['post_id']);
         Schema::drop('comments');
     }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2AB8llh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire