DEV Community

Morcos Gad
Morcos Gad

Posted on

Clean Code - Laravel

Let's get started quickly In some cases, clean code must be used as in the following

// Don't do this
Post::where('category_id', $category->id)
    ->where('author_id', $user->id)
    ->first();
// Do this
Post::whereBelongsTo($category)
    ->whereBelongsTo($user, 'author')
    ->first();
Enter fullscreen mode Exit fullscreen mode
// Don't do this
$post->author_id == $user->id;
// Do this
$post->author()->is($user);
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code.

Discussion (0)