4

I am attempting to make multiple queries in a scope function in laravel. my code is as follows. the first query executes normally but the second seems to be ignored. what is the correct way to do this?

public function scopeUpdateStatus($query,$oldUser, $newUser, $alias, $env) {

$query->where('db_conn_app_alias_user', $newUser)->where('db_conn_app_alias', $alias)->where('app_instance_environment', $env)->update(array('user_status' => 'active'));
$query->where('db_conn_app_alias_user', $oldUser)->where('db_conn_app_alias', $alias)->where('app_instance_environment', $env)->update(array('user_status' => 'passive'));

return "success";
}
7
  • do you have "success" returned ? Commented Feb 18, 2014 at 18:32
  • yes the first query executes just not the second, there is no issues accessing the function Commented Feb 18, 2014 at 18:33
  • hm,strange try to trace calls with debugger. Commented Feb 18, 2014 at 18:36
  • I changed the query to use DB::update and now it works....i wonder what the issue is here Commented Feb 18, 2014 at 18:38
  • great - I am thinking that returned result from update is messing something. Commented Feb 18, 2014 at 18:39

3 Answers 3

1

The trick here is to use with (a Laravel helper function) and clone.

function scopeSomeName($query) {
    with(clone $query)->whereStuff;
    with(clone $query)->whereOtherStuff;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This happens because you use the same $query variable in the two updates. You add where()s to the $query in the first update query and then run it, but when you add your where()s in the second update query the where()s from the first query are still there. Because of this your query will return zero result so there is nothing to update. Copy the $query first to a new variable then run the second query in the copied one:

public function scopeUpdateStatus($query, $oldUser, $newUser, $alias, $env) {
    $queryTemp = $query;

    $query->where('db_conn_app_alias_user', $newUser)
        ->where('db_conn_app_alias', $alias)
        ->where('app_instance_environment', $env)
        ->update(array('user_status' => 'active'));

    $queryTemp->where('db_conn_app_alias_user', $oldUser)
        ->where('db_conn_app_alias', $alias)
        ->where('app_instance_environment', $env)
        ->update(array('user_status' => 'passive'));

    return "success";
}

2 Comments

I attempted this, and it did not seem to work. I ended up just calling the function twice and passing in different parameters to set active or passive.
@arrowill12 I created a test environment that produced the same problem that you described: only the first update happened. After this change both update happened. Your DB::update works because of the same reason. It doesn't use the $query so it has a fresh start.
0

A minor edit (reduces overhead) to @merlinpatt answer. You don't need to clone the $query twice. Just once, as you already have the existing/original variable

function scopeSomeName($query) {
    $query_cloned = clone $query;
    $query->whereStuff;
    $query_cloned->whereOtherStuff;
}

Also, there's no need for the with() helper function. Tested it and works as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.