Reputation: 574
Why does firstOrFail()
cause the results of this eloquent query to change?
>>> Organization::find('300048');
=> Models\Organization {#889
id: 300048,
token: "redacted",
name: "DAYJK4EGPE",
domain: null,
created_at: "2017-05-19 17:42:55",
updated_at: "2017-05-19 17:42:56",
stripe_customer_id: "redacted",
referral_snippet: null,
primary_business: null,
}
>>> Organization::find('300048')->firstOrFail();
=> Models\Organization {#897
id: 300033,
token: "redacted",
name: "123456",
domain: "google.com",
created_at: "2017-05-17 13:24:23",
updated_at: "2017-05-24 15:50:25",
stripe_customer_id: "redacted",
referral_snippet: null,
primary_business: null,
}
>>>
The model with id 300033
is the first in my database, but I expected it to still return the organization with id 300048
, since that was the only result in the collection firstOrFail()
is being called on.
Upvotes: 0
Views: 754
Reputation: 9146
You've figured it out yourself, but essentially firstOrFail
is going to grab the first record when you use a where
, if you don't use a where
clause it will just get the first record in the DB.
As you've figured out, you need findOrFail
in order to find the first record with that ID, or fail.
Upvotes: 2
Reputation: 574
You should use use Organization::findOrFail('300048');
.
See the Laravel docs for more information on throwing exceptions.
Upvotes: 2