Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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 Laravel雄辩地通过多个栏目获取相关内容_Php_Laravel - Fatal编程技术网

Php Laravel雄辩地通过多个栏目获取相关内容

Php Laravel雄辩地通过多个栏目获取相关内容,php,laravel,Php,Laravel,在我的应用程序中,我试图创建一个相关事件部分,其中显示与特定事件相关的所有事件 我有这个问题 public function show($id) { // The current event $event = Event::find($id); // Find related, upcoming events $tags = $event->tags; $relatedEvents = Event::where('startDate', '>

在我的应用程序中,我试图创建一个相关事件部分,其中显示与特定事件相关的所有事件

我有这个问题

public function show($id)
{
    // The current event
    $event = Event::find($id);

    // Find related, upcoming events
    $tags = $event->tags;

    $relatedEvents = Event::where('startDate', '>=', Carbon::now())
    ->where(function ($query) use ($event) {
        $query->where('hostedBy', $event->hostedBy);
        $query->orWhere('type', $event->hostedBy);
        $query->orWhere('category', $event->hostedBy);
        $query->orWhere('sector', $event->hostedBy);
    })
    ->toSql();

    dd($relatedEvents);
}
以下哪种输出SQL

“从
事件
中选择*,其中
开始日期
=?和(
=?或
类型
=?或
类别
=?或
扇区
=?)”

然后我尝试了这个

/**
 * Display the specified resource.
 *
 * @param  \App\Event  $event
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    // The current event
    $event = Event::find($id);

    // Find related, upcoming events
    $tags = $event->tags;

    $relatedEvents = Event::where('startDate', '>=', Carbon::now())
    ->where(function ($query) use ($event) {
        $query->where('hostedBy', $event->hostedBy);
        $query->orWhere('type', $event->hostedBy);
        $query->orWhere('category', $event->hostedBy);
        $query->orWhere('sector', $event->hostedBy);
        $query->orWhereHas('tags', function ($query) use ($tags) {
            $query->where('slug', $tags->slug);
        });
    })
    ->toSql();

    dd($relatedEvents);
}
但是通过这次尝试,我得到了未定义的变量
$tags

可以有许多嵌套查询吗

这是因为我需要检查字段,但也需要比较给定给事件的标记

另外,如果当前文章根本没有标签,我该怎么办

更新

public function show($id)
{
    // The current event
    $event = Event::find($id);

    // Find related, upcoming events
    $tags = $event->tags;

    $relatedEvents = Event::where('startDate', '>=', Carbon::now())
    ->where(function ($query) use ($event, $tags) {
        $query->where('hostedBy', $event->hostedBy);
        $query->orWhere('type', $event->type);
        $query->orWhere('category', $event->category);
        $query->orWhere('sector', $event->sector);
        $query->orWhereHas('tags', function ($query) use ($tags) {
            foreach ($tags as $tag) {
                $query->where('slug', $tag->slug);
            }
        });
    })
    ->orderBy('startDate', 'dsc')
    ->toSql();

    dd($relatedEvents);
}

我需要对此进行测试,但由于可能有多个标记,所以我循环遍历了它们。

从第一个位置传递
标记

$relatedEvents = Event::where('startDate', '>=', Carbon::now())
    ->where(function ($query) use ($event, $tags) { // add tags here
        $query->where('hostedBy', $event->hostedBy);
        $query->orWhere('type', $event->hostedBy);
        $query->orWhere('category', $event->hostedBy);
        $query->orWhere('sector', $event->hostedBy);
        $query->orWhereHas('tags', function ($query) use ($tags) {
            $query->where('slug', $tags->slug);
        });
    })
    ->toSql();

试试这个。您需要在首次使用时传递$tags变量

 $relatedEvents = Event::where('startDate', '>=', Carbon::now())
    ->where(function ($query) use ($event, $tags) {
        $query->where('hostedBy', $event->hostedBy);
        $query->orWhere('type', $event->hostedBy);
        $query->orWhere('category', $event->hostedBy);
        $query->orWhere('sector', $event->hostedBy);
        $query->orWhereHas('tags', function ($query) use ($tags) {
            $query->where('slug', $tags->slug);
        });
    })
    ->toSql();
您在事件和标记之间定义了什么样的关系。我认为它有很多,所以在这种情况下,您应该使用$q->where('slug',$tags->slug)

否则

或wherehas(“标签”);足够了。

尝试使用->with('tags')作为第一个查询。
$query->orWhereHas('tags', function ($q) use ($tags) {
        $q->where('slug', $tags->slug);
    });