Luis Masuelli
Luis Masuelli

Reputation: 12323

Laravel 4 - Getting a subset of model attributes

I have this laravel model method call:

$entry = self::firstOrCreate(array('url' => $url, 'version' => $version));

But it has a problem: One of the columns is a TEXT column which could hold really large data I don't need (in this context).

What do I need: A way to load-or-create this model without loading the content (TEXT) data.

Question: Is there a model method I could use in place of this, or a parameter I'm missing?

Upvotes: 0

Views: 431

Answers (1)

Jason Lewis
Jason Lewis

Reputation: 18665

Not using firstOrCreate as you'll need to issue some custom selects. I'd suggest using firstOrFail, catch the exception and then create a new model.

$payload = compact('url', 'version'); // Build an associative array out of the variables.

try
{
    $entry = Model::where('url', $url)->where('version', $version)->firstOrFail(array_keys($payload));
}
catch (Illuminate\Database\Eloquent\ModelNotFoundException $e)
{
    $entry = Model::create($payload);
}

Upvotes: 1

Related Questions