2 回答
TA贡献1802条经验 获得超6个赞
您不需要加载整个模型来保存文章,您只需要一个 id,因此:
$article->country_id = country::where('name',$request->input('country'))->pluck('id')->first(); $article->category_id = category::where('name',$request->input('category'))->pluck('id')->first();
最后
$article->save();
TA贡献1826条经验 获得超6个赞
SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值
上述错误意味着每当您将数据插入数据库时,您都没有传递country_id的值和数据库搜索默认值,并且您没有在数据库表中为country_id分配默认值。
试试这个,我在保存文章之前已经为country_id和category_id分配了值
public function store(Request $request)
{
$article = new article(
[
'title' => $request->input('title'),
'top_content' => $request->input('top_content'),
'quote' => $request->input('quote'),
'left_content' => $request->input('left_content'),
'right_content' => $request->input('right_content'),
]
);
if ($request->hasFile('article_slider_image')) {
$file = time() . '_' . $request->file('article_slider_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleSliderImages';
$request->file('article_slider_image')->move($destination, $file);
$article->article_slider_image = $file;
}
if ($request->hasFile('left_image')) {
$file = time() . '_' . $request->file('left_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleLeftImages';
$request->file('left_image')->move($destination, $file);
$article->left_image = $file;
}
if ($request->hasFile('right_image')) {
$file = time() . '_' . $request->file('right_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleRightImages';
$request->file('right_image')->move($destination, $file);
$article->right_image = $file;
}
$country = country::where('name',$request->input('country'))->first();
$category = category::where('name',$request->input('category'))->first();
$article->country_id = $country->id;
$article->category_id = $category->id;
$article->save();
return redirect()->route('article.index')->with('success', 'article created successfully' . $request->title);
}
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报