LARAVEL 对编辑和插入操作使用单一表单

LARAVEL use single form for both Edit and Insert actions

在我的应用程序中,我想简化表单并更改 Form::model 以同时使用更新和插入,因为具有此功能,我正在创建此 route:controller 以显示视图并修改它:

Route::controller(
    'customers' , 'customersController',
    array(
        'getIndex'  =>'customers.index',
        'postUpdate'=>'customers.update'
    )
);

customersController 控制器 class:

<?php
class customersController extends \BaseController
{
    public function getIndex()
    {
        if ( Auth::check() ){
            $customers = new Customers;
            return View::make('layouts.customers')->with('customers', $customers);
        }
        return Redirect::route('dashboard');
    }
    public function postUpdate($id)
    {
        print_r( $id);
        die;
    }
}

?>

getIndex 我可以 return 正确地查看 customers.blade.php 并且我可以创建新变量作为 new Customers,因为我是从以下表格创建的从客户创建了新实例:

{{ Form::model($customers,array('route' => array('customers.update', $customers->id))) }}
...
{{ Form::submit('UPDATE', array('class'=>'btn btn-default btn-default-small') ) }}
{{ Form::close() }}

现在我想将表单值发送到控制器,但是在发送之后我得到这个错误:

错误:

 Missing argument 1 for customersController::postUpdate() 

视图中的表单必须与此代码类似:

{{ Form::model($customers,array('route' => array('customers.update', $customers->id))) }}

你的 Form::text 必须是这样的:

{{ Form::text('name', $customers->name, array('class'=>'form-control rtl' ) ) }}

路线:

Route::controller(
    'customers', 'customersController',
    array(
        'getIndex' => 'customers.index',
        'postUpdate' => 'customers.update'
    )
);

现在在控制器中,您可以尝试使用此代码来检测表单是更新还是插入

public function postUpdate()
{
    if (Input::get('id')) {
        $customer = Customers::find(Input::get('id'));
    } else {
        $customer = new Customers;
    }
    ...
    ...
    ...
}