Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Narares's avatar

How to custom the RegisterController to save multiple data when user selected

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.

  1. User to save the registered users
  2. Categories to show in the front-end that I provided for them to choose (multiple)
  3. 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.

0 likes
2 replies
Rarepyre's avatar

maybe this one?

protected function create(array $data){
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'tel' => $data['tel'],
            'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
        ]);

	$vendor_cat = VendorCat::create([
            'category_id' => $data['category'],
            'vendor_id' => $user->id
        ]);

	return $user;
}

NB: Dont forget to add import for VendorCat model

use App\VendorCat;

And make sure that you've add 'category_id' & 'vendor_id' to protected $fillable inside VendorCat.php model

protected $fillable = [
	'category_id',
	'vendor_id'
];
Narares's avatar

Thank you for your answer, sir.

I still got this error


Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\whoservice\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 884

This is my front-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

Please or to participate in this conversation.