0

尝试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;
    }
4

1 回答 1

0

最后,我解决了。主要问题是我试图在运行时检索公司的 ID,但该 ID 不可用。

令我惊讶的是,Laravel 已经关心我试图手动做的事情,我所做的只是传递一个通用的 id,然后 Laravel 找出了我要查询的公司,并完成了这项工作。

public function destroy($id)
    {
        $company = Company::findOrFail($id);
        if(!$company->user_id === Auth::user()->id)
        {
            return redirect()->route('companyindex')->with('message', 'Sorry, you cannot delete this company');
        }else{
            $company->delete();
            return redirect()->route('companyindex')->with('message', 'Company successfully deleted.');
        }
    }
于 2015-12-21T19:18:51.493 回答