我如何使用laravel 5.3更新公司信息。

时间:2017-03-06 18:16:32

标签: php laravel-5.3

我正在尝试更新公司信息。当我获得已保存的先前输入的编辑表单,并在编辑输入并单击更新按钮后,它不会显示任何消息,也不会更新任何信息。

这是edit.blade.php:

<form class="form-horizontal company-form" action="/company/edit" method="post">

    <input type="hidden" name="contact_id" value="{{$company->id}}">

    <div class="form-group">
        <label class="col-sm-3 control-label">Company Name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="name" value="{{$company->company_name}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Email</label>
        <div class="col-sm-9">
            <input type="email" class="form-control" name="email" value="{{$company->email}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Contact</label>
        <div class="col-sm-9">
            <input type="number" class="form-control" name="contact" value="{{$company->contact}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Address</label>
        <div class="col-sm-9">
            <textarea class="form-control" name="address"  rows="5">{{$company->address}}</textarea>
        </div>
    </div>

    @if(Session::has('message'))
        <div class="form-group">
            <label class="col-sm-3 control-label"></label>
            <div class="col-sm-9">
                <div class="alert alert-info" role="alert">{{Session::get('message')}}</div>
            </div>
        </div>
    @endif

    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <button type="submit" value="Save" class="btn btn-default">Update</button>
        </div>
    </div>
    {{ csrf_field() }}
</form>

这是控制器:

public function updateInfo(Request $request){
        $company = Company::where('id','=',$request->id);

        $data = [
            'company_name' => $request->name,
            'email' => $request->email,
            'contact' => $request->contact,
            'address' => $request->address
        ];

        if($company->update($data)){
            $request->session()->flash('message','Successfully Update Company!!');
                return redirect('/company/edit'.'/'.$request->id);
        }else{
            $request->session()->flash('message','Not Update Company!!');
            return redirect('/company/edit'.'/'.$request->id);
        }
    }

这是路线:

    Route::get('/company/edit/{id}','CompanyController@editCompany');
    Route::post('/company/edit/{id}','CompanyController@updateInfo');

1 个答案:

答案 0 :(得分:0)

您的更新路线需要ALTER TABLE tablename AUTO_INCREMENT = 1; 参数,但您的表单只是发布到id,因此它与您的任何路线都不匹配。

更改表单上的操作以包含ID。

/company/edit/