无法显示上传到laravel文件夹中的图像

时间:2016-06-10 17:43:14

标签: laravel

这是AdminUsersController方法!!!!

,

}

通过此图像上传到/ public / images

这是用于在index.blade.php中显示的代码

) VALUES (NULL, \'test\', \''.$password.'\', \'test\')';

但图片未显示在网页中我不知道为什么!请帮帮我这个

我的路线代码:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UsersRequest;
use App\Photo;
use App\Role;
use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;

  class AdminUsersController extends Controller
{

public function index()
{
    //
   $users = User::all();



    return view('admin.users.index', compact('users'));
}


public function create()
{
    //

    $roles = Role::lists('name','id')->all();

    return view('admin.users.create', compact('roles'));

}


public function store(UsersRequest $request)
{
    //

    $input = $request->all();


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

        $name = time() . $file->getClientOriginalName();

        $file->move('images', $name);

        $photo = Photo::create(['file'=> $name]);

        $input['photo_id'] = $photo->id;

    }

    $input['password'] = bcrypt($request->password);
    User::create($input);

   return redirect('/admin/users');




}


public function show($id)
{
    //

    return view('admin.users.show');
}


public function edit($id)
{
    //

    return view('admin.users.edit');
}


public function update(Request $request, $id)
{
    //
}


public function destroy($id)
{
    //
}

这是我在index.blade.php中的代码

 <img src="/images/{{$user->photo ? $user->photo->file : 'no user photo'}}" alt="">

this is my output in which i cannot display photos but photo is uploaded into /public/images folder

1 个答案:

答案 0 :(得分:0)

首先要显示带有laravel的图片,您需要在{{asset(..)}}中使用<img/> ...示例:

<img src="{{asset('...')}}" alt="">

然后,您需要使用public_path()功能将示例存储在公共文件夹中

$imageName = $user->id.'.'.'jpg';
        $request->file('img')->move(
            public_path() . '/img/', $imageName
        );

现在要在公共路径或存储路径中显示用户的照片,例如:

<img src="{{asset('/img/user->photo')}}" alt="">

我希望能帮到你