Laravel:仅获取单击的图像以显示,而不是全部

发布于 2025-01-20 02:31:59 字数 2143 浏览 0 评论 0原文

大家哟!

我实际上正在做一个学习 laravel 的 lil' 项目,这是我努力实现的目标:我希望我点击的图像显示在视图中,但不是我的所有图像(我的图像存储在数据库)。

我认为代码非常简单,我将与您分享:

我使用的控制器:

<?php

namespace App\Http\Controllers;

use App\Models\Images;
use Illuminate\Support\Facades\DB;

class ShowAdults extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $query = DB::table('images')->where('type', 'F');

        $images = $query->orWhere('type', 'M')->get();
    
        return view('showimages.chiens',compact('images'));
    }

     /**
     * Display the specified resource.
     *
     * @param  \App\Models\Images  $images
     * @return \Illuminate\Http\Response
     */
    public function show(Images $images)
    {
        $images = DB::table('images')->select('*')->get();
        
        return view('showimages.displaydog',compact('images'));
    }

    
}

以及显示图像的视图部分:

@foreach($images as $image)
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    {{ $image->name }}
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Details:</strong>
                    {{ $image->detail }}
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Image:</strong>
                    <img src="/image/{{ $image->image }}" width="500px">
                </div>
            </div>
        </div>
@endforeach

现在,我知道这个逻辑应该显示我存储在我的DB,但是假设我像这样分享它,因为我尝试过的所有其他方法......混乱哈哈。

你们知道我如何更改循环中的逻辑,以便当我单击图像时它显示我单击的图像而不是所有存储的图像吗?

非常感谢您的时间,提前抱歉,因为即使对我来说,经过几个小时的搜索后我找不到它似乎很愚蠢,我想我需要知道如何更好地搜索东西^^'。

Yo everyone!

I'm actually working on a lil' project to learn laravel and here is the thing I struggle to achieve : I want the image I click on to be displayed in a view, but not all of my images (my images are stored in a database).

I think the code is pretty simple, I'll share it with you :

the controller I use:

<?php

namespace App\Http\Controllers;

use App\Models\Images;
use Illuminate\Support\Facades\DB;

class ShowAdults extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $query = DB::table('images')->where('type', 'F');

        $images = $query->orWhere('type', 'M')->get();
    
        return view('showimages.chiens',compact('images'));
    }

     /**
     * Display the specified resource.
     *
     * @param  \App\Models\Images  $images
     * @return \Illuminate\Http\Response
     */
    public function show(Images $images)
    {
        $images = DB::table('images')->select('*')->get();
        
        return view('showimages.displaydog',compact('images'));
    }

    
}

And the part of the view that's displaying the images:

@foreach($images as $image)
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    {{ $image->name }}
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Details:</strong>
                    {{ $image->detail }}
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Image:</strong>
                    <img src="/image/{{ $image->image }}" width="500px">
                </div>
            </div>
        </div>
@endforeach

Now, I know this logic is supposed to display all of the imgaes I stored in my DB, but let's say I'm sharing it like that because all the others methods that I tried where... chaotic lol.

Do you guys know how I can change the logic in my loop so when I click on my image it displays the one I clicked instead of all the stored images?

Thanks a lot for your time, and sorry in advance cause even to me, it seems dumb that I can't find it after hours of searching, I think I need to know how to search things better ^^'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2025-01-27 02:31:59

好吧,我所要做的就是将链接放入图像,如下所示:

<a href="/displaydog/{{ $image->id }}" ><img src="/image/{{ $image->image }}" width="140px"></a>

我认为我必须在控制器和/或路线中做一些事情,但我只需修改图像本身链接中的路线。

再次感谢您对我的帮助,祝大家有美好的一天!

编辑:在我看来摆脱了 foreach 循环,让它像那样工作

Ok so all I had to do was to put the links to the images like that :

<a href="/displaydog/{{ $image->id }}" ><img src="/image/{{ $image->image }}" width="140px"></a>

I thought I had to do something in the controller and/or the route but I just had to modify the route in the link to the image itself.

Thanks again for helping me, have a nice day everyone!

Edit : got rid of the foreach loop in my view for it to work like that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文