Neon Tsunami

Comparing model equality in Laravel

July 15, 2016

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, written by Dwight Watson.