osherdo
osherdo

Reputation: 568

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="{{$routine->id}}" data-token="{{csrf_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!

Upvotes: 0

Views: 563

Answers (3)

Ray Zion
Ray Zion

Reputation: 678

There you go

$remove = 2;

$filtered = $c->filter(function ($value, $key) use($remove){
    return $value['id']!=$remove;
});

Upvotes: 0

osherdo
osherdo

Reputation: 568

Working delete, based on the code above and this updated controller function:

 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');

    } 

Upvotes: 0

Loek
Loek

Reputation: 4135

Don't know if it exactly answers your question, and I don't use AJAX, but I always do my deletes like this:

View

@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

Controller

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

Upvotes: 1

Related Questions