samedi 13 août 2016

Delete A Record From A Relationship

I want to delete a record: remove a routine,that belongs to a user, on button click (hasMany). I have set up the view, models and relationship within,delete route, and the controller method to delete.

When I try to click the button to remove the routine from the db, it does nothing. why does it not removing the record?

Here's my code: route: Route::post('routine/delete', 'RoutineController@delete'); // Delete a routine for a user. Controller:

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"];
    }

View:

<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="" data-token="" style="display:inline">Delete</button>
        </div>

User Model:

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

Routine model:

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

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire