I am creating a 'Add to Playlist' function for my cocktail app. The user will be able to add Cocktails to a Playlist. This is a many to many relationship.
Cocktail.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class cocktail extends Model
{
protected $primaryKey = 'idDrink';
protected $fillable = ['strDrink', 'strCategory', 'strGlass', 'strInstructions', 'strIngredient1'];
public function playlist()
{
return $this->belongsTo(Playlist::class);
}
public function favourite()
{
return $this->hasMany(Favourite::class);
}
}
Playlist.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class playlist extends Model
{
protected $fillable = ['name', 'description'];
public function cocktails()
{
return $this->hasMany(Cocktail::class);
}
}
I have a 'Add to Playlist' button on the cocktail, that has a name of {{ $cocktail-idDrink }}. When the user clicks that button, a modal pops up with the list of available playlists that are also links with a name of the {{ $playlist->id }}. I am trying to connect these 2 some two, so when the playlist button is clicked, the cocktail is added to that playlist.
I know I need some sort of form on the Add to Playlist button, but am not sure how to achieve this. When I add a form to the button, I get a 'No Message' error.
Add to Playlist Btn
<button type="submit" href="#" class="primary-btn add-btn" name="{{ $cocktail->idDrink }}" data-toggle="modal" data-target="#exampleModalCenter"><i class="fas fa-plus"></i>Add to Playlist</button>
Modal Playlist Btn
@foreach($playlists as $playlist)
<div class="modal-body">
<a name="{{ $playlist->id }}" href="#">{{ $playlist->name }}</a>
</div>
@endforeach