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(); }); }