无法从公用文件夹laravel打印图像 [英] Trouble printing out image from public folder laravel
问题描述
我想显示我刚刚上传的图像,并且只显示给已上传图像的人.例如,用户表包含jack,Emily和John,因此,如果jack上传文件,图像将直接显示在他的下面,但是我不知道该怎么做?
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?
现在是这样的:
控制器:(我如何存储图像)
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
view.blade.php
@foreach ($data as $object)
<b>Name: </b>{{ $object->name }}<br><br>
@endforeach
我看到人们在他们的blade.php中使用它,但是我不知道$ model是什么:
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(这是我的上传页面,用户将在其中上传其图像)
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>
推荐答案
有很多方法可以做到这一点.
There are many ways to do it.
您可以使用Laravel的Eloquent
或query builder
.
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
方法:
//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
在您看来,编写如下的foreach循环:
in your view write a foreach loop like so:
@foreach($images as $image)
<img src="{{ asset('public/images/' . $image->name ) }}">
@endforeach
这篇关于无法从公用文件夹laravel打印图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!