Courses

How to Structure Laravel 11 Projects

  • 11 lessons
  • 11042 words

This content is available only for premium members.

About this course

Structuring Laravel projects is one of the most typical questions I see in the community. The reason is that, for better or worse, Laravel has many options to do that!

  • Services?
  • Actions?
  • Jobs?
  • Observers?
  • Mutators?
  • Events/listeners?
  • Traits?
  • Helpers?
  • Modules?
  • Components?
  • Packages?

In this course, I will provide an overview of the main options with real-life examples from open-source projects.

We will discuss:

  • Where to offload logic from the Controller to make it shorter
  • How to structure admin/user areas
  • Is it worth dividing the app into modules
  • When is it time to create re-usable packages
  • ... and more

As a practical example, we will try to shorten this Controller method:

public function store(Request $request)  
{  
    $this->authorize('user_create');  
  
    $userData = $request->validate([  
        'name' => 'required',  
        'email' => 'required|unique:users',  
        'password' => 'required',  
    ]);  
  
    $userData['start_at'] = Carbon::createFromFormat('m/d/Y', $request->start_at)->format('Y-m-d');  
    $userData['password'] = bcrypt($request->password);  
  
    $user = User::create($userData);  
    $user->roles()->sync($request->input('roles', []));  
  
    Project::create([  
        'user_id' => $user->id,  
        'name' => 'Demo project 1',  
    ]);
    Category::create([  
        'user_id' => $user->id,  
        'name' => 'Demo category 1',  
    ]);
    Category::create([  
        'user_id' => $user->id,  
        'name' => 'Demo category 2',  
    ]);  
  
    MonthlyReport::where('month', now()->format('Y-m'))->increment('users_count');  
    $user->sendEmailVerificationNotification();  
  
    $admins = User::where('is_admin', 1)->get();  
    Notification::send($admins, new AdminNewUserNotification($user));  
  
    return response()->json([  
        'result' => 'success',  
        'data' => $user,  
    ], 200);  
}

For more complex topics, I intentionally used the word overview above. This course aims to show you the options, but each option is a "rabbit hole," often requiring an entirely separate course on that specific pattern.

So, at the end of the course, you will see a lesson with extra links to other resources from myself and the community if you choose to dive deeper into some structure topics.

Now, let's start our journey with the lesson "From Controllers to Form Request".