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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭