laravel 5.2图片无法加载

时间:2019-05-23 13:02:29

标签: php laravel

您好,我正在尝试学习laravel,当我观看本课程时,出现了有关图像上传和显示的讲座,我毫无问题地进行了上传,并创建了表格和关系,但是当我尝试显示图像时不会将图像加载到公用文件夹中名称为“ images”的文件夹中

这是显示表


 @if($users)

        @foreach($users as $user)
            <tr>
                <td>{{$user->id}}</td>
                <td><img height="50" src="/images/{{$user->photo ? $user->photo->file : 'no photo'}}" alt=""></td>
                <td>{{$user->name}}</td>
                <td>{{$user->email}}</td>
                <td>{{$user->role->name}}</td>
                <td>{{$user->is_active == 1 ? 'Active' : 'Not Active'}}</td>
                <td>{{$user->created_at->diffForHumans()}}</td>
                <td>{{$user->updated_at->diffForHumans()}}</td>
            </tr>

    @endforeach
        @endif

这是我的控制器


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

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

        //        return $request->all();
    }

这是我的模型,我这样做是为了使文件名显示可以是动态的,而不是在图像src中写入/ image /,但是仍然无法正常工作


class Photo extends Model
{
    //


    protected $uploads = '/images/';

    protected $fillable = ['file'];

    public function getFileAttribute($photo){

        return $this->uploads.$photo;

    }


}

1 个答案:

答案 0 :(得分:0)

将 index.blade.php 替换为以下内容:

 @if($users)

        @foreach($users as $user)



      <tr>
        <td>{{$user->id}}</td>
         <td><img height='50' src='{{$user->photo?$user->photo->file:"No image"}}'></td>

        <td>{{$user->name}}</td>
        <td>{{$user->email}}</td>
        <td>{{$user->role->name}}</td>
        <td>{{$user->is_active==1?'Active':'Not Active'}}</td>

        <td>{{$user->created_at->diffForhumans()}}</td>
        <td>{{$user->updated_at->diffForhumans()}}</td>
      </tr>

      @endforeach

    @endif

Photo.php 在这里:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{


protected $fillable=['file'];
    protected $uploads='../images/';

public function getFileAttribute($photo)
{

    return $this->uploads . $photo;
}

}

控制器在这里:

  public function store(UsersRequest $request)
    {

        $input=$request->all();

        if($file=$request->file('photo_id')){  //check if the user has submit photo from the form
           $name=time().$file->getClientOriginalName(); //append the time in the beginning to make image unique
           $file->move('images',$name); //move the image to the image folder in public dir
           $photo=photo::create(['file'=>$name]); //inserting the image in the photo table , file column
           $input['photo_id']=$photo->id; //getting its ID of the inserted record and storing it in the input array

        }
       $input['password']=bcrypt($request->password);// encrypting the password from the request variable , and storing it in the input array
        user::create($input); //inserting all the input array in the user's table which means photo_id as well above

        return redirect('/admin/users'); //returing the user to the display users

    }