无法从具有一对一关系的 public 文件夹中取出图像
Trouble getting the image out from public folder with a one-to-one relationship
我在从数据库中的 public 文件夹中获取图像时遇到一些问题,我有 UserImage
table 和 personal_info
table 和它们以一对一的关系连接在一起。我试过使用它,但它只是 return 我的空白结果。
代码如下:
家庭控制器:
public function getInfo($id) {
$data = personal_info::where('id', $id)->get();
$data3 = UserImage::where('user_id', $id)->get();
return view('test',compact('data', 'data3'));
test.blade.php (我显示图片的地方)
@foreach ($data as $object)
<b>Name: </b>{{ $object->Name }}<br><br>
@foreach ($data3 as $currentUser)
<img src="{{ asset('public/images/' . $currentUser->name ) }}">
@endforeach
@if ($data3->count())
@foreach ($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id' => $currentUser->user_id]) !!}">
<button class="btn btn-primary">
<i class ="fa fa-plus"></i>Upload Images
</button>
</a>
@endforeach
@else
<a href="{!! route('user.upload.image', ['id' => $object->id]) !!}">
<button class="btn btn-primary">
<i class ="fa fa-plus"></i>Upload Images
</button>
@endif
@endforeach
创建控制器:
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();
$id = $request->user_id;
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$userImage = new UserImage;
$personal_info = new personal_info;
$userImage->name = $name;
$userImage->size = $size;
$user = personal_info::find($id);
$user->UserImages()->save($userImage);
}
return redirect('/home');
}
据我所知,保存的图像名称与 $currentUser->name 之间没有关系
你能展示你用来保存图像的代码吗?在那里你必须用当前用户名保存图像。
这里的错误:$image->move($destinationPath, $name);
由于您没有将资产保存到存储中,而是保存到 public 目录中,因此您可以使用 url()
method instead. If you need to stick with asset()
you need to create a symlink from the storage directory to the public directory which is explained here,并使用存储目录来保存资产。
我在从数据库中的 public 文件夹中获取图像时遇到一些问题,我有 UserImage
table 和 personal_info
table 和它们以一对一的关系连接在一起。我试过使用它,但它只是 return 我的空白结果。
代码如下:
家庭控制器:
public function getInfo($id) {
$data = personal_info::where('id', $id)->get();
$data3 = UserImage::where('user_id', $id)->get();
return view('test',compact('data', 'data3'));
test.blade.php (我显示图片的地方)
@foreach ($data as $object)
<b>Name: </b>{{ $object->Name }}<br><br>
@foreach ($data3 as $currentUser)
<img src="{{ asset('public/images/' . $currentUser->name ) }}">
@endforeach
@if ($data3->count())
@foreach ($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id' => $currentUser->user_id]) !!}">
<button class="btn btn-primary">
<i class ="fa fa-plus"></i>Upload Images
</button>
</a>
@endforeach
@else
<a href="{!! route('user.upload.image', ['id' => $object->id]) !!}">
<button class="btn btn-primary">
<i class ="fa fa-plus"></i>Upload Images
</button>
@endif
@endforeach
创建控制器:
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();
$id = $request->user_id;
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$userImage = new UserImage;
$personal_info = new personal_info;
$userImage->name = $name;
$userImage->size = $size;
$user = personal_info::find($id);
$user->UserImages()->save($userImage);
}
return redirect('/home');
}
据我所知,保存的图像名称与 $currentUser->name 之间没有关系
你能展示你用来保存图像的代码吗?在那里你必须用当前用户名保存图像。
这里的错误:$image->move($destinationPath, $name);
由于您没有将资产保存到存储中,而是保存到 public 目录中,因此您可以使用 url()
method instead. If you need to stick with asset()
you need to create a symlink from the storage directory to the public directory which is explained here,并使用存储目录来保存资产。