Last active
September 5, 2017 20:36
-
-
Save vocolboy/f7eb8b9fbfe99945961c6dac28ff1f0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
建立一個新的 Provider | |
<?php | |
namespace App\Providers; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Support\ServiceProvider; | |
class MacroServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Boot the macro extend query for the application. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Builder::macro('like', function ($column, $value) { | |
$this->where($column, 'like', "%$value%"); | |
return $this; | |
}); | |
} | |
} | |
------- | |
去 config 設定檔註冊新建的 Provider | |
'provider' =>[ | |
... | |
\App\Providers\MacroServiceProvider::class | |
] | |
------- | |
然後就可以用 $query->like($column,$value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment