Dwight Watson's blog

Comparing model equality in Laravel

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

Dropping in Laravel 5.3 is an easy way to compare if two model instances you've got represent the same record in your database. The new is() method checks to see if the model keys, table and connection names match up.

if (auth()->user()->is($post->author)) {
// The currently authenicated user is the author of the post.
}

For more information, see the relevant pull requests: #14281 and #14316.

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.