Olasunkanmi
Olasunkanmi

Reputation: 333

Image persisting in my database but not displaying on the frontend

I am working on a project with Laravel 5.2. I need to create, edit and display user profile. The user profile should have an uploaded user image. Below is a code snippet from my controller. This is meant to persist the user photo, drop it in a file called images. The photos persists in the database, it doesn't drop in the images folder neither does it display on the front end.

public function store(UserRequest $request)
{
    /*   User::create($request->all());
       */

    $input=$request->all();
    $input['password']=bcrypt($request->password);

    if ($file=$request->file('photo_id')){

        $name= time() . $file->getClientOriginalName();
        $file->move('images',$name);
        $photo=Photo::create(['file'=>$name]);
        $input['photo_id']=$photo->id;


    }

    User::create($input);
    return redirect('admin/users');

}

also this is my front end table to display the image.

 <tr>

       <td><a href="{{route('admin.users.edit', $user->id)}}">{{$user->name}}</a></td>
       <td>{{$user->email}}</td>
       <td><img height='50' src="/images/{{ isset($user->photo)?$user->photo['file']:"has no photo"}}" alt=""></td>
       <td>{{$user->is_active = 1 ? 'Active':'Non-Active' }}</td>
       <td>{{$user->role['name'] }}</td>
       <td>{{$user->created_at->diffForHumans()}}</td>
       <td>{{$user->updated_at->diffForHumans()}}</td>
                            </tr>

I need image to drop in the images folder, persist in the database and display on the front end. Any one please?

Upvotes: 1

Views: 389

Answers (1)

Mahfuz Shishir
Mahfuz Shishir

Reputation: 841

Change this line

<td><img height='50' src="/images/{{ isset($user->photo)?$user->photo['file']:"has no photo"}}" alt=""></td>

And use

<td><img height='50' src="{{ url('images/' . \App\Photo::find($user->photo_id)->file) }}" alt=""></td>
//Include valid Photo.php location that you create. I just use demo...

Upvotes: 1

Related Questions