Przemek Wojtas Przemek Wojtas - 7 months ago 36
SQL Question

Updating junction table using laravel

Problem is simple I have a form that inserts data into Handymen table (everything except check boxes) and to Skill table (check boxes) however these are connected using junction table and when I click submit button, data is added to both tables however how do I make it update a junction table to add a new row, with skill_id and handyman_id just added?

function addhandyman()
{
return view('layouts/addhandyman');
}
function pushdetails(Request $request)
{
$handyman = new Handyman();
$handyman->first_name = $request->first_name;
$handyman->last_name = $request->last_name;
$handyman->street = $request->street;
$handyman->postcode = $request->postcode;
$handyman->town = $request->town;
$handyman->save();
$skill = new Skill();
$skill->skill = $request->skill;
$skill->save();
return redirect('addhandyman');
}





@section('content')
<h1>Add new Handyman</h1>
<form action="{{url('pushdetails')}}" method="POST">
{{ csrf_field() }}
<div>
<input type='text' name='first_name' placeholder='Enter First Name' />
<input type='text' name='last_name' placeholder='Enter Last Name' />
<input type='text' name='street' placeholder='Enter your Street' />
<input type='text' name='postcode' placeholder='Enter your postcode' />
<input type='text' name='town' placeholder='Enter your town' />
<label>Carpenter</label>
<input type='checkbox' name='skill' value='Carpenter' />
<label>Plumber</label>
<input type='checkbox' name='skill' value='Plumber' />
<label>Painter</label>
<input type='checkbox' name='skill' value='Painter' />
</div>
<input type="submit" name="submitBtn" value="Add new Handyman">
</form>
@endsection


If any other files/code is required please let me know. Need help badly! Thanks!

Answer

The cool thing about Laravel Models (Eloquent), when you create a new resource/model, it actually pulls the newly created Primary Key ID from your new resource in your database, into your model.

So when you do:

$model = new Model();
$model->field = "value";
$model->save();
// This will actually already have the Primary Key ID in it.
$mID = $model->id;

So the manual way would be to extract the separate ID's from the Models, and then add them into your table manually. Or you could setup relationships in your models using Eloquent's Relationships Builder.

So it would look something like this:

$h_id = $handyman->id;
$s_id = $skill->id;
DB::table('myJunctionTable')->insert(array('h_id' => $h_id, 's_id' => $s_id));

With your code added:

$handyman->first_name = $request->first_name;
$handyman->last_name = $request->last_name;
$handyman->street = $request->street;
$handyman->postcode = $request->postcode;
$handyman->town = $request->town;
$handyman->save();

$skill = new Skill();
$skill->skill = $request->skill;
$skill->save();

$h_id = $handyman->id;
$s_id = $skill->id;
DB::table('myJunctionTable')->insert(array('h_id' => $h_id, 's_id' => $s_id));

return redirect('addhandyman');
Comments