Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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)
   {

   });
}
Last updated 2 years ago.
0

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();
 
}

Last updated 2 years ago.
0

Adding a new table...sighs.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Jameron jameron Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.