Finalized many to many relationship
This commit is contained in:
@@ -37,6 +37,6 @@ class BeerController extends Controller
|
|||||||
|
|
||||||
$beer->save();
|
$beer->save();
|
||||||
|
|
||||||
return redirect('/profile');
|
return back()->with('success', 'Beer added!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class BeerListController extends Controller
|
|||||||
|
|
||||||
$list->save();
|
$list->save();
|
||||||
|
|
||||||
return redirect('/profile');
|
return redirect("/list/" . $list->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addItem(Request $request, $id)
|
public function addItem(Request $request, $id)
|
||||||
@@ -41,8 +41,8 @@ class BeerListController extends Controller
|
|||||||
$beerId = $request->beer;
|
$beerId = $request->beer;
|
||||||
$list = BeerList::findOrFail($id);
|
$list = BeerList::findOrFail($id);
|
||||||
|
|
||||||
// $list->beer()->attach($beerId);
|
$list->beer()->attach($beerId);
|
||||||
|
|
||||||
dd($list->beer());
|
return redirect("/list/" . $list->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ class Beer extends Model
|
|||||||
|
|
||||||
public function list()
|
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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class BeerList extends Model
|
|||||||
|
|
||||||
public function beer()
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ class BeerListPivot extends Migration
|
|||||||
Schema::create('beer_list_pivot', function (Blueprint $table) {
|
Schema::create('beer_list_pivot', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->foreignId('beer_id')->constrained();
|
$table->foreignId('beer_id')->constrained();
|
||||||
$table->foreignId('list_id')->constrained();
|
$table->foreignId('list_id')->constrained('beer_lists');
|
||||||
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,10 @@
|
|||||||
<label for="review">
|
<label for="review">
|
||||||
Review
|
Review
|
||||||
</label>
|
</label>
|
||||||
<textarea name="review" style="resize:none"></textarea><
|
<textarea name="review" style="resize:none"></textarea>
|
||||||
<button type="submit">Add Beer</button>
|
< <button type="submit">Add Beer</button>
|
||||||
</form>
|
</form>
|
||||||
|
@if (session()->has('success'))
|
||||||
|
<p class="text-green-400">{{ session()->get('success') }}</p>
|
||||||
|
@endif
|
||||||
@endsection
|
@endsection
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
@section('content')
|
@section('content')
|
||||||
{{ $list->title }}
|
{{ $list->title }}
|
||||||
|
|
||||||
<form method="POST" action="{{ route('list.additem', $list->id) }}">
|
<form method="POST" action="{{ route('list.additem', $list->id) }}">
|
||||||
@csrf
|
@csrf
|
||||||
<select name="beer">
|
<select name="beer">
|
||||||
<option hidden>
|
<option hidden>
|
||||||
@@ -26,8 +26,45 @@
|
|||||||
<button type="submit">
|
<button type="submit">
|
||||||
Add beer
|
Add beer
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
<a href="{{ route('beer.create') }}">
|
<table class="border-2 border-black text-center my-10">
|
||||||
|
<tr>
|
||||||
|
<th class="px-2">
|
||||||
|
Beer
|
||||||
|
</th>
|
||||||
|
<th class="px-2">
|
||||||
|
Rating
|
||||||
|
</th>
|
||||||
|
<th class="px-2">
|
||||||
|
Country
|
||||||
|
</th>
|
||||||
|
<th class="px-2">
|
||||||
|
Type
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
@foreach ($list->beer as $beer)
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
{{ $beer->beer }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ $beer->rating }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ $beer->country }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ $beer->type }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<a href="{{ route('beer.create') }}">
|
||||||
Can't find your beer?
|
Can't find your beer?
|
||||||
</a>
|
</a>
|
||||||
@endsection
|
@endsection
|
||||||
|
|||||||
Reference in New Issue
Block a user