dqwh1201 2017-10-25 01:54
浏览 33
已采纳

无法从公用文件夹laravel打印出图像

I want to show the image that I had just uploaded and only show it to those who have uploaded it. For example, user table contain jack, Emily and John, so if jack were to upload a file the image will show directly under him, but I don't know how to do it?

This is how it look like now: enter image description here

Controller: (how I store the image)

public function store1(Request $request){

   $this->validate($request, [
        'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

   if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $userImage = new UserImage;
        $userImage->name = $name;
        $userImage->size = $size;
        //dd($userImage);
        $userImage->save();
}

view.blade.php

 @foreach ($data as $object)
    <b>Name: </b>{{ $object->name }}<br><br>
@endforeach

I saw people using this inside their blade.php, but I don't know what the $model is:

    <img src="{{ asset('public/images/' . $model->image) }}"> 

Upload.blade.php (this is my upload page where user will upload their image)

                        {{  csrf_field()  }}


<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
                <input type="file" name="file">

        </div>
    </div>

 <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
          </form>

enter image description here

  • 写回答

1条回答 默认 最新

  • dpdjpt5137 2017-10-25 02:06
    关注

    There are many ways to do it.

    You could either use Eloquent or using query builder of Laravel.

    In your controller you should get all the images that the user uploaded.

    Query builder approach :

    //don't forget the namespace
    `use DB;`
    
    //in your function write this.
    $images = DB::table('user_images')
            ->join('users', 'users.id', '=', 'user_images.user_id')
            ->where('users.id', '=', $id) 
            ->get();
    
    //use dd($images) to verify that the variable $images has data
    
    //send $images in your view
    

    in your view write a foreach loop like so:

    @foreach($images as $image)
        <img src="{{ asset('public/images/' . $image->name ) }}"> 
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”