I have a simple form for adding pages information, the form looks like this
as you can see above the prebid input, To add prebids we need to click plus button and pop up appear inside a popo up, I have a form which contains the following inputs
1.bidder_name
2.params_name
3.params_value
So now when the user saves the prebids inputs from pop up , data are saved to the table named parameters
and then the user clicks add page
button and the data are saved to the table named pages
,
I would like to link up the added page with added parameters(prebids inputs).
Here is what I have tried so far
Page model
class Page extends Model
{
protected $fillable =[
"title",
"articles",
"status"
];
public function parameter(){
return $this->belongsToMany('AppParameter');
}
}
Parameter model
public function pages(){
return $this->belongsToMany('AppPages');
}
Here is pivot table
public function up()
{
Schema::create('page_parameter', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('page_id')->unsigned();
$table->foreign('page_id')->references('id')->on('pages');
$table->bigInteger('parameter_id')->unsigned();
$table->foreign('parameter_id')->references('id')->on('parameters');
});
}
Here is hwo it look in phpmyadmin
Here is store function to save prebids inputs(parameters) inside parameter controller
public function store(Request $request){
// dd($request);
if($request->ajax())
{
$rules = array(
'params_name.*' => 'required',
'params_value.*' => 'required',
'bidders_name.*' => 'required'
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json([
'error' => $error->errors()->all()
]);
}
$params_name = $request->params_name;
$params_value =$request->params_value;
$bidders_name =$request->bidders_name;
for($count = 0; $count < count($params_name); $count++)
{
$data = array(
'params_name' => $params_name[$count],
'params_value' => $params_value[$count],
'bidders_name' => $bidders_name[$count],
);
$insert_data[] = $data;
// dd($insert_data);
}
Parameter::insert($insert_data);
return response()->json([
'success' => 'Data Added successfully.'
]);
}
}
Now when user save data to database, I can see data saved to pages table and parameter table but the pivot table is empty.
I am so new to laravel,
Now when I save the data to the database, the page_parameter table
(pivot table) is empty;
What am I doing wrong with my codes?
2
Answers
First thing you need to create page item:
after that