Reputation: 159
I m new at Laravel, I want to add data in two tables using single form. My table and their relation are
I want to add all the fields for user then select the class_id and then section_id through dropdown.
And then register and I want that after data is registered . The Registration table should have user_id of that new user registered and selected class and section id.
Fields for Student Table
<form method="POST" action="{{url('/storeStudent')}}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<input id="role_id" type="hidden" name="role_id" value="2">
Fields Starting for Registration Table
<select class="" name="class_id">
<option value="null">Class</option>
@foreach($cid as $cc)
@if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{{$counterr++}}
@else
<option value="{{$cc->id}}">{{$cc->title}}</option>
@endif
@endforeach
</select>
<select section="" name="section_id">
<option value="null">Section</option>
@foreach($sec as $sc)
@if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
@else
<option value="{{$sc->id}}">{{$sc->title}}</option>
@endif
@endforeach
</select>
</div>
</div>
</form>
Registration Controller
protected function create(array $data)
{
return Registration::create([
'reg_id' => $data['reg_id'],
'user_id' => $data['user_id'],
'class_id' => $data['class_id'],
'section_id' => $data['section_id'],
]);
}
public function store(Request $request)
{
registration::create($request->all());
return back();
}
Store Student
public function storeStudent(Request $request)
{
$user=new User();
$user->name=$request->name;
$user->password=Hash::make($request->password);
$user->email=$request->email;
$user->role_id=2;
$user->parent_id=$request->parent_id;
$user->save();
return back();
}
Now I want that same user_id which is going to be registered should also be saved in Registration Table
I have used simple store method in controller for saving data.
Upvotes: 0
Views: 110
Reputation: 1807
It's easy..your should get created User and use it's id...something like this:
Student Controller:
public function storeStudent(Request $request)
{
$created_user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'parent_id' => $request->parent_id,
'role_id' => $request->role_id,
]);
//And after that you need to store user_id in Registration table , so:
Registration::create([
'user_id' => $created_user->id,
'class_id' => $request->class_id,
'section_id' => $request->section_id,
]);
return redirect()->back();
}
hope it helps
Upvotes: 1
Reputation: 84
Please refer to this link. You might get some ideas regarding relationship in laravel. https://laravel.com/docs/5.8/eloquent-relationships
If this does not help then I need to see your controller and model. Please paste your controller and model code.
Upvotes: 0