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
public function lastMessage()
{
return $this->hasOne('App\Models\Message','chat_id', 'id')->latest();
}
$chats = Chat::with('lastMessage')->get();
@foreach ($chats as $chat)
{{ $chat->lastMessage->name }}
@endforeach
public function tags()
{
return $this->belongsToMany('как в доках');
}
$user = User::with('tags.posts')
->find(Auth::id());
@foreach ($user->tags as $tag)
@foreach ($tag->posts as $post)
{{ $post->name }}
@endforeach
@endforeach
public function scopeOfUser($query, $userId)
{
return $query->whereHas('users', function($query) use($userId) {
$query->where('user_tag.user_id',$userId);
});
}
$posts = Post::with('tags')
->whereHas('tags', function($query) use($userId) {
$query->ofUser($userId);
})->get();