php Laravel 5.4 crud 中的更新方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43614815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the update method in laravel 5.4 crud?
提问by Masum
i just trying to crud system. for that into controller store function my code is
我只是想 crud 系统。对于控制器存储功能,我的代码是
public function store(Request $request)
{
Article::create([
'user_id' => auth()->id(),
'content' => $request->content,
'live' => (boolean)$request->live,
'post_on' => $request->post_on
]);
return redirect('/articles');
}
it's enough to store data, but when i want to edit article & save again then what will be my edit function code? i have no idea. i trying same code into edit function & it create new article not update. so what will be right code for edit function? thanks
存储数据就足够了,但是当我想编辑文章并再次保存时,我的编辑功能代码是什么?我不知道。我在编辑功能中尝试相同的代码,它会创建新文章而不是更新。那么编辑功能的正确代码是什么?谢谢
回答by Alexey Mezenin
Resource controller method for update is update()
. Eloquent method for update()
is update()
too, so you can do this:
更新的资源控制器方法是update()
. Eloquent 方法update()
也是update()
如此,所以你可以这样做:
public function update(Request $request, $id)
{
Article::where('id', $id)->update($request->all());
return redirect('/articles');
}
You also can use the same controller and Eloquent method for both crerate and update data updateOrCreate()
method.
您还可以对创建和更新数据updateOrCreate()
方法使用相同的控制器和 Eloquent方法。
回答by Mortada Jafar
you can use
您可以使用
public function update(Request $request, $id)
{
$article = Article::find($id);
$article->fill($request->all());
}
sure you should add Your column attributes to $fillable array in Your model
确保您应该将您的列属性添加到您的模型中的 $fillable 数组中
protected $fillable = ['user_id', 'content', 'live'];
回答by Sami
You can also update it as object format like this.
您也可以像这样将其更新为对象格式。
public function update(Request $request, $id)
{
$article = Article::find($id);
$article->user_id = auth()->id();
$article->content = $request->content;
$article->live = (boolean)$request->live;
$article->post_on = $request->post_on;
$article->save();
}`
回答by Abu Sufian
public function update(Request $request, $id) {
Article::where('id', $id)->update($request->except(['_token']));
return redirect('/articles');
}
回答by user9913328
//route//
Route::any('/update/{id}', 'ProductController@update');
//controller//
public function update(Request $request, $id) {
$product = $request - > all();
Product::find($id) - > update($product);
return redirect('/product') - > with('message', 'Success', compact('product'));
}
回答by Mahmudul Hasan Shohag
If you auto-generate resource controller for a specific Model using php artisan make:model -a Artical
then you have the update()
function like below:
如果您使用自动生成特定模型的资源控制器,php artisan make:model -a Artical
那么您将拥有如下update()
功能:
public function update(Request $request, Article $article)
{
//
}
Here, Lavarel automatically fetch the Article
object into $article
. So, you can save the $request
data like below:
在这里,Lavarel 自动将Article
对象提取到$article
. 因此,您可以保存如下$request
数据:
public function update(Request $request, Article $article)
{
$article->update($request->all());
return redirect()->route('article.index'); // or your desired location :)
}
回答by rashedcs
public function update(Request $request, $id)
{
$info = array('user_id' =>$request->user()->id,
'content' => $request->content, 'live'=>$request->live);
DB::table('article')->where('id', $id)->update($info);
session()->flash('success', 'Update Successfully');
return redirect('/article');
}
回答by Adriano Rosa
First, you are having two actions right, the create and update, so for a real crud in laravel you might have these two in a separated logic methods the store()
and update()
:
首先,您有两个正确的操作,创建和更新,因此对于 laravel 中的真正 crud,您可能将这两个操作放在单独的逻辑方法 the store()
and 中update()
:
/**
* This is a resource create method which is a HTTP POST.
*/
public function store(Request $request) {
// create a new item in database
}
/**
* This is a resource update which is a HTTP PUT METHOD.
*/
public function update(Request $request, $id) {
// update the item in database
}
You set up your routes withPOST
for creating and PUT
to update then your doing a proper crud resource.
你设置你的路线以POST
创建和PUT
更新然后你做一个适当的 crud 资源。
I recommend you to separate the create logic off the update logic and if you have sort of unique data then you should validate its value before creating a new resource.
我建议您将创建逻辑与更新逻辑分开,如果您有某种独特的数据,那么您应该在创建新资源之前验证其值。