Laravel 4 where like 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17431818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:04:10  来源:igfitidea点击:

Laravel 4 where like clause

mysqllaravellaravel-4eloquent

提问by harishannam

    public function getIndex()
        {


// Get all the blog posts
        /*$posts = Post::with(array(
            'author' => function($query)
            {
                $query->withTrashed();
            },
        ))->orderBy('created_at', 'DESC')->paginate(10);*/


        $posts =Post::with(array('search' => function($query)
        {
            $query->where('title', 'like', '%Lorem ipsum%')->withTrashed();
        }))->orderBy('created_at', 'DESC')->paginate(10);


        // Show the page
        return View::make('frontend/blog/index', compact('posts'));

    }

This is my code in Controller. I am using a starter bundle available on GitHub.

这是我在控制器中的代码。我正在使用 GitHub 上提供的入门包。

I created this model for this controller

我为这个控制器创建了这个模型

public function search()
{
    return $this->belongsTo('Post', 'user_id');
}

Problem is it is not taking the results where title contains "Lorem ipsum". It just prints all the values from the table.

问题是它没有获取标题包含“Lorem ipsum”的结果。它只是打印表中的所有值。

How can i implement this to get only the values that contain my Tag/Keyword. I am doing this to add search option to the laravel site

我如何实现它以仅获取包含我的标签/关键字的值。我这样做是为了向 Laravel 站点添加搜索选项

回答by Vit Kos

Why not create a scope for this? Have you read scopes docs? Here is an example how would I have achieved that:

为什么不为此创建一个范围?你读过范围文档吗?这是一个示例,我将如何实现这一目标:

The scope:

范围:

public function scopeTagged($query, $tag)
{
    return $query->where('title', 'LIKE', '%' . $tag . '%');
}

And modification to your action:

并修改您的操作:

public function getIndex()

{

    $posts = Post::tagged($lorem_ipsum)->withTrashed()->orderBy('created_at', 'DESC')->paginate(10);

    // Show the page
    return View::make('frontend/blog/index', compact('posts'));

}

回答by Kylie

Try this...

尝试这个...

    $posts =Post::with(array('search' => function($query)
    {
        $query->raw_where("title LIKE '%Lorem ipsum%")->withTrashed();
    }))->orderBy('created_at', 'DESC')->paginate(10);

Or something along these lines...

或者类似的东西......

$search being your input

$search 是您的输入

 Post::raw_where("match (`title`) against (?)", array($search))
    ->orderBy('created_at', 'DESC')->paginate(10);

EDIT

编辑

What about this?

那这个呢?

 Post::where(DB::raw('MATCH(`title`)'),'AGAINST', DB::raw('("+'.implode(' +',$search).'" IN BOOLEAN MODE)->orderBy('created_at', 'DESC')->paginate(10);

回答by deepika jain

I think it was an issue with the Laravel Eager loading Constraints implementations:

我认为这是 Laravel Eager 加载约束实现的一个问题:

For ref:-

参考:-

https://github.com/laravel/laravel/pull/1069

https://github.com/laravel/laravel/pull/946