简体   繁体   English

Laravel 4雄辩的模型链接了附加的where子句

[英]Laravel 4 eloquent models chaining additional where clauses

I Have the following code (member is just a standard Eloquent model) 我有以下代码(成员只是一个标准的口才模型)

$members = new Member;
$members->where('user_id', '=', 5);
$_members = $members->get();

The last query run produces " SELECT * from members ", so it seems to be ignoring my where clause, what am I doing wrong here? 上一次查询运行会产生“ SELECT * from members ”,所以它似乎忽略了我的where子句,我在这里做什么错的?

By the way I know I could do $members = new Member::where(...) etc... but I will be adding the where clauses in a loop in order to create filtering on the results from the database. 顺便说一句,我知道我可以做$members = new Member::where(...)等...,但是我将在循环中添加where子句,以便对数据库结果进行过滤。

UPDATE 更新

The only way around this seems to be to add a where that will catch all on initialization such as: 解决此问题的唯一方法似乎是添加一个可以捕获所有初始化的位置,例如:

 $members = Member::where('member_id', '<>', 0);
 $members->where('user_id', '=', 5);
 $_members = $members->get();

But this seems quite a bit of a hack. 但这似乎是一个很大的漏洞。 I am not trying to do anything complicated so I cant be the only one who has had this problem? 我没有尝试做任何复杂的事情,所以我不能成为唯一遇到此问题的人吗?

FIXED MAYBE 已修正

For anyone who has stumbled here I have fixed this by using: 对于任何在这里跌跌撞撞的人,我已使用以下方法修复了此问题:

$members =  Member::query();
$members->where('user_id', '=', 5);
$_members = $members->get();

Not sure if that is the correct way but it works for me and doesn't appear like a hack. 不知道这是否是正确的方法,但是它对我有用,并且看起来并不像hack。

I don't believe Eloquent works like that. 我不相信Eloquent这样的作品。

Try this... 尝试这个...

$members = new Member;
$members = $members->where('user_id', '=', 5);
$members = $members->get();

There is a much better way to achieve what you need here using query scopes . 有一种更好的方法可以使用查询范围来实现您所需要的功能。 Here is what you need to do. 这是您需要做的。

In your Member.php model do the following: 在您的Member.php模型中执行以下操作:

public function scopeMembers($query, $condition1, $condition2)
{
    if ( ! empty($condition1))
    {
        $query = $query->where('column1', $condition1);
    }

    if ( ! empty($condition2))
    {
        // Use if ( ! empty($condition2[0]) { $query->whereIn('column2', $condition2); } if you are exploding the input in the controller.
        $query = $query->where('column2', $condition2);
    }
}

In your controller do this: 在您的控制器中执行以下操作:

protected $member;

public function __construct(Member $member)
{
    $this->member = $member;
}

public function getMembers()
{
    $condition1 = Input::get('condition1');
    $condition2 = Input::get('condition2');

    $members = $this->member->members($condition1, $condition2)->paginate(10);

    return View::make('members', compact('members'));
}

This is a very basic example that can be expanded upon depending on what you need. 这是一个非常基本的示例,可以根据需要进行扩展。 As you can see you can pass as many conditions as you require to the query scope. 如您所见,您可以根据需要将尽可能多的条件传递给查询范围。

$query = Member::where('user_id', 5);

if ( $someCondition )
{
    $query->where(....);
}

$members = $query->get();

您不必调用find()而不是get()吗?

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

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