I have been struggling with this for a while, am a new developer with very little experience. So am working on a project dealing with drugs and diseases. There exists a many to many relationship.
Drug model
public $timestamps=false;
public function disease()
{
return $this->belongsTo('App\Disease');
}
Disease model
public $timestamps=false;
public function drug()
{
return $this->belongsTo('App\Drug');
}
I want to create relationships in the Disease_Drug pivot table using forms. form.blade.php
<label>Disease</label>
<select class="form-inline input-sm " name="disease" id="disease">
@foreach($diseases as $key => $disease)
<option value=""> </option>
@endforeach
</select>
<label>Drugs</label><br>
@foreach($drugs as $key=>$drug)
<input type="hidden" name="drug[]" value="0" />
<input class="checkbox-inline" type="checkbox" name="drug[]"value="" id=""> <br>
@endforeach
<button type="submit" class="btn btn-primary">Submit</button>
I have a disease_drug controller
public function form()
{
$diseases = Disease::all();
$drugs = Drug::all();
return view('admin.form')
->with('diseases', $diseases)
->with('drugs', $drugs);
}
public function store(Request $request)
{
$diseases = $request->get('diseases.ids');
$drugs = $request->get('drugs.id', []); // Empty array by default if no checkbox checked.
$diseases->drugs()->sync($request->input('drugs', []));
}
I am unable to save the results in the database. I am really clueless on this so kindly help me out.
Aucun commentaire:
Enregistrer un commentaire