【问题标题】:Update Data in Laravel在 Laravel 中更新数据
【发布时间】:2018-06-30 08:03:38
【问题描述】:

这是我的代码:

路线:

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


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

控制器:

public function Update($id)
{

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

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

}

和视图:

            @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

但是当我想更新我的数据时,我收到一个错误:

http://localhost:8000/editposts/1

App\Http\Controllers\PostController::Update() 缺少参数 1

【问题讨论】:

    标签: laravel


    【解决方案1】:

    首先声明你的路线:

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

    然后更新您的表单网址:

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

    这是假设您的模型的 id 列是 id

    (可选)您也可以使用隐式模型绑定:

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

    【讨论】:

      【解决方案2】:

      修正路线,指定参数

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

      将 post'id 作为参数传递

        {{ 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() }}
      

      通知$post->id

      【讨论】:

      • RouteCollection.php 第 161 行中的 NotFoundHttpException:
      【解决方案3】:

      将您的发布路线更改为:

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

      完成!

      【讨论】:

      • RouteCollection.php 第 161 行中的 NotFoundHttpException:
      【解决方案4】:

      你需要改变路线:

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

      然后表格到:

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

      【讨论】:

      • RouteCollection.php 第 161 行中的 NotFoundHttpException:
      • @Mohammad 请发布您现在使用的确切路线和表格。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2015-04-16
      • 2021-10-02
      • 2016-03-07
      • 2021-10-03
      • 2017-04-04
      • 1970-01-01
      相关资源
      最近更新 更多