尝试softdelete,我还不明白代码有什么问题。每当我尝试删除。它没有,它把页面扔给我。
这是删除前确认页面的控制器:
public function deleteView($id)
{
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
这是删除前要确认的视图:
@extends('layouts.member')
@section('content')
{!! Form::open(array('action'=> array('CompanyController@destroy', $company->id, '_method'=>'delete'))) !!}
{!! Form::token() !!}
<p>{!! $company->companyname !!}</p>
<p>{!! $company->companyaddress !!}</p>
</p>{!! $company->country !!}</p>
<p>{!! $company->state !!}</p>
<p>{!! $company->email !!}</p>
<p>{!! $company->phone !!}</p>
<p>{!! $company->website !!}</p>
<p>{!! $company->companytype !!}</p>
{!! Form::hidden('company_id', $company->id) !!}
<p>{!! Form::submit('DELETE') !!}</p>
{!! Form::close() !!}
@stop
这是实现删除的控制器:
public function destroy(CompanyRequest $companyRequest)
{
$company_id = $companyRequest->company_id;
if(!$this->companyBelongsToUser($company_id))
{
return redirect()->route('companyindex', $company_id)->with('message', 'Sorry, yuo cannot delete this company');
}else
{
$company = Company::findOrFail($company_id);
$company->delete();
return redirect()->route('companyindex', $company_id)->with('message', 'Company deleted successfully');
}
}
这是删除前返回确认页面的路线:
Route::get('delete/{id}', array('as'=>'deleteView', 'uses'=>'CompanyController@deleteView'));
这是实际删除的路线:
Route::delete('deletecompany/{id}', array('as'=>'deleteCompany', 'uses'=>'CompanyController@destroy'));
这是检查用户是否是预期用户的功能。
private function companyBelongsToUser($id){
$company = Company::find($id);
if($company->user_id == Auth::user()->id){
return true;
}
return false;
}