i have 1 form and i want to insert it in 2 table
this is my form
<form action="{{route('dealers.store')}}" method="post"> @csrf <input class="text email" type="text" name="name" placeholder="Full Name" required=""> <input class="text email" type="text" name="name_of_firm" placeholder="Firm Name" required=""> <input class="text email" type="text" name="number" placeholder="Mobile Number" required=""> <input class="text email" type="text" name="address" placeholder="Address" required=""> <input class="text email" type="email" name="email" placeholder="Email" required=""> <input class="text" type="password" name="password" placeholder="Password" required=""> <input class="text w3lpass" type="password" name="password" placeholder="Confirm Password" required=""> {{-- <div class="wthree-text"> <label class="anim"> <input type="checkbox" class="checkbox" required=""> <span>I Agree To The Terms & Conditions</span> </label> <div class="clear"> </div> </div> --}} <input type="submit" value="Submit" name="submit"> </form>
this is my DealerController store function where i seperate the data to insert in 2 different table
public function store(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required', 'password' => 'required', 'name_of_firm' => 'required', 'address' => 'required', 'number' => 'required', ]); $user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('price'), 'password' => $request->input('detail'), ]); $dealer = Dealer::create([ 'name_of_firm' => $request->input('name_of_firm'), 'address' => $request->input('address'), 'number' => $request->input('number'), ]); return redirect()->route('admin/charts')->withSuccess('Done'); }
this is my Dealer model where is a relation with user table
use HasFactory; protected $table = 'dealers'; public function user() { return $this->belongsTo(User::class); }
and this is my User model it is package of jetstream
public function dealer() { return $this->hasMany(Dealer::class); }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
In your controller you’re saving the info from the User like this:
$user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('price'), 'password' => $request->input('detail'), ]);
But there is a request there that makes no sense. In the email you are trying to save the info from an input with the name “PRICE”, but that input doesn’t exist on your form. The same case applies to the password. That’s the source of the error, your are tying to save a null value (since price doesn’t exist on the request) to the email column. Try this:
use IlluminateSupportFacadesHash; [....] $user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('email'), 'password' => Hash::make($request->input('password')), ]);
Note how on the password I added Hash. This encrypts the password to store it securely on your database. If you want to just save the text without that type of security refrain from using Hash.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0