无法以一对一的关系从公用文件夹中取出图像 [英] Trouble getting the image out from public folder with a one-to-one relationship

查看:103
本文介绍了无法以一对一的关系从公用文件夹中取出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从公用文件夹中获取图像时遇到了一些麻烦,在数据库中,我有UserImage表和personal_info表,它们以一对一的关系连接在一起.我已经尝试过使用它,但它只返回空白结果.

I am having some trouble getting the image from the public folder where in the database, I have UserImage table and personal_info table and they are connected together in a one to one relationship. I have tried using this but it just return me blank results.

以下是代码:

HomeController:

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

Create1Controller:

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');
}

推荐答案

由于您没有将资产保存到存储中,也不保存到公共目录中,因此可以使用 asset() ,则需要从以下位置创建符号链接此处中说明的存储目录到公用目录,并使用该存储目录保存您的资产.

Since you are not saving your assets into storage and saving into your public directory you could use the 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, and use the storage directory to save your assets.

这篇关于无法以一对一的关系从公用文件夹中取出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆