Reputation: 151
I have to insert multiple rows into the database table.
I have in database two tables, the first-named projets and the second named projets_castiong.
So the first input fields in the form should be inserted into projets table and the second input field is a dynamic input and all the added input fields should be inserted into projets-casting table.
so I'm using the following view and the following script which add dynamically input fields.
$(document).ready(function() {
$("body").on("click", ".add_new_frm_field_btn", function() {
var random = 1 + Math.floor(Math.random() * 1000); //generate random values..
var index = $(".form_field_outer").find(".form_field_outer_row").length + 1;
$(".form_field_outer").append(
`<div class="col-12 outer" data-index="${index}_${random}">
<div class="card-body form_field_outer_row">
<div class="form-row"><div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select id="id_casting" class="form-control" name="id_casting">
<option selected>Choose...</option>
@foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
@endforeach
</select>
</div>
</div></div></div> `);
})
// Ajax for store data into databse
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#submit').click(function(){
$.ajax({
url:"{{route('ajout_projet.store')}}",
method:"POST",
data:$('#projetform').serialize(),
type:'json',
});
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<form method="POST" id="projetform" class="myForms" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" id="id_projet" name="id_projet" />
<div class="form-group">
<label for="inputAddress">Numéro de projet</label>
<input type="text" class="form-control" id="numero_projet" name="numero_projet" placeholder="Description">
</div>
<div class="card mb-4 form_field_outer ">
<div class="card-body form_field_outer_row outer" data-index="0">
<input type="hidden" id="id_projet_casting" name="id_projet_casting" />
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select id="id_casting" class="form-control" name="id_casting">
<option selected>Choose...</option>
@foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
@endforeach
</select>
</div>
</div>
</div>
</div>
</form>
<button type="button" class="btn btn-outline-primary mb-1 add_new_frm_field_btn">Ajouter un nouveau casting</button>
<button type="submit" name="submit" id="submit" class="btn btn-primary mb-1">Add</button>
And I'm using the following controller :
public function store(Request $request)
{
if(request()->ajax())
{
$rules = array(
'numero_projet' => 'required');
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json(['errors' => $error->errors()->all()]);
}
$projets = new Projet();
$projets->numero_projet = $request['numero_projet'];
$projets->save();
$projets_castings = new Projet_Casting();
$projets_castings->id_projet = $projets->id;
$projets_castings->id_casting = $request['id_casting'];
$projets_castings->id_contrat = 1;
$projets_castings->save();
}
}
so when I execute mon code I get no error, and the data is inserted into the table: projets, but in table projets-casting, only one input field value is inserted and the id-projet champs get the value null, while I'm trying to insert multiple rows in the table according to the input fields added and the project-id inserted into table project should be inserted like a foreign key into table projets-casting.
How can I solve that? if you have any idea I will be grathfull.
Upvotes: 0
Views: 260
Reputation: 8178
There are a lot of possibilities, but the place to start is to look at your Project_Casting model. Have you correctly set up the mass assignment variables?
In that model, you should have (at least):
public $fillable = [
'id_projet',
'id_casting',
'id_contrat,
// Any other fields to populate that you want to allow
];
It might be helpful to use the Laravel create()
convention on your controller. Example:
$projets_castings = Projet_Casting::create(array(
'id_projet' => $projets->id,
'id_casting' => $request->input('id_casting'),
'id_contrat' => 1
));
Are you sure you have a $projets->id
at that point in the controller? IE maybe add some error checking after creating the $project
object just in case it isn't creating an object with an id.
Also - is Project_Casting connected to the right table? I know you said one field is saving, but with the name not quite adhering to Laravel convention (ProjectCasting) something might be messed up. Try testing by adding the direct stipulation of the table name on the model:
protected $table = '*whatever your table name is';
HTH
Upvotes: 0