我有一个数据库,像这样将图像存储在表行中 名称(字符串)“名称” sometext(string)“一些描述” 文件名(字符串)image1.jpeg | image2.jpeg
image1和image2存储在“ public / storage / image”文件夹中\ images保存在文件夹中
public function store(Request $request)
{
$data = request()->validate([
'name' => 'required',
'sometext' => '',
'testImages' => '',
'testImages' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$images=array();
if($files=$request->file('testImages')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
auth()->user()->tests()->create([
'name' => $data['name'],
'sometext' => $data['sometext'],
'filenames' => implode("|",$images),
]);
}
public function show ($id)
{
$tests = Tests::findorfail($id); //get row data from table with ID
return view('test.show-report', compact('tests')); //returing data to blade
}
在刀片中,我可以执行$ tests->名称和$ tests->描述来成功获取数据。.我无法弄清楚如何从“文件名”刀片中显示图像。
答案 0 :(得分:0)
尝试这样
<img src="/storage/image/{{ $tests['filenames'] }}" height="30px" width="30px" />
希望对您有所帮助:
在这种情况下:
在控制器中,使用explode()来$ tests->文件名创建一个数组,然后像这样循环它
@foreach(json_decode($tests->filenames, true) as $images)
<img src="/storage/image/{{ $images }}" height="30px" width="30px" />
@endforeach
答案 1 :(得分:0)
如果图像名称正确地以正确的扩展名存储在数据库中,则这是刀片视图中需要的:
<img src="/storage/image/{{$tests->NAME_OF_COLUMN_IN_DB}}" />
只需将“ NAME_OF_COLUMN_IN_DB”替换为在数据库中存储图像名称的列名称。
答案 2 :(得分:0)
像这样使用
<img src="{{ url('') }}/storage/image/{{$tests-> filenames}}">
答案 3 :(得分:0)
您可以这样做:
@foreach(explode('|',$tests->filenames) as $fileName)
<img src="/storage/image/{{$fileName}}"/>
@endforeach
答案 4 :(得分:0)
在你的模型中写一个这样的函数。假设您在用户模型中。
public function getImage()
{
return isset($this->image) && $this->image ? asset('userPhoto/'.$this->image): asset("dist/img/user2-160x160.jpg");
}
现在像这样检索刀片文件中的图像-
<a href="{{asset('userPhoto/'.$user->image)}}"target="_blank">
<img src="{{ $user->getImage() }}"width="50"></a>
答案 5 :(得分:-1)
您可以按照以下步骤
<img src="{{ public_path('storage\image') }}{{ $tests->filenames }}" alt=""/>
使用以下代码分隔图像的名称
$img = exploade('|', $tests->filenames);
dump($img);