Sunday, May 21, 2017

Base table or view not found: Many to many relation laravel

I have two models setup in many to many relation with pivot tabel climbincludeds_tour:

Tour.php

class Tour extends Model
{
  protected $table = 'tours';
  public function climbIncluded(){
    return $this->belongsToMany('App\ClimbIncluded',
                                'climbincludeds_tour',
                                'tour_id', 'cincluded_id');
    }
}

ClimbIncluded.php

class ClimbIncluded extends Model
{
  protected $table = 'climbincludeds';
    public function tours()
    {
     return $this->belongsToMany('App\Tour');
    }
}

And I have a delete button on view attached to destroy method in ClimbIncludedController.php

    public function destroy(ClimbIncluded $climbIncluded)
{
    $climbIncluded->tours()->detach();
    $climbIncluded ->delete();
    Session::flash('success','Item sucessfully deleted !');
    return redirect()->route('climb.ie');
}

When I want to delete a record laravel returns an error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'peak.tour_trek_included' 
doesn't exist (SQL: delete from `tour_trek_included` where `trek_included_id` = 1)

If I remove or comment out the $climbIncluded->tours()->detach(); from the destroy method, laravel deletes the record without any error. I have passed name of pivot table as second argument to belongsToMany method also. What am I missing here ?



via Tanja Forsberg

Advertisement