Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aguirreg's avatar

Laravel 5.7 show.blade.php file not displaying uploaded images

Hey everyone, I am having trouble uploading images to my blog project. When I view my show.blade.php file, I cannot see the image that was uploaded, just a small image icon.

I made sure to use php artisan storage:link as well but when I look at my database in Sequel Pro, the images are not being stored properly in "storage". They are being stored to /private/var/folders/j5/

Hopefully someone can help out and thanks so much in advance. Also, please let me know if I need to include any other files.

Here is my PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Storage;
use App\Post;
use App\User;

class PostController extends Controller
{

  public function __construct()
  {
      $this->middleware('auth');
  }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::with('user')->get();
        $user_id = auth()->user()->id;
        $users = User::find($user_id);
        return view('index', compact('posts'));

        //return view('index', ['posts' => $posts]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $post = new Post;

        $request->validate([
            'title'=>'required',
            'body'=>'required',
        ]);

        if ($request->image != null) {
        $path = Storage::putFile('public', $request->file('image'));
        $url = Storage::url($path);
        $post->image = $url;
      }

        Post::create([
          'title' => request('title'),
          'body' => request('body'),
          'image' => request('image'),
          'user_id' => auth()->id()
        ]);

        return redirect()->route('home');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $post = Post::find($id);
        return view('show', compact('post'));
    }

    public function edit($id)
    {
        $post = Post::find($id);

        return view('edit', ['post' => $post]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $post = Post::find($id);

        $post->title = $request->title;
        $post->body = $request->content;

        $post->update();

        return redirect()->route('home', ['post' => $post]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $post = Post::find($id);

        $post->delete();

        return redirect()->route('home');
    }
}

Here is my Post.php model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['title', 'body', 'image', 'user_id'];

    public function user(){
      return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * The has Many Relationship
     *
     * @var array
     */
    public function comments()
    {
        return $this->hasMany(Comment::class)->whereNull('parent_id');
    }
}

Here is my migration file:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('posts');
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('body');
            $table->string('image')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::table('posts', function (Blueprint $table) {
            $table->unsignedInteger('user_id');

            $table->foreign('user_id')->references('id')->on('users');
        });

        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('post_id')->unsigned();
            $table->integer('parent_id')->unsigned()->nullable();
            $table->text('body');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
      Schema::dropIfExists('posts');
      Schema::dropIfExists('comments');
    }
}

Here is my web.php routes file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


 //Auth::routes();

 // Authentication Routes
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

 // Registration Routes
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

 // Password Reset Routes
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');


// Route::get('/home', 'HomeController@index')->name('home');

Route::get('/home', 'PostController@index')->name('home');

Route::name('create_post')->get('/create', 'PostController@create');
Route::name('store_post')->post('/home', 'PostController@store');
Route::name('view_post')->get('/{id}', 'PostController@show');
Route::name('edit_post')->get('/{id}/edit', 'PostController@edit');
Route::name('update_post')->put('/{id}', 'PostController@update');
Route::name('delete_post')->delete('/{id}', 'PostController@delete');

//Route::resource('comments', 'CommentController');
Route::name('store_comments')->post('/{post_id}', 'CommentController@store');

Here is my create.blade.php:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Post</div>
                <div class="card-body">
                  <form action="{{ route('store_post') }}" method="post" enctype="multipart/form-data">
                    @csrf
                    <br>
                    <div class="form-group">
                      <label for="title">Title</label>
                      <input type="text" name="title" class="form-control">
                    </div>

                    <div class="form-group">
                      <label for="body">Add a description...</label>
                      <textarea name="body" rows="10" class="form-control"></textarea>
                    </div>

                    <label class="btn btn-default">
                      Browse <input type="file" name="image">
                    </label>

                    <div class="form-group">
                      <button type="submit" class="btn btn-outline-success">Create Post</button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Here is my show.blade.php:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
          <br>
          <br>
            <div class="card">
                <div class="card-body">
                    <h3 class="text-center text-success">Tōku</h3>
                    <br/>
                    @if($post->image != null)
                      <img src="{{ asset($post->image) }}" class="card-img-top">
                    @endif
                    <h2>{{ $post->title }}</h2>
                    <p>
                        {{ $post->body }}
                    </p>
                    <p>Posted {{ $post->created_at->diffForHumans() }} by {{ Auth::user()->username }} </p>
                    <hr />
                    <h4>Display Comments</h4>

                    @include('display_comments', ['comments' => $post->comments, 'post_id' => $post->id])

                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('store_comments', ['post' => 'post_id']) }}">
                        @csrf
                        <div class="form-group">
                            <textarea class="form-control" name="body"></textarea>
                            <input type="hidden" name="post_id" value="{{ $post->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" value="Add Comment" />
                        </div>
                    </form>
                    <div class="container">
                    <form action="{{ route('delete_post', ['post' => $post->id]) }}" method="POST">
                      <a href="{{ route('home') }}" class="btn btn-outline-secondary btn-md">Back</a>
                      <a href="{{ route('edit_post', ['post' => $post->id]) }}" class="btn btn-outline-info btn-md">Edit</a>
                      @csrf
                      @method('DELETE')
                      <button type="submit" class="btn btn-outline-danger btn-md"name="button">Delete</button>
                    </form>
                  </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

I notice you create a new post, put the filepath in it, but then create a totally new post a little later down.

aguirreg's avatar

@SNAPEY - Oh, whoops! Yeah, I commented out the first creation of Post but now I'm getting another error that I've been fighting with that says Creating default object from empty value and the error message refers to PostController.php at the line that says $post->image = $url For some reason my images just don't want to save correctly.

aguirreg's avatar

I fixed my problem. My PostController.php file now looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Storage;
use App\Post;
use App\User;

class PostController extends Controller
{

  public function __construct()
  {
      $this->middleware('auth');
  }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::with('user')->get();
        $user_id = auth()->user()->id;
        $users = User::find($user_id);
        return view('index', compact('posts'));

        //return view('index', ['posts' => $posts]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $post = new Post;

        $request->validate([
            'title'=>'required',
            'body'=>'required',
        ]);

        $post->title = $request->title;
        $post->body = $request->body;

        if ($request->image != null) {
        $path = Storage::putFile('public', $request->file('image'));
        $url = Storage::url($path);
        $post->image = $url;
      }

        $post->user_id = auth()->user()->id;

        $post->save();

        return redirect()->route('home');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $post = Post::find($id);
        return view('show', compact('post'));
    }

    public function edit($id)
    {
        $post = Post::find($id);

        return view('edit', ['post' => $post]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $post = Post::find($id);

        $post->title = $request->title;
        $post->body = $request->content;

        $post->update();

        return redirect()->route('home', ['post' => $post]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $post = Post::find($id);

        $post->delete();

        return redirect()->route('home');
    }
}

Please or to participate in this conversation.