$query = User::query();
if ($request->has('age')) {
$query->where('age', $request->get('age'));
}
if ($request->has('experience')) {
$query->where('experience', $request->get('experience'));
}
$users = $query->get();
->when()
, но на мой вкус он только усложняет код. critical сгенерировал файл критических стилей, и потом вручную скопировать эти стилиЗачем копировать руками, если содержимое файла можно прочитать и добавить программно?
select('pages.id', 'pages.title', 'pages.slug', DB::raw('"page" as `type`'))
If you would like to continue using the original auto-prefixed controller routing, you can simply set the value of the $namespace property within your RouteServiceProvider and update the route registrations within the boot method to use the $namespace property
public function reviews(): HasManyThrough
{
return $this->hasManyThrough(
Review::class,
Comment::class,
'commentable_id',
'reviewable_id'
);
}
public function getCommentsCountAttribute(): int
{
return $this
->reviews
->where('commentable_type', Review::class)
->count();
}
whenLoaded
method may be used to conditionally load a relationship. In order to avoid unnecessarily loading relationships, this method accepts the name of the relationship instead of the relationship itself:use App\Http\Resources\PostResource;
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'posts' => PostResource::collection($this->whenLoaded('posts')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
Как я делаю, но ничего не получается.
foreach($roles as $cat => $role) { $categories = Category::with('roles')->find($cat); $data[] = $role; } $categories->roles()->sync($data);
$categories
, хотя она содержит ровно одну категорию, найденную по ID?$role
, хотя она содержит массив ролей, которые вы выше дампнули?$categories
работаете вне цикла, в котором она создаётся?foreach($request->category as $categoryId => $roles) {
Category::find($categoryId)->roles()->sync($roles);
}
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your route closure or controller method. The incoming request instance will automatically be injected by the Laravel service container