Skip to content

Instantly share code, notes, and snippets.

@vocolboy
Last active September 5, 2017 20:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vocolboy/f7eb8b9fbfe99945961c6dac28ff1f0b to your computer and use it in GitHub Desktop.
Save vocolboy/f7eb8b9fbfe99945961c6dac28ff1f0b to your computer and use it in GitHub Desktop.
建立一個新的 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