namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
trait Multitenantable {
protected static function bootMultitenantable()
{
// name convention bootXYZ() means Laravel will automatically launch this method when trait is used. You can call it a trait “constructor”.
if (auth()->check()) {
static::creating(function ($model) {
$model->created_by_user_id = auth()->id();
});
// We’ve included Eloquent/Builder class here, and then used static::addGlobalScope() to filter any query with current logged in user. except admin
if (auth()->user()->role_id != 1) {
static::addGlobalScope('created_by_user_id', function (Builder $builder) {
$builder->where('created_by_user_id', auth()->id());
});
}
}
}
}
view()->composer('*', function (View $view) {
//logic goes here
});
public function handle($request, Closure $next)
{
URL::defaults(['locale' => $request->user()->locale]);
return $next($request);
}
protected $touches = ['blog'];
// helpful to update the parent’s timestamp when the child is updated
public function blog()
{
return $this->belongsTo(App\Blog::class);
}
$appends = [
'full_name',
'blogs:id,title'
];
public function blogs() {
return $this->hasMany(App\Blog::class);
}
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
// User::popular()->get()
$user = User::first();
$user->name = "Peter";
$user->phone->number = '1234567890';
$user->push(); // This will update both user and phone record in DB
// Clone a model
$user = App\User::find(1);
$newUser = $user->replicate();$newUser->save();
$inUsa = collect($hosts)
->where('location', 'USA')
->when(request('retired'), function($collection) {
return $collection->reject(function($employee){
return $employee['is_active'];
});
});
@includeWhen($post->hasComments(), 'posts.comments');
$article->increment('read_count');
https://youtu.be/nCiNqboYFVQ