Dear all,
I am a newbie in Laravel and I need your help.
I'm using the Laravel 7.
I have 3 tables in my database.
- User to save the registered users
- Categories to show in the front-end that I provided for them to choose (multiple)
- Vendor_Cat that I collecting the user_id and category_id after the user registered
Here are my Models
public function users(){
return $this->BelongsToMany(User::class);
}
public function categories(){
return $this->BelongsToMany(Categories::class);
}
And this is the Laravel RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'tel' => $data['tel'],
'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
]);
My Fron-End Code
@if($categories->count()>0)
<div class="form-row">
<label class="form-row-inner">
<select class="selectpicker col-sm-12 md-12" id="selectpicker" name="categories[]"
multiple data-live-search="true" data-selected-text-format="count" data-count-selected-text="Selected Categories ({0})" title="Choose your categories" aria-expanded="false" aria-haspopup="true" required>
@foreach ($categories as $categories)
<option class="com-sm-4 md-4" aria-expanded="false" value="{{$categories->id}}"
@if (isset($user))
@if($user->hasCat($catgories->id))
selected
@endif
@endif
><strong>{{Str::limit($categories->name,51)}}</strong></option>
@endforeach
</select>
@error('category_id')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
@endif
If I want to save the category Ids that I use @foreach to show in the Register Form after the users
selected. What should I do?
Here is the explanation image
https://ibb.co/vQpfxxH
Thank you,
p.s. sorry for my bad English.