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('[email protected]', $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'=>'[email protected]')); 

這是實際刪除路線:

Route::delete('deletecompany/{id}', array('as'=>'deleteCompany', 'uses'=>'[email protected]')); 

這是檢查用戶是否是預期的用戶的功能。謝謝我欣賞

private function companyBelongsToUser($id){ 
     $company = Company::find($id); 
     if($company->user_id == Auth::user()->id){ 
      return true; 
     } 
     return false; 
    } 
+0

你是什麼意思的「扔我的頁面」?你得到什麼錯誤? –

+0

沒有錯誤信息,只是返回頁面。 – kehinde

回答

0

最後,我解決了它。主要的問題是我試圖在運行時檢索公司的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.'); 
     } 
    }