Laravel - Getting Started - Comparing Models
Sometimes you may need to determine if two models are the "same" or not. The is
and isNot
methods may be used to quickly verify two models have the same primary key, table, and database connection or not:
if ($post->is($anotherPost)) {
//
}
if ($post->isNot($anotherPost)) {
//
}
The is
and isNot
methods are also available when using the belongsTo
, hasOne
, morphTo
, and morphOne
relationships. This method is particularly helpful when you would like to compare a related model without issuing a query to retrieve that model:
if ($post->author()->is($user)) {
//
}