$users = User::with(['categories' => function ($query) {
$query->withCount(['posts' => function ($q) {
$q->whereColumn('posts.user_id', 'user_categories.user_id');
}]);
}])->get();
$category = Category::whereSlug($slug)
->firstOrFail();
$translations = $category->translations()
->with('team')
->paginate(15);
return view('translation.index', compact('category', 'translations'));
// В модели Post
public function activeComments()
{
return $this->commnets()->ofActive();
}
public function inactiveComments()
{
return $this->commnets()->ofInactive();
}
// в контроллере/классе
$relations = ['activeComments', 'inactiveComments'];
Post::with($relations)
->withCount($relations)
->get();
public function toArray($request)
{
return [
'id' => $this->id,
'status' => $this->status,
'client_email' => $this->client_email,
'partner' => new PartnerResource($this->partner)
];
}
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
// отношение
public function roles()
{
return $this->belongsToMany('App\Models\Role', 'user_role');
}
// проверка
public function hasRole($id)
{
return $this->roles()
->where('roles.id', $id)
->exists();
}
// для наглядности
public function isAdmin()
{
return $this->hasRole(Role::ID__ADMIN);
}
// 1 это ид роли
const ID__ADMIN = 1;
if ($user->isAdmin()) {
// code...
}
->action(Lang::get('Reset Password'), url(config('app.url').route('password.reset', ['en', 'token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
User::where('gender', $gender)
public function getFullNameAttribute()
{
return $this->name.' '.$this->lastname;
}
public function passes($attribute, $value)
{
// проверка на совпадение расширения файла с svg
return $value->getClientOriginalExtension() === File::EXTENSION__SVG;
}
public function isAdmin()
{
return $this->admin === 1;
}
@if (Auth::user()->isAdmin())
показать кнопку
@endif