You can use model event or boot method, attach checking routine on saving/updating event.
public static function boot()
{
parent::boot();
self::saving(function($model_obj)
{
});
}
I think you just look up the album name to check if it already exists before creating a new one.
You might also want to put some sort of primary key or index on the album table to make the searching faster. Album name would probably work but you need to decide if you treat 'High Way To HELL !!' as the same album as 'Highway to Hell' so using the name might not be the best way to uniquely identify and album
This is what I typically do..
// always use first to make sure you get only one album not the collection
$album = Album::whereName( 'Highway to Hell')->first();
if ( is_null( $album ) ) {
// create a new one and save there is no album with this name
} else {
// do what you want to do with the existing one
// example
$album->timesRequested = $album->timesRequested + 1;
$album->save();
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community