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)…


This content originally appeared on DEV Community and was authored by Morcos Gad

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();
// Don't do this
$post->author_id == $user->id;
// Do this
$post->author()->is($user);

I hope you enjoyed the code.


This content originally appeared on DEV Community and was authored by Morcos Gad


Print Share Comment Cite Upload Translate Updates
APA

Morcos Gad | Sciencx (2021-11-30T08:31:42+00:00) Clean Code – Laravel. Retrieved from https://www.scien.cx/2021/11/30/clean-code-laravel/

MLA
" » Clean Code – Laravel." Morcos Gad | Sciencx - Tuesday November 30, 2021, https://www.scien.cx/2021/11/30/clean-code-laravel/
HARVARD
Morcos Gad | Sciencx Tuesday November 30, 2021 » Clean Code – Laravel., viewed ,<https://www.scien.cx/2021/11/30/clean-code-laravel/>
VANCOUVER
Morcos Gad | Sciencx - » Clean Code – Laravel. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/30/clean-code-laravel/
CHICAGO
" » Clean Code – Laravel." Morcos Gad | Sciencx - Accessed . https://www.scien.cx/2021/11/30/clean-code-laravel/
IEEE
" » Clean Code – Laravel." Morcos Gad | Sciencx [Online]. Available: https://www.scien.cx/2021/11/30/clean-code-laravel/. [Accessed: ]
rf:citation
» Clean Code – Laravel | Morcos Gad | Sciencx | https://www.scien.cx/2021/11/30/clean-code-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.