2016-08-13 126 views
0

我想要刪除記錄:在按鈕單擊(hasMany)時刪除屬於用戶的例程。我已經建立了視圖,模型和關係,刪除路線以及要刪除的控制器方法。從關係中刪除記錄

當我嘗試點擊按鈕從數據庫中刪除例程時,它什麼都不做。爲什麼它不刪除記錄?

這裏是我的代碼:路線: Route::post('routine/delete', '[email protected]'); // Delete a routine for a user. 控制器:

public function delete(Request $request) 
{ 
     $id = $request->input("id"); // Getting the id via. the ajax request. 

     $routine = \App\Routine::find($id); //Fetching the routine object from the db ifentified by the id passed via ajax 

     if ($routine) 
     { 
      $routine->delete(); 
     } 

     return ["status" => "success"]; 
    } 

查看:

<div class="col-lg-2"> 
     <!-- When this button is clicked, we determine which routine to remove. --> 
      <button class="btn btn-danger remove_routine" data-id="{{$routine->id}}" data-token="{{csrf_token()}}" style="display:inline">Delete</button> 
     </div> 

用戶模型:

public function routine() 
    { 
    return $this->hasMany('App\Routine'); 
    } 

常規模式:

public function user() 
{ 
    return $this->belongsTo('App\User'); 
} 

在此先感謝!

+0

你確定你通過ajax調用你想要刪除的正確的id嗎? – tanvirjahan

+0

這個問題的ajax部分在哪裏?瀏覽器控制檯中的請求是否有任何錯誤?如果出現錯誤,您將能夠在開發工具的網絡部分查看laravel堆棧跟蹤。 –

+0

我想我錯過了這裏的ajax部分,因爲我沒有它。 – osherdo

回答

1

不知道它究竟是回答你的問題,我不使用AJAX,但我總是做我刪除這樣的:

查看

@foreach($database-thing as $item) 
    <form method="POST" action="$yourActionHere" style="display:inline" > 
     <input name="_method" type="hidden" value="DELETE"> 
      <button type="submit" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Delete</button> 
    </form> 
@endforeach 

// Even easier with laravelcollective/forms 
@foreach($database-thing as $item) 
    {!! Form::open([ 
     'method'=>'DELETE', 
     'url' => [$yourUrl, $item->id // <-- Very important], 
     'style' => 'display:inline' 
    ]) !!} 
    {!! Form::button('<i class="fa fa-trash"></i> Verwijder', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs']) !!} 
    {!! Form::close() !!} 
@endforeach 

控制器

public function destroy($id) 
{ 
    YourModel::destroy($id); 
    // All your other logic like redirects and stuff 
} 
+0

我現在得到這個錯誤: '應用程序\ Http \ Controllers \ RoutineController :: delete()'缺少參數2' 您確定我不需要ajax嗎? 「action」期望的url的第二個參數是什麼? – osherdo

+1

我從來沒有用AJAX做過,它工作得很好。該操作可能是您設置的網址或路線。我使用RESTful控制器,所以我所有的路由都是資源路由。 – Loek

+0

好的,謝謝你。我會在這裏貼上最終的解決方案。 – osherdo

0

工作刪除,基於上面的代碼和這個更新的控制器功能:

public function delete(Request $request,$id) 
    { 

     $user=Auth::user(); 
     $routine = \App\Routine::findOrFail($id); // find or throw an error if you don't find the routine id in db. 
     // Makes if() statement redundant, since it checkes for that id already. 

     // Need to check that the owner of the routine is the current authenticated user. 
     if ($user->id != $routine->user->id) 
     { 
      Session::flash('flash_message', 'No routine found.'); 
     } 
     else 
     { 
      $routine->delete(); 
      Session::flash('routine_deleted','Routine deleted!'); 
     } 

     // No need to return $routine since I am deleting it, otherwise it will throw and error of trying to get property of non-object. 

     return redirect()->back()->with('user') ; 
     //return view('view_routine')->with('user', 'routine'); 

    }