2016-08-13 12 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

私はそれのために1つ持っていないので、私はここでは、アヤックスの部分を逃すと思う。 – 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

私は今このエラーが発生しています: 'App \ Http \ Controllers \ RoutineController :: delete()の引数2がありません 私はそれのためにajaxは必要ありませんか? 'action'が期待しているurlの第2引数は何ですか? – osherdo

+1

私はAJAXでこれをやったことは一度もありません。このアクションは、おそらく設定したURLまたはルートです。私は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'); 

    } 
関連する問題