Mohammad
Mohammad

Reputation: 85

Update Data in Laravel

This is my code :

Route:

Route::get('/editposts/{id}', function ($id) {
    $showpost = Posts::where('id', $id)->get();
    return view('editposts', compact('showpost'));
});


Route::post('/editposts', array('uses'=>'PostController@Update'));

Controller :

public function Update($id)
{

    $Posts = Posts::find($id);
    $Posts->Title = 10;
    $Posts->Content = 10;
    $Posts->save();

    //return Redirect()->back(); Input::get('Title')

}

and View:

            @foreach($showpost as $showpost)

            <h1>Edit Posts :</h1>

            {{ Form::open(array('url'=>'editposts', 'method'=>'post')) }}
            Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{ Form::submit('Update') }}
            {{ Form::close() }}

            @endforeach

but when I want to Update my data i receive an error :

http://localhost:8000/editposts/1

Missing argument 1 for App\Http\Controllers\PostController::Update()

Upvotes: 1

Views: 422

Answers (4)

apokryfos
apokryfos

Reputation: 40700

First declare your route:

Route::post('/editposts/{id}', array('uses'=>'PostController@Update'));

Then update your form url:

{{ Form::open(['url' => url()->action('PostController@Update', [ "id" => $showpost->id ]), 'method'=>'post']) }}

This is assuming your model's id column is id

(Optional) You can also use implicit model binding :

public function Update(Posts $id) {
    //No need to find it Laravel will do that 
    $id->Title = 10;
    $id->Content = 10;
    $id->save();
 }

Upvotes: 0

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

Correct the route,specify a parameter

    Route::post('editposts/{id}', 'PostController@Update');

Pass the post'id as paramater

  {{ Form::open(array('url'=>'editposts/'.$post->id, 'method'=>'post')) }}

        Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{ 

        Form::submit('Update') }}
   {{ Form::close() }}

Notice $post->id

Upvotes: 0

Max Ahmed
Max Ahmed

Reputation: 135

Change your post route to:

Route::post('/editposts/{id}', 'PostController@Update');

Done!

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

You need to change route:

Route::post('editposts/{id}', 'PostController@Update');

Then the form to:

{{ Form::open(['url' => 'editposts/' . $showpost->id, 'method'=>'post']) }}

Upvotes: 2

Related Questions