Back to School Sale! All accounts are 50% off this week.

kevinbui's avatar

kevinbui liked a comment+100 XP

1w ago

How do you actually read the Laravel docs without getting overwhelmed as a self-learner?

Start with bare basics and build a step at a time and take the free courses on PHP and laravel.

https://laracasts.com/series/php-for-beginners-2023-edition

https://laracasts.com/series/laravel-from-scratch-2026

And go from there to javascript, etc.

how do you read them so it sticks

When new I had a small app to just work the example in the documentation. Actually code the examples. Type, not copy and paste.

kevinbui's avatar

kevinbui was awarded Best Answer+1000 XP

2w ago

How to mock a service class used inside a controller ?

All of your solutions are legit.

Not super important, but to make this feature more SOLID. Maybe we create an interface for TypeService to implement, TypeServiceInterface for example, and bind that interface to TypeService. That will abide to the Dependency Injection principle.

class TypeService implements TypeServiceInterface {}

$app->bind(TypeServiceInterface::class, TypeService::class);

public function index(TypeServiceInterface $typeService) {}

As I understand, mocking interfaces in test cases could be more efficient and faster than actual classes.

Plus, a textbook benefit is the ability to substitute different implementations for the same interface (I don't think this is important anymore).

kevinbui's avatar

kevinbui liked a comment+100 XP

3w ago

Please advise how a beginner should learn to implement filter and search features.

From your question, it might be a good idea to learn the MySql WHERE and ORDER BY clauses as a first step.

kevinbui's avatar

kevinbui wrote a reply+100 XP

4w ago

Where is the best place to log user activity ?

All solutions given by @laryai are legit.

This package by Spatie also looks pretty cool.

kevinbui's avatar

kevinbui wrote a reply+100 XP

4w ago

How to mock a service class used inside a controller ?

All of your solutions are legit.

Not super important, but to make this feature more SOLID. Maybe we create an interface for TypeService to implement, TypeServiceInterface for example, and bind that interface to TypeService. That will abide to the Dependency Injection principle.

class TypeService implements TypeServiceInterface {}

$app->bind(TypeServiceInterface::class, TypeService::class);

public function index(TypeServiceInterface $typeService) {}

As I understand, mocking interfaces in test cases could be more efficient and faster than actual classes.

Plus, a textbook benefit is the ability to substitute different implementations for the same interface (I don't think this is important anymore).

kevinbui's avatar

kevinbui liked a comment+100 XP

4w ago

How to mock a service class used inside a controller ?

Hello,

I have this index function.

public function index()
{
    Gate::authorize('viewAny', Type::class);

    return Inertia::render('Admin/Types/Index', [
        'types' => fn () => (new TypeService)->all()->toResourceCollection(),
    ]);
}

But this way, the TypeService isn't mockable.

Is it a good idea to replace by this code ?

'types' => fn () => app(TypeService::class)->all()->toResourceCollection(),

I can also inject the service in the constructor of the controller, but if this service class isn't used inside all methods, it's not useful to load it inside the constructor.

So perhaps another way is to inject the service class inside the methods where it's used ?

Like this example ?

public function index(TypeService $typeService)

What's the best way to use this service class so that I can test the controller without executing the service class methods ?

Thanks for your help.

V

kevinbui's avatar

kevinbui wrote a reply+100 XP

4w ago

laravel Job

Have you searched for "Laravel" on LinkedIn Jobs?

Have you been to your local PHP/Laravel meetup? They typically find job candidates there.

kevinbui's avatar

kevinbui liked a comment+100 XP

4w ago

laravel Job

Have you tried Larajobs. https://larajobs.com/

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

How to manage different data for a customer

Cool, all the suggestions are spot on.

This thread inspires me to propose a Illuminate\Auth\Events\LoggingOut in this PR.

If the PR is approved, we can do something like this:

use Illuminate\Auth\Events\LoggingOut;

class SaveLastStore
{
    public function handle(LoggingOut $event)
    {
        return $event->user->update(['last_store_id' => session('last_store_id')]);
    }
}

So we don't have to re-update the last_store_id in the above selectStore method.

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

How to manage different data for a customer

@yougotnet I’ve worked on plenty of “multi-tenant” applications like this. Just keep things simple by using foreign keys and route parameters.

For example, one of my projects is a multi-tenant CMS (like Wix or Squarespace). When a user logs into the admin panel, if they belong to multiple websites then they can pick which website they want to manage. When they do, they’re then just redirected to the dashboard route (/websites/{website}/admin). The route group looks like this:

Route::group([
    'middleware' => ['auth', 'can:update,website'],
    'prefix' => 'websites/{website:slug}/admin',
], static function (): void {
    Route::get('/', DashboardController::class)->name('website.admin.dashboard');

    // Other website admin routes...
});

Each controller action then gets the current website injected as a parameter, so you can then scope model queries to that website:

namespace App\Http\Controllers\Admin;

class ArticleController extends Controller
{
    public function index(Website $website)
    {
        $articles = $website->articles()->paginate();

        return view('admin.article.index', compact('website', 'articles'));
    }

    public function store(Website $website, StoreArticleRequest $request)
    {
        $article = $website->articles()->create($request->validated());

        return redirect()
            ->route('website.admin.article.index')
            ->with('success', 'Article created.');
    }
}
kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

How to manage different data for a customer

You can simply add a field to the users table to save the last used store.

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

How to manage different data for a customer

Solution:

You can achieve this by leveraging session variables to store the "active store" for the customer and also saving the last active store in the database (in the user's profile, for example) so you can restore it when the user logs back in.

Here's a high-level approach:

1. Update Customer Model

Add a field to your users (or customers) table to store last_store_id.

// migration example
Schema::table('users', function (Blueprint $table) {
    $table->unsignedBigInteger('last_store_id')->nullable();
});

2. Store Selected Store in Session

When the user selects a store, update the session and the user's last_store_id:

public function selectStore(Request $request, Store $store)
{
    // Put the store ID in session
    session(['current_store_id' => $store->id]);
    
    // Update user's last_store_id
    Auth::user()->update([
        'last_store_id' => $store->id
    ]);

    return redirect()->back();
}

3. Set Store on Login

When the user logs in, check if last_store_id is set, and put it in the session:

public function authenticated(Request $request, $user)
{
    if ($user->last_store_id) {
        session(['current_store_id' => $user->last_store_id]);
    }
}

Register this method in your LoginController. In Laravel, you can use the authenticated method for this:

// app/Http/Controllers/Auth/LoginController.php
protected function authenticated(Request $request, $user)
{
    if ($user->last_store_id) {
        session(['current_store_id' => $user->last_store_id]);
    }
}

4. Retrieve Data Based on Selected Store

Whenever you need to fetch store-specific data, use session('current_store_id'):

public function someControllerMethod()
{
    $storeId = session('current_store_id');
    $store = Store::findOrFail($storeId);

    // Fetch data related to this store
    $products = $store->products;
    // ...
}

5. Optional: Middleware for Store Context

If you want, you can create a middleware to ensure the session always has a store, redirecting the user to select one if not:

public function handle($request, Closure $next)
{
    if (!session('current_store_id')) {
        return redirect()->route('select.store');
    }
    return $next($request);
}

Apply this middleware to any routes that require a store context.


Summary:

  • Use a session variable to store the active store.
  • Save the last active store in the user/customer table.
  • Restore this when the user logs in.
  • Always fetch data using the store from the session.

This approach will make sure the app always serves up data for the selected store, and that the user's last-used store is remembered on re-login.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

Pest Featured test tutorial with latest version any suggestions?

For something that specific, I believe you have no problems figuring out to do so yourself.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

Laravel, React , Typescript ,Inertia Course

What's wrong with with taking a separate course for each?

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

Laravel without Composer?

I suggest troubleshoot the problem, fix it and correctly use composer.

kevinbui's avatar

kevinbui was awarded Best Answer+1000 XP

1mo ago

How to mark one of my questions as Solved?

You probably know by now that it is the top right corner of a post.

This confusion makes me realise that some items in this dark mode could be super blurry for new members.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

How to mark one of my questions as Solved?

You probably know by now that it is the top right corner of a post.

This confusion makes me realise that some items in this dark mode could be super blurry for new members.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

Paid Laracasts Subscription

I highly recommend the paid subscription. The content quality if superb, help me get hired a decade ago and stay up-to-date today.

Quality content production and maintaining this website are very time-consuming. I believe they mostly don't have time to answer your questions directly.

But that should not be a problem, you can always ask AI or post your question in this forum. There are like 50 Laravel veterans always happy to help.

kevinbui's avatar

kevinbui wrote a comment+100 XP

1mo ago

The Many Misconceptions of Laravel : Ep 6, Is the Service Container Too Much?

I have been working with Laravel for more than a decade, but I still learn something new in every video in this series. Great work!

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

New book on how Laravel works under the hood. Looking for volunteer reviewers 🙏

Hi everyone,

I've been working on something for a few months, and I'm finally excited to share it here. I wrote a book about how Laravel actually works internally; the idea is that you build a mini Laravel from scratch, piece by piece: the service container, service providers, facades, the request lifecycle, the router, middleware, and the query builder to Eloquent.

Right now it's in the final review stage with me. Before I wrap it up, I'd love to get more eyes on it from people who really know the framework.

So I'm looking for a few volunteers who'd be up for reviewing it. It's completely volunteer-based; if you enjoy digging into Laravel's internals and want to help make the book better, I'd genuinely appreciate it.

If you're interested, you can inform me via the official website book: https://laravelinternals.com/

Thanks so much, and happy to share details more via email.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

Eloquent inside a migration ?

If you are playing around, not a problem. But pls don't do this a real project. Eventually it will slow down CI/CD drastically.

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

Eloquent inside a migration ?

Oh it was so silly ... I had refreshed all the database and the database is seed after the migrations, so while he migrations are run, there is no data.

kevinbui's avatar

kevinbui wrote a reply+100 XP

1mo ago

Eloquent Performance Patterns Course Needs an Update for Laravel 12/13

This is one of my favourite course in Laracasts. I actually bought the course from Jonathan Reinink for around $100 a year before it was released on Laracasts.

My answer is nah. No, that course does not need updating. Eloquent got quite a few updates since then but most of the examples are still working. Maybe the use of hasOne gotta be more up-to-date but people will figure that out.

Plus, the point of that course is not really about Eloquent. It is about constructing about the underlying DB queries that will be a lot more performant. Eloquent could be evolving, but DB queries will mostly stay the same.

kevinbui's avatar

kevinbui liked a comment+100 XP

1mo ago

How to review AI-generated code

@phpmick You should be in control of any and all code making its way into your codebase, whether that’s written by you, a colleague, or an LLM. Here are some tips and guidelines I follow when doing AI-assisted development:

  • Give agents very discreet tasks to complete.
  • When you’re prompting the agent, ask it to ask you about anything it’s unsure of instead of guessing. You’ll find you’ll get something far more in line with what you had in mind and were expecting, instead of giving an agent a loose description, and it making assumptions and making something that’s maybe 60% of what you wanted.
  • Give agents way to verify the work they’re producing. There should be a goal, as well as instructions on how to run any tool such as linting and testing tools. If linting/tests fail, the agent should go back and fix what’s broken before asking for your attention.
  • Agents should also be given guardrails to avoid getting stuck in a loop and burning tokens.
  • You should only be merging code you actually understand. If you don’t, review the agent output log. If you’re still unsure, then ask the agent to explain what it’s produced. As with human-produced code, less code is easier to grok than lots of code. Don’t have your agent spew out 50,000 lines of code and then review.
kevinbui's avatar

kevinbui wrote a reply+100 XP

4mos ago

Reverting from PEST to PHPUnit

This Youtube video presents exactly what you need: https://www.youtube.com/watch?v=lPX-BCkkoO0

kevinbui's avatar

kevinbui started a new conversation+100 XP

4mos ago

Save My Video Speed Preferences

Hi @JeffreyWay.

I typically watch the videos at 1.2x or 1.5x speed. But any time I move on to a new video, my video speed is back to 1.0x.

Can we preserve the preferences for our video speeds? We had this feature before, and have been losing it for quite while.

Cheers,

kevinbui's avatar

kevinbui wrote a comment+100 XP

4mos ago

Supercharged Search with Typesense: Ep 12, Instant Search From Scratch

That trick to substitute debouncing is pretty sick!

kevinbui's avatar

kevinbui wrote a reply+100 XP

4mos ago

Is it worth it to learn all the courses and coding when AI can make the production level application

As of today, I believe it is still worthwhile to learn coding yourself. We still have to understand, review and request changes or refactoring to works done by AI.

There are still a lot of concerns regarding security and code quality with AI.

kevinbui's avatar

kevinbui liked a comment+100 XP

4mos ago

Is it worth it to learn all the courses and coding when AI can make the production level application

Wait I am not taking about like AI would solve a code problem or fix it. I am talking according to business, If I code an application humanly, Its gonna take too much time, When a non coder who mabe barely knows coding but he Understands how the application works, He make the whole production level application without coding a single line.

So here is my question, Is it worth it to put my all valuable time learning vuejs components structure, Laravel MVC structure, how the code should refactor etc etc? Is it worth it put my value learning all these core stuff where now AI is handling 90% of the these stuff, We just need to tell the feature name or proper prompt.

OK Here is my another analyzation, Suppose I have built Production grade with Just Claude or copilot or mabe Lovable AI prompt, I know I dont know any of code inside what's happening, But the AI generated application is serving my goals perfectly, I mean the features I needed are working perfectly, So why do we need a developer to refactor the code or understand for human ? Because the application is already serving my 95% of the goal. If I need any customization AI is also doing that for that application.

So my whole point is, Is it really worth it to learn the core stuffs? Should I put valuable time on learning these core stuffs? Or should I migrate my self to learning making application with prompt or AI.

I dont know if I am thinking in right way or not, But these questions are in my head like AI already knows why should we learn core stuffs instead I can directly make applications with AI, Ok I understand I can't control the whole codebase but my application would be ready and with AI I can make customization or scale them.

Is it really worth it to learn Core Stuff or Coding? According current situation on earth?

The recent news oracle removed 30000 employee, and investing on AI.

kevinbui's avatar

kevinbui liked a comment+100 XP

4mos ago

Is it worth it to learn all the courses and coding when AI can make the production level application

Certainly, this is a very topical and thoughtful question! Here’s a nuanced answer:


It’s true that AI can generate functional code and even entire applications from prompts. For many scenarios, this is already a huge productivity boost and enables non-coders to prototype and even ship apps quickly.

But here’s why it’s still very much worth learning core coding fundamentals, structures, and patterns:

1. Understanding and Control

  • Debugging and Maintenance: AI can generate "working" code, but when things go wrong — and they will, especially at scale — you need to understand how things work under the hood to fix, debug, or even understand issues.
  • Customization: What if the AI generates code that almost works but isn’t quite what you want, or introduces subtle bugs, security issues, or performance problems? Deep knowledge lets you adapt and truly own your app.
  • AI Limitations: AI often generates code by pattern-matching, not real reasoning. It might miss context, overlook edge cases, or accidentally introduce hard-to-find bugs.

2. Best Practices, Security, and Large Scale

  • Scalability and Security: A human developer who understands best practices will be better positioned to make an app robust, secure, and able to scale — things AI can only do reliably if you can guide and review its decisions.
  • Refactoring: As your app grows, codebases get messy. You’ll need to refactor and optimize. Even with AI, knowing what and why to refactor is a core developer skill.

3. Job Market and Value

  • AI is a Tool, Not a Replacement (Yet): Companies are laying off in some places due to AI, but they are also hiring engineers who know how to use AI productively and oversee software quality.
  • Prompting requires understanding: Prompt engineering is easier when you understand what you’re asking AI to do. The best AI users are often those with deep context and technical understanding.

4. Business Risk

  • Black Box Risk: If you don’t know what’s happening in your codebase, you’re taking a business and legal risk. You may expose yourself to security vulnerabilities or technical debt that AI can’t explain, and if AI tools disappear, your knowledge base is gone too.

5. Learning is Never Wasted

  • Long-Term Flexibility: Technology changes rapidly. A solid foundation will make you adaptable to new frameworks, programming languages, and even future AI tools themselves.

A Balanced Approach

It’s smart to leverage AI for speed and help, but combine it with ongoing learning of core principles. Use AI to accelerate rote tasks and prototyping, and dedicate time to understanding the architecture, design patterns, and debugging.

Practical suggestion:

  • Use AI to help you learn! Let it give examples, ask it to refactor your code, or explain concepts.
  • But don’t skip the step of hands-on understanding and code reading.

Summary

Yes, it’s absolutely still worth learning the core stuff. AI will make you more productive, but foundational knowledge is what will truly make you a capable, valuable developer — whether you’re building with or without AI.


Example Scenario:

Suppose AI generates a Laravel API endpoint, but you encounter strange performance issues as your app scales. If you understand Laravel's request lifecycle, Eloquent, and how to profile code, you can identify and fix the bottlenecks or even teach the AI how to do it better next time. Otherwise, you're stuck, no matter how smart the AI appears!

In summary: Think of AI as a superpower, not a replacement for understanding. Combine both, and you'll have a massive edge in the new era.

kevinbui's avatar

kevinbui wrote a comment+100 XP

6mos ago

Supercharged Search with Typesense: Ep 6, Facets

Awesome lesson! And super clear explanation for newbies!

kevinbui's avatar

kevinbui wrote a reply+100 XP

6mos ago

project ideas

If it is simply to practise what you have learn, you don't need to come up with ground breaking ideas. Just try to re-create the applications that you see everyday:

  • A forum, like this forum.
  • A social network, such as Facebook, Instagram,....
  • A Q&A platform, such as Quora.
  • Something related to your hobbies. For example, I like reading, so I create a library app.

And you don't have to finish any of those, spin up as many projects as you like.