$input = $request->get('category', []);
foreach (Category::all() as $category) {
$category->roles()->sync(Arr::get($input, $category->id, []));
}
<input type="hidden" name="category[42][11]" value="0" class="form-check-input" />
<input type="checkbox" name="category[42][11]" value="1" class="form-check-input" />
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
componentDidMount() {
this.setState({
x: 1
y: 2
z: 1-2
})
}
"~^(?:[A-Z]{4}-){3}[A-Z]{4}$~"