Source: martinjoo.dev

Blog for Laravel Artisans
This article is all about writing maintainable Laravel code in general. I've been (mostly) a PHP (90% Laravel) developer since 2012 and I've been reviewing code since around 2017.

Consider the following snippet: class Post extends Model { public function comments(): HasMany { return $this->hasMany(Comment::class); }} class PostController { public function index() { foreach (Post::all() as $post) { $comments = $post->comments; }} }

Consider this class: class PostResource extends JsonResource { public function toArray($request) { return [ 'title' => $this->title, 'content' => $this->content, 'comments' => CommentResource::collection( $this->comments ), ]; }}

Gimme some Laravel code: public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('body'); $table->dateTime('published_at')->index(); $table->timestamps(); }); }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners