【问题标题】:Trying to display image using (img src) tag, the path of the image is saved in my database尝试使用(img src)标签显示图像,图像的路径保存在我的数据库中
【发布时间】:2019-09-02 21:16:53
【问题描述】:

我已成功将用户上传的图片保存到“广告”表中名为“图片”的字段中。但在我的视图 (blade.php) 文件上显示图像时出现问题。

我尝试在网上搜索解决方案,但没有一个对我有用...

下面是我的 AdvertisementsController 的 store 方法:-

public function store(Request $request)
{
    // $media = Media::all();

    request()->validate([
        'image' => 'required',
        'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    //find current admin's id
    $admin_user = auth()->id();

    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $image_name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $folderPath = public_path('/uploads/images');
        $imagePath = $folderPath. "/".  $image_name;
        $image->move($folderPath, $image_name);
    }

    $title = request('title');
    $description = request('description');
    $sort_num = request('sort_num');

    $media = Media::create([
        'title' => $title,
        'description' => $description,
        'sort' => $sort_num,
        'created_by' => $admin_user,
    ]);

    Advertisement::create([
        'image' => $imagePath,
        'expired_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'media_id' => $media->id,
    ]);

    // return back()->with('success', 'Your advertisement has been successfully created!');
    return redirect('/advertisements');

}

我可以成功保存路径但是保存的路径很长,例如:“/Users/hannzern/Desktop/numericledger-backend/public/uploads/images/klang-river.jpeg”并认为路径将保存为“/public/uploads/images/klang-river.jpeg”。会不会是这个问题?

下面是我的视图代码(blade.php):-

<h1>Advertisements</h1>

<ul>
    @foreach ($advertisements as $advertisement)
        <a href="/advertisements/{{$advertisement->id}}">
            {{ $advertisement->media->title }}   
        </a>
        <br>

        //Three methods of retrieving the image from the path but all three not working
        <img src="{{ $advertisement->image }}" height="300" width="200">

        <img src="/Users/hannzern/Desktop/numericledger-backend/public/uploads/images/klang-river.jpeg" height="300" width="200">

        <img src="/public/uploads/images/klang-river.jpeg" height="300" width="200">

        <br>
        <br>
        <br>
        <br>
    @endforeach

</ul>

<a href="/advertisements/create">Add new advertisement</a>

另外,从文件目录中,我可以在我的 /public/uploads/images 文件夹中看到“klang-river.jpeg”图像...有人知道为什么吗? :(

【问题讨论】:

    标签: image laravel-5.8


    【解决方案1】:

    /public 将通过在浏览器的链接中使用/ 来解析,因此您的链接应保存到数据库中:

    /Users/hannzern/Desktop/numericledger-backend/uploads/images/klang-river.jpeg
    

    但是,为了确保无论您在哪里托管应用程序都可以正常工作,您不应保存绝对路径。仅将以下内容保存到数据库中:

    klang-river.jpeg
    

    然后,使用 asset() 帮助器,您可以在模板中调用正确的 url:

    <img src="{{ asset('uploads/images/' . $advertisement->image) }}" height="300" width="200">
    

    这将解析为预期的网址。我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 2014-12-10
      • 2014-02-15
      相关资源
      最近更新 更多