KB_Shayan
KB_Shayan

Reputation: 652

Laravel: Error trying to display images using image name from database

On my website, registered users can view other users posts. These posts can have multiple images. the image names are stored in the images tables and have a post_id as foreign key to know which post the images are for. The image name is stored in the images table and the actual image is stored in the storage folder. My issue is when I try and use a loop to retrieve all and display the images that are relevant to the post, I am presented with the following error.

 ErrorException (E_ERROR)
Property [cover_image] does not exist on this collection instance. (View: C:\xampp\htdocs\eventcw\resources\views\viewmore.blade.php)

Here is my Post model images function:

public function images()
{
    return $this->hasMany('App\Image');
}

Here is my Image model post function:

public function images()
{
    return $this->hasMany('App\Image');
}

Here is my view:

@foreach($post->images->cover_image as $image)
<div class="row">
    <div class="col-md-12">
        <img style="width: 700px; height: 500px;" src="{{ asset('storage/cover_images/' . $post->images->cover_image) }}"> 
    </div>
</div>
@endforeach

This is my controller method that loads the view:

public function getMore($post_id){
    $post = Post::where('id', $post_id)->firstOrFail();
    return view ('viewmore',  ['post' => $post]); 
}

Here is my post and image migration:

    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('title');
        $table->enum('type', array('sport','culture','other'));
        $table->string('subtype');
        $table->text('body');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
    });

    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('cover_image');
        $table->integer('post_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts');
        $table->timestamps();
    });

Upvotes: 0

Views: 413

Answers (1)

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

You are getting this error because in you foreach loop

use src="{{ asset('storage/cover_images/' . $image->cover_image) }}"

instead of $post->images->cover_image

@foreach($post->images->cover_image as $image)
<div class="row">
    <div class="col-md-12">
        <img style="width: 700px; height: 500px;" src="{{ asset('storage/cover_images/' . $image->cover_image) }}"> 
    </div>
</div>
@endforeach

Secondly you are not using eager loading get the images for Post.

$post = Post::where('id', $post_id)->with('images')->firstOrFail();

Or you can

 @foreach($post->images()->get() as $image)
    <div class="row">
        <div class="col-md-12">
            <img style="width: 700px; height: 500px;" src="{{ asset('storage/cover_images/' . $image->cover_image) }}"> 
        </div>
    </div>
    @endforeach

Hope this helps

Upvotes: 2

Related Questions