【问题标题】:Image persisting in my database but not displaying on the frontend图像保留在我的数据库中但未显示在前端
【发布时间】:2018-04-21 06:36:22
【问题描述】:

我正在使用 Laravel 5.2 开发一个项目。我需要创建、编辑和显示用户资料。用户配置文件应该有一个上传的用户图像。下面是来自我的控制器的代码 sn-p。这是为了保存用户照片,将其放入一个名为 images 的文件中。照片保存在数据库中,不放在图片文件夹中,也不显示在前端。

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

}

这也是我显示图像的前端表。

 <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>

我需要将图像放入图像文件夹,保存在数据库中并显示在前端。请问有人吗?

【问题讨论】:

  • 上传个人资料图片后,图片会掉入图片文件夹??做检查吗?
  • 请检查您的图像是否正确存储在 images 文件夹中
  • 不,它没有。但它掉进了我的数据库中。
  • 是的,它在我的公用文件夹下,我只是检查了一下确定。
  • 公用文件夹的路径是否与您页面上生成的路径匹配?

标签: php database laravel laravel-5 laravel-eloquent


【解决方案1】:

改变这一行

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

并使用

<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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2021-12-18
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多