Indeed, I do not think that no one knows about the laraveldaily platform and the useful articles that we get from there. I wrote an article on this platform that talks about Typical Mistakes Juniors Make, and I wanted to share with you the main points in it
- Not Using Route Groups
// you have routes like this
Route::get('dashboard', [HomeController::class, 'index'])->name('dashboard')->middleware(['auth']);
Route::resource('donation', DonationController::class)->middleware(['auth']);
Route::resource('requisition', RequisitionController::class)->middleware(['auth']);
Route::name('admin.')->prefix('admin.')->group(function () {
Route::view('/', 'admin.welcome')->middleware(['auth', 'admincheck']);
Route::resource('donor', DonorController::class)->middleware(['auth', 'admincheck']);
Route::resource('details', OrganisationDetailController::class)->middleware(['auth', 'admincheck']);
});
// Now
Route::middleware('auth')->group(function () {
Route::get('dashboard', [HomeController::class, 'index'])->name('dashboard');
Route::resource('donation', DonationController::class);
Route::resource('requisition', RequisitionController::class);
Route::name('admin.')->prefix('admin.')->middleware('admincheck')->group(function () {
Route::view('/', 'admin.welcome');
Route::resource('donor', DonorController::class);
Route::resource('details', OrganisationDetailController::class);
});
});
- Not Using Route Model Binding
// in your routes you have
Route::resource('student', StudentController::class);
// But I see some beginners still write Controller code like this
public function show($id)
{
$student = Student::findOrFail($id);
return view('dashboard/student/show', compact(['student']));
}
// Now, use Route Model Binding and Laravel will find Model
public function show(Student $student)
{
return view('dashboard/student/show', compact(['student']));
}
- Too Long Eloquent Create/Update Code
// When saving data into DB I have seen people write code similar to this
public function update(Request $request)
{
$request->validate(['name' => 'required']);
$user = Auth::user();
$user->name = $request->name;
$user->username = $request->username;
$user->mobile = $request->mobile;
// Some other fields...
$user->save();
return redirect()->route('profile.index');
}
// First, in this example, you don't need to set the Auth::user() to the $user variable. The first option could be
public function update(Request $request)
{
$request->validate(['name' => 'required']);
auth()->user()->update([$request->only([
'name',
'username',
'mobile',
// Some other fields...
]);
return redirect()->route('profile.index');
}
// The second option put validation into Form Request. Then into the update() method you would need just to pass $request->validated()
public function update(ProfileRequest $request)
{
auth()->user()->update([$request->validated());
return redirect()->route('profile.index');
}
- Not Naming Things Properly
// For example, shortening variable names: instead of $data they call $d. Always use proper naming. For example
Route::get('/', [IndexController::class, 'show'])
->middleware(['dq'])
->name('index');
Route::get('/about', [IndexController::class, 'about'])
->middleware(['dq'])
->name('about');
Route::get('/dq', [IndexController::class, 'dq'])
->middleware(['auth'])
->name('dq');
// if we would go into app/Http/Kernel.php to find this middleware, we could find something like this
class Kernel extends HttpKernel
{
// ...
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'admin' => \App\Http\Middleware\EnsureAdmin::class,
'dq' => \App\Http\Middleware\Disqualified::class,
'inprogress' => \App\Http\Middleware\InProgress::class,
// ...
];
}
- Breaking MVC Pattern: Logic in Blade
// Whenever I see a @php directive in a Blade file, my heart starts beating faster
@php
$x = 5
@endphp
MVC architecture was created for a reason: that separation of concerns between Model, View and Controller makes it much more predictable where to search for certain code pieces.
- Too Big Controllers
- N+1 Eloquent Query Problem
- Relationships: Not Creating Foreign Keys
- Not Reading The Documentation
I spoke briefly about the article, but you should go deeper and visit the article itself
Happy Code :)
Top comments (0)