mardi 20 novembre 2018

Change collection to query builder in laravel

I have a function with return type Builder

For some specific case, i need to change the value of collection (which i'll get from builder object) and return updated value as builder object.

Is there any way to convert a collection back to it's builder object?

Here is what i want to achieve in simpler form:

$users = DB::table('users')->where('is_human',1);    // Builder obj

$isAlien = true; 

$users = $users->get()->map(function($user) use ($isAlien) {
   $user->is_human = $isAlien? 0 : 1;
   return $user;
});

Now, the problem is when i execute this code, it will return a collection.

But i want to return $users as Builder Object.

I have searched through the laravel's API document to find a solution.

Please share your knowledge.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire