Trouble getting the image out from public folder with a one-to-one relationship

邮差的信 提交于 2019-12-11 15:21:02

问题


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.

Here are the codes:

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 (where I show the image)

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

回答1:


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.




回答2:


As I see, there is no relation between saved image name and the $currentUser->name
Can you show your code you use to save the image? There you have to save the image with current user name.
The error here: $image->move($destinationPath, $name);



来源:https://stackoverflow.com/questions/46950583/trouble-getting-the-image-out-from-public-folder-with-a-one-to-one-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!