Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Query Augment

Let the example speak by itself.

use Illuminate\Support\Facades\DB;

// usual mysql insert
DB::table('testing')
    ->insert([
        'text' => 'foo',
        'number' => 50,
    ]);

$item = DB::table('testing')
    ->where('number', 50)
    ->addSelectCase(function ($testing) {
        if ($testing->text == 'foo') {
            return 'yay!';
        }
    }, 'value')
    ->first();

$item->value; // 'yay!'

How is this witchcraft working?

You probably noticed the addSelectCase method in the example above. Actually, the closure passed to addSelectCase method is not invoked. The package compiles the source code inside the closure into a MySQL CASE statement. It sounds crazy, but it works!

The transpiled code looks something like:

select
    (case when testing.text = 'foo' then 'yay!' end) as 'value'
from "testing"
where "number" = 50

Only a small subset of PHP is allowed inside the closure.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages