简体   繁体   English

从外键表 Laravel 中获取第一张图片

[英]Fetch first image from foreign key table Laravel

Reference: Fetch first image from foreign key table but this time in Laravel.参考: 从外键表中获取第一张图像,但这次是在 Laravel 中。

I started playing with Laravel and I want to take first image from every post and show these in my blade.我开始玩 Laravel,我想从每个帖子中获取第一张图片并在我的刀片中显示这些图片。

$secondpost = DB::table('posts')
        ->orderBy('id', 'desc')
        ->skip(1)
        ->take(8)
        ->get();

foreach ($secondpost as $spost)
$secondph = DB::table('post_images')
        ->select('filename')
        ->where('post_id', $spost->id)
        ->limit(1)
        ->get()
        ->pluck('filename');

return view ('pages.index', compact('firstp', 'fph', 'secondpost', 'secondph'));
<div class="row">
 @foreach ($secondpost as $secondp)
 <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 post-ab">
  <div class="row">
   @foreach ($secondph as $sph);
    <img src="{{url($sph)}}" class="imgpost" alt="">
   @endforeach
    <div class="bottomleft">
      <p class="ab-desc">{{$secondp->marca}} {{$secondp->model}}</p>
      <p class="ab-desc">{{$secondp->pret}} Eur</p>
    </div>
    </div>
   </div>
 @endforeach
</div>

This code load only one image for every img src.此代码仅为每个 img src 加载一张图像。 Post_id is foreign key for posts table id. Post_id 是posts 表id 的外键。

There are few things you need to confirm.您需要确认的事情很少。

  1. You have created models for the table like Post for posts table and PostImages for post_images table.您已经创建了模型样表Postposts表和PostImagespost_images表。 If not, follow the documentation.如果没有,请按照文档进行操作。 https://laravel.com/docs/5.8/eloquent https://laravel.com/docs/5.8/eloquent

  2. You have created the hasMany relationship in your post model with post_images .您已经使用post_images在您的post模型中创建了hasMany关系。 Like:喜欢:

In Post model.Post模型中。

/**
* Get the images for the post.
*/
public function images()
{
    return $this->hasMany('App\PostImage');
}
  1. Then change in your controller to use early loading, Like:然后更改您的控制器以使用早期加载,例如:

$secondpost = \App\Post::with("images")
        ->orderBy('id', 'desc')
        ->skip(1)
        ->take(8)
        ->get();

And now the view looks like:现在视图看起来像:

<div class="row">
 @foreach ($secondpost as $secondp)
 <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 post-ab">
  <div class="row">
    @php
        $image = $secondp->images->first();
    @endphp
    <img src="{{url($image->filename)}}" class="imgpost" alt="">
    <div class="bottomleft">
      <p class="ab-desc">{{$secondp->marca}} {{$secondp->model}}</p>
      <p class="ab-desc">{{$secondp->pret}} Eur</p>
    </div>
    </div>
   </div>
 @endforeach
</div>

Solution: 2解决方案:2

You can create another relationship with hasOne to get a single image like:您可以使用hasOne创建另一个关系以获得单个图像,例如:

// In your Post model.

/**
* Get the default image for post.
*/
public function default_image()
{
    return $this->hasOne('App\PostImage')->orderBy("id");
}

Make change in controller to use ->with("default_image") .更改控制器以使用->with("default_image")

And in view you can just use like:鉴于您可以使用如下:

    $secondp->default_image->filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM