存储功能不将数据保存到数据库 [英] store function not saving data to the database
问题描述
我有一个表单,用户可以在其中输入不同的数据,该表单包含两个部分.
I have a form in which a user can enter different data, the form contains two parts.
第一部分数据将保存到名为pages
的表中(正常工作),第二部分数据将保存到名为parameters
的表中.表parameters
包含一列,其中包含重复的数据名称(不起作用),
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),
这是表格的外观.
我已经像这样为paramaters
和prebids
表创建了PIVOT table
.
I have created a PIVOT table
for paramaters
and prebids
table like this.
prebid_parameter,
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');
});
}
像这样的page_prebid.
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.
页面模型.
class Page extends Model
{
protected $fillable =[
"title",
"articles",
"status"
];
public function prebids(){
return $this->belongsToMany('App\Prebid');
}
}
预设模型.
class Prebid extends Model
{
protected $fillable =["bidders_name"];
public function parameters(){
return $this->belongsToMany('App\Parameter');
}
public function pages(){
return $this->belongsToMany('App\Page');
}
}
参数模型如下所示.
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");
}
当我添加dd($request)
店内功能时,我得到以下信息
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.
这是回购协议:演示
我的代码在做什么错? laravel是新手.
What am I doing wrong with my code? am new to laravel though.
推荐答案
事物的耦合正在跳出来.在您显示的模型中,我没有看到tags
的实际关系.您在
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
?
此外,这一行:
$page->prebids()->sync($request->prebids, false);
正在从prebids
的请求中寻找输入,但是在您的请求对象中没有看到该输入.我看到了params_value
,它不会发送到sync()方法.
因此,不被同步.
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.
尝试一下:
$page->prebids()->sync($request->params_value, false);
这篇关于存储功能不将数据保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!