【问题标题】:how to display video per category in laravel eloquent如何在 laravel eloquent 中显示每个类别的视频
【发布时间】:2020-11-22 04:59:03
【问题描述】:

模特视频

protected $table = 'lokit_video';


protected $fillable = 
[
    'title',
    'cover_img',
    'trailer',
    'url',
    'order_',
    'active',
    'description',
    'lokit_category_id',
    'duration'
];
public function lokit_category(): BelongsTo
{
    return $this->belongsTo(Category::class);
}

型号类别

protected $table = 'lokit_category';
    protected $fillable = ['name'];

在控制器中

public function index(){
       $dataCategory = Category::all();
        $dataVideo = Video::all();
        $video = Video::where('lokit_category_id', $dataCategory)->get();
        dd($video);
        return View('bnpt.content.home',compact('dataCategory','dataVideo'));
    }

当我尝试上面的代码时,代码为空,如何解决?

【问题讨论】:

  • 您想显示某个类别的所有视频?
  • 是的,就像那样,但是我写代码很困惑
  • You want to display all the videos for a certain category? 你同意了。所以你想获取哪个分类的视频,请注明分类
  • 我想显示某个 lokit_category_id 的视频

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

如果你想得到所有类别相关的所有视频,你应该发展关系。

Category 模特:

    public function videos()
    {
        return $this->hasMany(Video::class);
    }

在控制器方法中:

$dataCategory = Category::all();
return View('bnpt.content.home',compact('dataCategory'));

在视图文件中:

<ul>
@foreach($dataCategory as $category)
    <li><span>{{$category->name}}</span>
        <ul>
           @foreach($category->videos as $video)
                   <li><span>{{$video->name}}</span></li>
           @endforeach
        </ul>
    </li>
@endforeach
</ul>

【讨论】:

    【解决方案2】:

    [问题]在您的代码中

    $dataCategory = Category::all();
    
    $video = Video::where('lokit_category_id', $dataCategory)->get();
    

    $dataCategory 是您的 lokit_category 表中所有条目的模型实例集合

    [解决方案] 在 where 语句中,您无法将 collection 与需要 id 的字段进行比较。

    lokit_category_id与类别的 id 进行比较

    如果您有 lokit_category_id

    $videos = Video::where('lokit_category_id', $lokit_category_id)->get();
    dd($videos);
    

    如果您有 lokit_category 的名称,请使用该名称获取 id,然后进行查询。

    // Get the category for which you want to get all the videos.
    $categoryName = 'CATGEORY NAME';
    
    $category = Category::where('name', $categoryName)->first();
    
    $videos = Video::where('lokit_category_id', $category->id)->get();
    dd($videos);
    

    [如果您想要某个类别的视频]

    在您的类别模型中

    public function videos(){
       return $this->hasMany(App\Video::class, 'lokit_catgeory_id', 'id');
    }
    

    控制器

    使用即时加载,因为它有助于减少查询次数。

    
    $categories = Category::with('videos')->get();
    
    foreach($categories as $category){
        // You can access the videos for the category using the videos relation.
        $category->videos;
    }
    

    【讨论】:

    • 当我调试 dd ($ category) 时内容为空
    • 您是否为 $categoryName 指定了您想要获取视频的类别名称?由于您的帖子标题表明您想要获取给定类别的视频,现在看到您的类别模型,我假设您会知道您想要视频的类别的名称。因此,将 $categoryName 分配给您需要视频的 categoryName。
    • 您可以将 categoryName 作为参数传递给控制器​​中的函数,这样您就可以传递 categoryName 并获取该类别的视频。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多