Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当一个对象与多个类别相关时,如何过滤查询?_Php_Laravel - Fatal编程技术网

Php 当一个对象与多个类别相关时,如何过滤查询?

Php 当一个对象与多个类别相关时,如何过滤查询?,php,laravel,Php,Laravel,我想获取与以下两个类别相关的电影结果: 例如“浪漫”和“戏剧”。。如果我说 $genre->with(“电影”)->where(“流派”、“浪漫”)->where(“流派”、“戏剧”)->get() 当然,这是行不通的,难道没有简单的方法吗 <!-- my controller --> public function show() { $genres = new genres; $y = $genre->w

我想获取与以下两个类别相关的电影结果: 例如“浪漫”和“戏剧”。。如果我说
$genre->with(“电影”)->where(“流派”、“浪漫”)->where(“流派”、“戏剧”)->get()
当然,这是行不通的,难道没有简单的方法吗

  <!-- my controller -->
   public function show()
      {
            $genres = new genres;
             $y = $genre->with("movie")->where("genre","Romance")->get();
             return $y;
       }
    <!-- model -->
   <?php

   namespace App;

   use Illuminate\Database\Eloquent\Model;
    use App\movies;
     class genres extends Model
       {
         public $table ="genres";
         public function movie(){
             return $this->belongsToMany(movies::class,
                   "relation","genre_id","movie_id");
                                 }
       }

公共活动展览(
{
$genres=新类型;
$y=$GREEP->with(“电影”)->where(“流派”、“浪漫”)->get();
返回$y;
}

使用或条件如下:

$genre->with("movie")->where("genre","Romance")->orWhere("genre","Drama") ->get();

这相当于
其中类型='浪漫'或类型='戏剧'
使用或条件如下:

$genre->with("movie")->where("genre","Romance")->orWhere("genre","Drama") ->get();
public function show()
{
    $search = ['Romance'];
    // Or
    $search = ['Action', 'Romance'];

    $genres = genres::with('movie')->whereIn('genre', $search)->get();
    return $genres;
}

这相当于
其中流派='浪漫'或流派='戏剧'

如果我想要共享这两个类别的结果呢??要做到这一点,我想我必须一次查询一个类别,然后尝试以某种方式过滤它……我指的是具有戏剧和浪漫类别的电影?如果我希望结果共享这两个类别呢??要做到这一点,我想我必须一次查询一个类别,然后尝试以某种方式过滤它……我的意思是那些有戏剧和浪漫类别的电影?使用
其中
可以解决问题在某种程度上说,它真的很有用谢谢:)使用
其中
可以解决问题在某种程度上,它真的很有用谢谢:)
public function show()
{
    $search = ['Romance'];
    // Or
    $search = ['Action', 'Romance'];

    $genres = genres::with('movie')->whereIn('genre', $search)->get();
    return $genres;
}