Finalized many to many relationship

This commit is contained in:
2021-09-05 02:51:33 +02:00
parent f4c030934a
commit d1d2198ef5
7 changed files with 77 additions and 35 deletions

View File

@@ -37,6 +37,6 @@ class BeerController extends Controller
$beer->save();
return redirect('/profile');
return back()->with('success', 'Beer added!');
}
}

View File

@@ -33,7 +33,7 @@ class BeerListController extends Controller
$list->save();
return redirect('/profile');
return redirect("/list/" . $list->id);
}
public function addItem(Request $request, $id)
@@ -41,8 +41,8 @@ class BeerListController extends Controller
$beerId = $request->beer;
$list = BeerList::findOrFail($id);
// $list->beer()->attach($beerId);
$list->beer()->attach($beerId);
dd($list->beer());
return redirect("/list/" . $list->id);
}
}

View File

@@ -11,6 +11,6 @@ class Beer extends Model
public function list()
{
return $this->belongsToMany(BeerList::class, 'beer_list_pivot', 'list_id', 'beer_id');
return $this->belongsToMany(BeerList::class, 'beer_list_pivot', 'beer_id', 'list_id');
}
}

View File

@@ -21,6 +21,7 @@ class BeerList extends Model
public function beer()
{
return $this->belongsToMany(Beer::class, 'beer_list_pivot', 'beer_id', 'list_id');
return $this->belongsToMany(Beer::class, 'beer_list_pivot', 'list_id', 'beer_id')
->withTimestamps();
}
}