what is the update method in laravel 5.4 crud?
Asked Answered
T

8

6

i just trying to crud system. for that into controller store function my code is

 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

Titan answered 25/4, 2017 at 15:21 Comment(1)
Welcome to StackOverflow! We will be glad to help you if you get stuck on a specific programming problem, but we are not here to write free code for you. Please see How do I ask a good question? and What topics can I ask about here?.Nibelung
E
11

Resource controller method for update is update(). Eloquent method for update() is update() too, so you can do this:

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.

Enteric answered 25/4, 2017 at 15:23 Comment(5)
@Titan you're using completely different code from the code I've shown. fill() method doesn't update data, it's just fills out attributes. Use $article->save(); after fill() or use update() as I've shown.Enteric
it shwos this errors SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update articles set _method = PUT, _token = l1ZyYVMAQJCAU6mzVXFEYqt2hUSQyAkqws9804aj, content = sadfasdf, post_on = 2017-04-25T03:43, Create_Post = Submit, updated_at = 2017-04-25 15:48:20 where id = 13)Titan
by fill method it dont shows any errors but it does not updated dataTitan
@Titan show guarded and fillable arrays from the Article model, please. Or just do this ->fill($request->except(['_method', '_token']))Enteric
Thanks all . i got solution & that is $article = Article::find($id); $article->update($request->all()); thank to every one for support meTitan
B
4

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();
}`
Bedivere answered 25/4, 2017 at 16:4 Comment(0)
I
2
//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'));
}
Indiscrete answered 8/6, 2018 at 9:40 Comment(2)
Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!Mesotron
It's better if there is some explanation in your answer about what code you wrote.Bergmann
G
1

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

 protected $fillable = ['user_id', 'content', 'live'];
Goodnight answered 25/4, 2017 at 15:27 Comment(0)
C
1
public function update(Request $request, $id) {
    Article::where('id', $id)->update($request->except(['_token']));
    return redirect('/articles');
}
Cogitation answered 15/6, 2017 at 5:58 Comment(0)
K
1

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:

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:

public function update(Request $request, Article $article)
{
    $article->update($request->all());

    return redirect()->route('article.index'); // or your desired location :)
}
Kendra answered 15/6, 2018 at 19:31 Comment(0)
B
1
    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');
    }
Baptize answered 14/2, 2019 at 16:28 Comment(0)
P
0

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():

/**
 * 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.

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.

Pyromagnetic answered 25/4, 2017 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.