2016-08-13 121 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'); 

    }