I have a form in which a user can enter different data, the form contains two parts.
First part data will be saved to a table named pages
(works perfect), the second part data will be saved to the table named parameters
the table parameters
contain a column which holds repeated data-name(not working),
Here is how table should look.
I have created a PIVOT table
for paramaters
and prebids
table like this.
prebid_parameter,
public function up()
{
Schema::create('prebid_parameter', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('prebid_id')->unsigned();
$table->foreign('prebid_id')->references('id')->on('prebids');
$table->bigInteger('parameter_id')->unsigned();
$table->foreign('parameter_id')->references('id')->on('parameters');
});
}
And page_prebid like this.
public function up()
{
Schema::create('page_prebid', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('page_id')->unsigned();
$table->foreign('page_id')->references('id')->on('pages');
$table->bigInteger('prebid_id')->unsigned();
$table->foreign('prebid_id')->references('id')->on('prebids');
});
}
And I have created relationships like this.
Page model.
class Page extends Model
{
protected $fillable =[
"title",
"articles",
"status"
];
public function prebids(){
return $this->belongsToMany('App\Prebid');
}
}
Prebid model.
class Prebid extends Model
{
protected $fillable =["bidders_name"];
public function parameters(){
return $this->belongsToMany('App\Parameter');
}
public function pages(){
return $this->belongsToMany('App\Page');
}
}
And Parameter Model looks like this.
class Parameter extends Model
{
protected $fillable =[
"params_name",
"params_value",
"bidders_name"
];
public function prebids(){
return $this->belongsToMany('App\Prebid');
}
}
And finanlly I have page controller store function to save the data like this.
public function store(Request $request)
{
$page = Page::create([
'title' => $request->get('title'),
'articles' => $request->get('articles'),
'status' => $request->get('status'),
]);
$page->save();
$page->tags()->sync($request->tags, false);
$page->prebids()->sync($request->prebids, false);
return redirect("/pages")->with("sucess", "data saved");
}
When I add dd($request)
in-store function I get the following
Note: parameter and prebid controllers are just empty
Now when I click the submit button only the first part data is saved to database, the rest are not saved to the database.
Here is repo: demo
What am I doing wrong with my code? am new to laravel though.
Couple of things are jumping out. I don't see an actual relationship for tags
in the model you have displayed. Do you have a relationship on Page for tags
?
Also, this line:
$page->prebids()->sync($request->prebids, false);
is looking for the input from request for prebids
but I'm not seeing that in your request object. I see params_value
, which would not be sent to the sync() method. And thus, not be synced.
Give this a try:
$page->prebids()->sync($request->params_value, false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.