Morpheus
Morpheus

Reputation: 997

Laravel FirstOrCreate or something else

I would like to limit only one entry in the table and I am not sure how to do it.

I've read about firstOrCreate but in my model I have:

use Illuminate\Database\Eloquent\Model;
class Ad extends Model
{
   protected $fillable = [
     'title',
     'description',
     'image'
   ];
}

Basically I want to use it for some advertising box on website but only one will be visible. So I don't want to loop over and show for example last one.

How can I achieve this?

Thank you.

Upvotes: 0

Views: 336

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163848

Since you want to have only one ad in a table the first() method to get it:

$ad = Ad::first();

Upvotes: 1

Related Questions