<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ZoZo</title>
    <description>The latest articles on Forem by ZoZo (@cyber_aurora_).</description>
    <link>https://forem.com/cyber_aurora_</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1949149%2F0e84c000-19c2-463d-85f5-4f5c1c484475.webp</url>
      <title>Forem: ZoZo</title>
      <link>https://forem.com/cyber_aurora_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cyber_aurora_"/>
    <language>en</language>
    <item>
      <title>Laravel Middleware Magic: Use Cases You Didn’t Know About</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Thu, 19 Jun 2025 05:43:18 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/laravel-middleware-magic-use-cases-you-didnt-know-about-593e</link>
      <guid>https://forem.com/cyber_aurora_/laravel-middleware-magic-use-cases-you-didnt-know-about-593e</guid>
      <description>&lt;p&gt;You probably know middleware as the gatekeeper for authentication and authorization. But middleware can do so much more than just check if a user is logged in. In real-world projects, it’s one of your most powerful tools for handling cross-cutting concerns and improving performance. It often works quietly behind the scenes, streamlining your code and reducing duplication.&lt;/p&gt;

&lt;p&gt;In this article, I’ll show you how to unlock the full potential of Laravel middleware with practical examples you can start using right away—even if you prefer minimal JavaScript or are building complex user features like wishlists and profile management.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj23kw8byarisoi5bzhjn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj23kw8byarisoi5bzhjn.jpg" alt="kirby" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Middleware Use Cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Dynamic User Preferences Without Extra Queries
&lt;/h3&gt;

&lt;p&gt;Let’s say your app allows users to customize their profiles and manage wishlists. Instead of manually fetching user preferences in every controller, you can use middleware to inject those preferences into all views or the request itself. This eliminates repetitive logic and makes your controllers cleaner.&lt;/p&gt;

&lt;p&gt;Here’s a quick example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    if (auth()-&amp;gt;check()) {
      $preferences = auth()-&amp;gt;user()-&amp;gt;preferences ?? [];
      view()-&amp;gt;share('preferences', $preferences);
    }
    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also inject these preferences directly into the request object if needed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$request-&amp;gt;attributes-&amp;gt;set('preferences', $preferences);&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. API Rate Limiting Per User Role
&lt;/h3&gt;

&lt;p&gt;Laravel’s built-in throttling is helpful, but sometimes you need more control. For example, what if admins, premium users, and guests should have different rate limits?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    $user = $request-&amp;gt;user();
    $limit = match (true) {
        !$user =&amp;gt; 10,
        $user-&amp;gt;isAdmin() =&amp;gt; 100,
        $user-&amp;gt;isPremium() =&amp;gt; 60,
        default =&amp;gt; 30,
    };

    $key = 'rate_limit:' . ($user?-&amp;gt;id ?? $request-&amp;gt;ip());

    if (RateLimiter::tooManyAttempts($key, $limit)) {
        abort(429, 'Too many requests.');
    }

    RateLimiter::hit($key, 60); // 60-second window

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write custom middleware that checks the user’s role and applies the appropriate rate limit dynamically. This keeps your API fair and responsive without bloating controller logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0i6nvohfbmn08woa5dov.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0i6nvohfbmn08woa5dov.jpg" alt="night" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Feature Flags and A/B Testing
&lt;/h3&gt;

&lt;p&gt;Middleware can manage feature flags by checking the user’s session or profile and enabling or disabling routes or views accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    if (auth()-&amp;gt;check() &amp;amp;&amp;amp; !auth()-&amp;gt;user()-&amp;gt;isInTestGroup('wishlist_redesign')) {
        return redirect()-&amp;gt;route('dashboard');
    }

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For instance, you can show a redesigned wishlist UI to only 10% of users, or roll out beta features gradually.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. On-the-Fly Content Localization
&lt;/h3&gt;

&lt;p&gt;If your app supports multiple languages, you don’t need heavy front-end logic to handle localization. Middleware can detect a user’s preferred language—based on profile settings, cookies, or browser headers—and set the app locale before any views or responses are rendered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    $locale = auth()-&amp;gt;user()?-&amp;gt;locale ?? $request-&amp;gt;getPreferredLanguage(['en', 'hu', 'de']);
    app()-&amp;gt;setLocale($locale);

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, even error messages and validation responses appear in the correct language, and your app feels native to the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frej5mdyyzr7qf6uu58j4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frej5mdyyzr7qf6uu58j4.jpg" alt="php" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Request Data Sanitization and Transformation
&lt;/h3&gt;

&lt;p&gt;Rather than manually sanitizing form input in every controller, you can create middleware that transforms or cleans up request data globally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    if ($request-&amp;gt;has('phone')) {
        $normalized = preg_replace('/\D/', '', $request-&amp;gt;input('phone'));
        $request-&amp;gt;merge(['phone' =&amp;gt; $normalized]);
    }

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normalize phone numbers, trim strings, or cast values to the correct type before they hit your controllers. This is especially useful for APIs or shared form endpoints.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesser-Known Middleware Tricks
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Early Response Short-Circuiting
&lt;/h4&gt;

&lt;p&gt;Middleware can return a response before the application logic runs. This is useful for use cases like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;IP whitelisting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;maintenance mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;serving cached responses&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s a great way to save processing time and system resources when certain conditions are met.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    if (Cache::has('products_list') &amp;amp;&amp;amp; $request-&amp;gt;is('api/products')) {
        return response()-&amp;gt;json(Cache::get('products_list'));
    }

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltg682b4k0fw4du3lujk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltg682b4k0fw4du3lujk.jpg" alt="white" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Chaining Middleware for Complex Logic
&lt;/h4&gt;

&lt;p&gt;Break logic into layers. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;one middleware checks if the user is authenticated&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;another ensures their profile is complete&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    if (!auth()-&amp;gt;user()-&amp;gt;hasCompletedProfile()) {
        return redirect()-&amp;gt;route('profile.edit')-&amp;gt;with('error', 'Please complete your profile.');
    }

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;a third verifies if they’ve accepted the latest terms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This keeps responsibilities separate, your code modular, and testing much easier.&lt;/p&gt;

&lt;h4&gt;
  
  
  Injecting Data Into the Request Object
&lt;/h4&gt;

&lt;p&gt;It can attach things like wishlist status or profile completion directly to the request object. This makes it easy to write clean, readable controllers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$request-&amp;gt;attributes-&amp;gt;set('wishlist', auth()-&amp;gt;user()?-&amp;gt;wishlist);&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Middleware Groups for Clean Routing
&lt;/h4&gt;

&lt;p&gt;If you have routes that share the same logic—like authentication, email verification, and profile checks—bundle the middleware into a group and apply it cleanly to those routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::middleware(['auth', 'verified', 'profile.complete'])-&amp;gt;group(function () {
    Route::get('/wishlist', [WishlistController::class, 'index']);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhphk900roy6jre55awiw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhphk900roy6jre55awiw.jpg" alt="pomi" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pro Tips for Real Projects
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Middleware is perfect for anything that should run globally or for a group of routes. Don’t let your controllers become dumping grounds for repetitive checks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use it to enforce business rules—for example, only users with a complete profile can access certain features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep middleware responsibilities single-purpose. That makes them easier to test, understand, and reuse.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don’t forget global middleware for handling things like security headers, logging, or CORS.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bonus Use Case: IP-Based Access Control
&lt;/h2&gt;

&lt;p&gt;Want to restrict sensitive parts of your app to certain IP addresses (e.g. admin panel)?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next)
{
    $allowedIps = ['123.45.67.89', '98.76.54.32'];

    if (!in_array($request-&amp;gt;ip(), $allowedIps)) {
        abort(403, 'Access denied.');
    }

    return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple but powerful security layer that runs before your app loads.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjvyvjlae5znrr40wo1o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjvyvjlae5znrr40wo1o.jpg" alt="kid" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Whether you’re managing user preferences, improving API responsiveness, or running feature experiments, it’s your behind-the-scenes ally for cleaner, smarter apps. The more you use it strategically, the more streamlined your codebase becomes—and the easier it is to scale and maintain.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Artisan Commands You're Not Using (But Definitely Should)</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Mon, 19 May 2025 13:13:18 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/artisan-commands-youre-not-using-but-definitely-should-oji</link>
      <guid>https://forem.com/cyber_aurora_/artisan-commands-youre-not-using-but-definitely-should-oji</guid>
      <description>&lt;p&gt;If you’ve worked with Laravel, you’ve typed &lt;code&gt;php artisan serve&lt;/code&gt;&lt;br&gt;
and maybe even &lt;code&gt;php artisan migrate&lt;/code&gt;. And while those are useful, they barely scratch the surface of what Artisan can do.&lt;/p&gt;

&lt;p&gt;Artisan isn’t just a tool for running migrations or spinning up servers—it’s a command-line companion that can boost your productivity, simplify debugging, and teach you more about Laravel’s inner workings. Yet many developers (my past self included) tend to stick with the same 3-4 commands and miss out on some seriously powerful features.&lt;/p&gt;

&lt;p&gt;Whether you’re just starting with Laravel or you’ve been building apps for years, it’s worth revisiting Artisan with fresh eyes. In this post, I’m diving into some of the lesser-known, underrated, or just plain useful Artisan commands I think every Laravel developer should explore.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4qggw7af0tw91zcxm7r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4qggw7af0tw91zcxm7r.jpg" alt="cli" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Route Commands with Artisan
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;php artisan route:list --method=POST&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Filters the route list to only show routes that respond to the POST method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt;&lt;br&gt;
You're debugging a form submission or an API endpoint and only want to see POST routes instead of searching through every GET, PUT, or DELETE one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan route:list --path=admin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Only shows routes that contain the word "admin" in the URI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt;&lt;br&gt;
If you're working on an admin panel and want to quickly audit only the /admin/... routes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan route:list --only-vendor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Lists only the routes that come from vendor packages (like Laravel Breeze, Jetstream, Filament, etc.).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt;&lt;br&gt;
You want to see what routes are registered by packages you're using—especially useful when customizing or debugging third-party tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0x3ikv8v6l2eqbn8lf2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0x3ikv8v6l2eqbn8lf2.jpg" alt="cat" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Essential make: Commands You Should Be Using More Often
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;php artisan make:controller PostController --model=Post --requests --test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--model=Post&lt;/code&gt;&lt;br&gt;
Automatically binds the model to your resource controller — no need to type-hint manually.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--requests&lt;/code&gt;&lt;br&gt;
Generates StorePostRequest and UpdatePostRequest classes with validation ready to go.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--test, --pest, --phpunit&lt;/code&gt;&lt;br&gt;
Choose your test style. This creates a matching test class for the controller you're generating.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--invokable&lt;/code&gt;&lt;br&gt;
Creates a one-method controller – perfect for single-action classes like WebhookController.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--singleton&lt;/code&gt;&lt;br&gt;
If you're working with singleton resources (like "profile"), this flag changes the routing logic to match.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:model Post --all&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--all&lt;/code&gt;&lt;br&gt;
Generates the model, migration, factory, seeder, policy, resource controller, and form requests. Insane time saver.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--controller&lt;/code&gt;&lt;br&gt;
Generates a controller along with the model (you can pair it with --resource or --api too).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--factory, --seeder, --migration&lt;/code&gt;&lt;br&gt;
Generate any of these pieces individually if you don’t need the full --all.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--morph-pivot, --pivot&lt;/code&gt;&lt;br&gt;
Generate intermediate or polymorphic pivot tables with special behavior built in.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--policy&lt;/code&gt;&lt;br&gt;
If you’re using Laravel’s authorization system, this gives you a head start.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:migration create_posts_table --create=posts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--create=table_name&lt;/code&gt;&lt;br&gt;
Tell Laravel to scaffold a full Schema::create(...) block for the specified table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--table=existing_table&lt;/code&gt;&lt;br&gt;
Use this to update an existing table – Laravel will scaffold Schema::table(...).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--path=custom/migrations&lt;/code&gt;&lt;br&gt;
Store your migration in a custom directory — helpful for organizing modular codebases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zztt27ewkqe5vebjdmn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zztt27ewkqe5vebjdmn.jpg" alt="time" width="640" height="323"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Time-Saving Shortcut Flags
&lt;/h2&gt;

&lt;p&gt;Many make: commands support shorthand versions of common flags. Instead of typing each one out, you can speed things up with single-character options.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:model Product -mcfs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This one-liner does a ton of work for you by generating:&lt;/p&gt;

&lt;p&gt;-m: a migration file&lt;br&gt;
-c: a controller&lt;br&gt;
-f: a factory&lt;br&gt;
-s: a seeder&lt;/p&gt;

&lt;p&gt;It’s your go-to for starting a new model with CRUD scaffolding ready. You can even pair it with --resource if you want a fully RESTful controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20gyg4bf4xviu4ocx0p9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20gyg4bf4xviu4ocx0p9.png" alt="excel" width="290" height="558"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Build Your Own Artisan Commands
&lt;/h2&gt;

&lt;p&gt;Sometimes the built-in Artisan commands aren't enough. You can create your own commands to automate repetitive tasks, run data cleanup, trigger services, or just make your workflow smoother.&lt;/p&gt;

&lt;p&gt;You can generate a new command with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:command CleanOldPosts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates a file in app/Console/Commands/CleanOldPosts.php. Open it, and you’ll see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected $signature = 'posts:clean';

protected $description = 'Deletes posts older than 90 days.';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can define the logic inside the handle() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle()
{
    Post::where('created_at', '&amp;lt;', now()-&amp;gt;subDays(90))-&amp;gt;delete();
    $this-&amp;gt;info('Old posts cleaned up!');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once your command is registered, you can run it just like any other Artisan command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan posts:clean&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can make it so it runs on schedule. Add it to your app/Console/Kernel.php:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$schedule-&amp;gt;command('posts:clean')-&amp;gt;daily();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Boom — automated.&lt;/p&gt;

&lt;p&gt;Creating your own Artisan commands is perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;data cleanup (e.g. old users, expired coupons)&lt;/li&gt;
&lt;li&gt;sending reminders or reports&lt;/li&gt;
&lt;li&gt;batch processing&lt;/li&gt;
&lt;li&gt;integrating with APIs on a schedule&lt;/li&gt;
&lt;li&gt;seeding logic-heavy demo data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmy5scf1cl2y45y1h6eyd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmy5scf1cl2y45y1h6eyd.jpg" alt="end" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Don’t let your workflow be limited by muscle memory and just a few familiar commands. Take time to explore what’s possible. Automate the boring stuff. Tweak the defaults. Build your own tools. That’s where you grow from just using the framework to mastering it.&lt;/p&gt;

&lt;p&gt;If you learned something new, consider bookmarking this guide — and if you’ve got favorite Artisan tricks (and let’s be honest, there are tons) I didn’t mention, I’d love to hear them in the comments! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Art of Laravel Models - Get the Most of Them</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Thu, 27 Feb 2025 17:49:13 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/the-art-of-laravel-models-get-the-most-of-them-3geh</link>
      <guid>https://forem.com/cyber_aurora_/the-art-of-laravel-models-get-the-most-of-them-3geh</guid>
      <description>&lt;p&gt;If you've ever worked with databases in Laravel, you've probably come across Eloquent models. They're the backbone of Laravel’s &lt;strong&gt;ORM&lt;/strong&gt;(Object-Relational Mapping) system, making database interactions smooth and intuitive. Think of models as the middleman between your database tables and your PHP code—no need to manually write SQL queries every time you need some data! 🤩&lt;/p&gt;

&lt;p&gt;With Laravel models, you can work with database records using simple and expressive PHP syntax. And the best part? They come with built-in features like &lt;strong&gt;relationships, accessors, mutators&lt;/strong&gt;, and &lt;strong&gt;query scopes&lt;/strong&gt; to make your life easier. Let’s break it down! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf7n3yzj879q55m2appk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf7n3yzj879q55m2appk.jpg" alt="first" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Basics of Laravel Models
&lt;/h2&gt;

&lt;p&gt;Every model in Laravel extends the Illuminate\Database\Eloquent\Model class, which gives it superpowers. Here’s what a basic model looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = ['name', 'email', 'password'];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A model represents a table in your database. If you follow Laravel conventions, the model User automatically maps to the users table. But if your table name doesn’t match, you can define it manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Product extends Model
{
    protected $table = 'my_products';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple, right? Now let’s dive into some best practices for handling models efficiently. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr00mto4p7f2qoi9i0532.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr00mto4p7f2qoi9i0532.jpg" alt="second" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Handle Models Efficiently?
&lt;/h2&gt;

&lt;p&gt;Before diving into your Laravel project, take a moment to set up your models correctly. A well-structured model ensures cleaner code and better performance.&lt;/p&gt;

&lt;p&gt;✅ &lt;em&gt;&lt;strong&gt;Prevent Mass Assignment Vulnerabilities&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ever heard of mass assignment? It happens when users send a big chunk of data to be saved—without any restrictions on which fields can be updated. Laravel protects you from this by using fillable and guarded attributes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using fillable (The Allowlist Approach)
&lt;/h4&gt;

&lt;p&gt;This explicitly defines which fields can be mass-assigned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    protected $fillable = ['name', 'email', 'password'];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, only the specified fields can be mass-assigned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User::create(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; 'john@example.com', 'password' =&amp;gt; 'secret']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using guarded (The Blocklist Approach)
&lt;/h4&gt;

&lt;p&gt;If you prefer to protect only a few fields, guarded is your friend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    protected $guarded = ['role'];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, all fields except role can be mass-assigned.&lt;/p&gt;

&lt;p&gt;💡 Which one should you use?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use fillable for better security as it follows an allowlist approach&lt;/li&gt;
&lt;li&gt;use guarded = [] if you want to allow all attributes by default and only protect specific ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;em&gt;&lt;strong&gt;Define Relationships Properly&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Eloquent relationships help you structure queries efficiently.&lt;/p&gt;

&lt;h4&gt;
  
  
  One-to-One (hasOne)
&lt;/h4&gt;

&lt;p&gt;A User has one Profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    public function profile()
    {
        return $this-&amp;gt;hasOne(Profile::class);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fetching the profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::find(1);
$profile = $user-&amp;gt;profile;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  One-to-Many (hasMany)
&lt;/h4&gt;

&lt;p&gt;A User can have multiple Posts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    public function posts()
    {
        return $this-&amp;gt;hasMany(Post::class);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Retrieve posts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$posts = User::find(1)-&amp;gt;posts;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out the &lt;a href="https://laravel.com/docs/12.x/eloquent-relationships#defining-relationships" rel="noopener noreferrer"&gt;docs&lt;/a&gt; on relationships for detailed informations. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9vosafrodu9g03f2nb9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9vosafrodu9g03f2nb9.jpg" alt="third" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;em&gt;&lt;strong&gt;Use Query Scopes for Reusable Queries&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Do you keep repeating the same query conditions? Query scopes allow you to define common filters inside the model itself.&lt;/p&gt;

&lt;h4&gt;
  
  
  Global Scope (applies to all queries)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('active', function ($query) {
            $query-&amp;gt;where('status', 'active');
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, User::all() only fetches active users.&lt;/p&gt;

&lt;h4&gt;
  
  
  Local Scope (applies on demand)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    public function scopeActive($query)
    {
        return $query-&amp;gt;where('status', 'active');
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft48e9ezxif5yzjsp1i80.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft48e9ezxif5yzjsp1i80.jpg" alt="fourth" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;em&gt;&lt;strong&gt;Use Accessors &amp;amp; Mutators for Data Formatting&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Need to transform data before saving or retrieving it? Accessors and mutators are here to help.&lt;/p&gt;

&lt;h4&gt;
  
  
  Accessors (Format data on retrieval)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    public function getFullNameAttribute()
    {
        return ucfirst($this-&amp;gt;first_name) . ' ' . ucfirst($this-&amp;gt;last_name);
    }
}

echo $user-&amp;gt;full_name; // John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Mutators (Modify data before storing)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    public function setPasswordAttribute($value)
    {
        $this-&amp;gt;attributes['password'] = bcrypt($value);
    }
}

$user-&amp;gt;password = 'mypassword';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The password is automatically hashed before saving. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyz0uzcikv295mkqjbv1y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyz0uzcikv295mkqjbv1y.jpg" alt="fifth" width="640" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Model Features
&lt;/h2&gt;

&lt;p&gt;Once you’re comfortable with the basics, here are some powerful model features to explore:&lt;/p&gt;

&lt;p&gt;🔹 Attribute Casting&lt;/p&gt;

&lt;p&gt;Laravel allows you to cast attributes to common data types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    protected $casts = [
        'is_admin' =&amp;gt; 'boolean',
        'created_at' =&amp;gt; 'datetime',
        'preferences' =&amp;gt; 'array',
    ];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, is_admin will always return a boolean, and preferences will be an array when retrieved.&lt;/p&gt;

&lt;p&gt;🔹 Soft Deletes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, deleted records stay in the database and can be restored!&lt;/p&gt;

&lt;p&gt;🔹 Custom Model Events&lt;/p&gt;

&lt;p&gt;Want to run logic when a model is created or updated? Use model events!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
    protected static function boot()
    {
        parent::boot();
        static::created(function ($user) {
            Mail::to($user-&amp;gt;email)-&amp;gt;send(new WelcomeEmail());
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔹 Observers for Better Model Event Handling&lt;/p&gt;

&lt;p&gt;Instead of defining event logic inside the model, you can use observers to keep your models clean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserObserver
{
    public function created(User $user)
    {
        Mail::to($user-&amp;gt;email)-&amp;gt;send(new WelcomeEmail());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Register the observer inside AppServiceProvider.php. Now, every time a user is created, an email will be sent. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feufz2brjtko538h2isii.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feufz2brjtko538h2isii.jpg" alt="sixth" width="640" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Laravel models are incredibly powerful, but they can be tricky if not used properly. Follow best practices, avoid common pitfalls, and use Laravel’s built-in features to keep your code clean and efficient.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel 12: What’s New and Why It’s Awesome!</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Tue, 25 Feb 2025 02:27:58 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/laravel-12-whats-new-and-why-its-awesome-2hn0</link>
      <guid>https://forem.com/cyber_aurora_/laravel-12-whats-new-and-why-its-awesome-2hn0</guid>
      <description>&lt;p&gt;Hey there, Laravel fan! 🎉 Laravel 12 has arrived, and it’s packed with exciting updates that make your life as a developer even easier. But don’t worry, no crazy changes that will break your app! Let’s dive in and see what’s new. &lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ Say Hello to Brand New Starter Kits!
&lt;/h2&gt;

&lt;p&gt;Imagine you’re about to build a brand-new Laravel app. Wouldn’t it be amazing if Laravel already set up the frontend for you? Well, guess what? Laravel 12 gives you shiny new starter kits for React, Vue, and Livewire! &lt;/p&gt;

&lt;p&gt;These &lt;a href="https://laravel.com/docs/12.x/starter-kits#available-starter-kits" rel="noopener noreferrer"&gt;starter kits&lt;/a&gt; come pre-packaged with awesome tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;React &amp;amp; Vue Kits → Powered by Inertia 2, TypeScript, Tailwind CSS, and shadcn/ui.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Livewire Kit → Uses the sleek Flux UI and Laravel Volt for fast, dynamic interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These kits already include authentication (login, registration, password reset, email verification), so you don’t have to build it from scratch. &lt;/p&gt;

&lt;p&gt;Oh, and there’s an extra cool version that supports social logins, passkeys, and Single Sign-On (SSO) through WorkOS AuthKit. It’s free for apps with under 1 million users! &lt;/p&gt;

&lt;p&gt;⛔ What about &lt;strong&gt;Breeze&lt;/strong&gt; and &lt;strong&gt;Jetstream&lt;/strong&gt;? Laravel’s old starter kits are retiring, so these new kits are the way forward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffanovn6ur893wzwsuifp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffanovn6ur893wzwsuifp.jpg" alt="first" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔄 No Major Breakage—Just Smooth Sailing!
&lt;/h2&gt;

&lt;p&gt;Upgrading to Laravel 12 is easy! Unlike some past versions, Laravel 12 keeps breaking changes to a minimum. That means you can upgrade without worrying about your existing code suddenly crashing. &lt;/p&gt;

&lt;p&gt;Laravel 12 is all about improving your developer experience without forcing you to rewrite everything. Simple, right? &lt;/p&gt;

&lt;h2&gt;
  
  
  🔧 Keeping Things Fresh with Upstream Updates
&lt;/h2&gt;

&lt;p&gt;Laravel 12 also updates important dependencies to keep things secure and up-to-date. While you might not see these changes directly, they help your app run faster, smoother, and safer. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmnnmzadhdu9u8nrw2f01.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmnnmzadhdu9u8nrw2f01.jpg" alt="second" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🆕 Other Exciting Changes in Laravel 12
&lt;/h2&gt;

&lt;p&gt;🏎️ Faster String Matching&lt;/p&gt;

&lt;p&gt;The Str::is() method now supports multiline string matching, making pattern matching way more powerful.&lt;/p&gt;

&lt;p&gt;📌 Better Schema Dumps for MariaDB&lt;/p&gt;

&lt;p&gt;Laravel 12 now natively supports MariaDB schema dumping.&lt;/p&gt;

&lt;p&gt;🌍 New ResponseFactory::streamJson() Method&lt;/p&gt;

&lt;p&gt;Want to send a huge JSON response efficiently? Laravel 12 introduces streamJson(), which helps in handling large JSON responses without using too much memory. &lt;/p&gt;

&lt;p&gt;🔢 Improved Numeric Key Handling in Validation&lt;/p&gt;

&lt;p&gt;Laravel 12 preserves numeric keys at the first level of validation arrays.&lt;/p&gt;

&lt;p&gt;🔑 UUID v7 for Model IDs&lt;/p&gt;

&lt;p&gt;Laravel now uses UUID v7 for model IDs, making them more unique, scalable, and efficient!&lt;/p&gt;

&lt;p&gt;⚡ Faster Hashing with xxhash&lt;/p&gt;

&lt;p&gt;Laravel replaces md5 with xxhash for hashing operations.&lt;/p&gt;

&lt;p&gt;🔀 Improved mergeIfMissing() for Arrays&lt;/p&gt;

&lt;p&gt;You can now merge nested arrays more effectively when handling complex data structures.&lt;/p&gt;

&lt;p&gt;📋 Better Query Chunking&lt;/p&gt;

&lt;p&gt;Chunking now respects user-defined limits and offsets, making bulk data operations more precise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx00knzw4lwc1ve037e5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx00knzw4lwc1ve037e5.jpg" alt="third" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Why Laravel 12 is a Game-Changer
&lt;/h2&gt;

&lt;p&gt;So, why should you be excited about Laravel 12? Here’s the TL;DR:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New Starter Kits that set up your frontend in seconds.&lt;/li&gt;
&lt;li&gt;Minimal Breaking Changes so upgrading is a breeze.&lt;/li&gt;
&lt;li&gt;Performance Boosts and better security.&lt;/li&gt;
&lt;li&gt;Better Authentication Options with social logins and SSO.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, Laravel 12 makes it faster, easier, and more fun to build apps. So what are you waiting for? Go update your projects and start coding! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>laravel</category>
      <category>news</category>
    </item>
    <item>
      <title>The Art of Database Migrations in Laravel -- Common Mistakes</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Tue, 11 Feb 2025 09:37:36 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/the-art-of-database-migrations-in-laravel-common-mistakes-3kkc</link>
      <guid>https://forem.com/cyber_aurora_/the-art-of-database-migrations-in-laravel-common-mistakes-3kkc</guid>
      <description>&lt;h2&gt;
  
  
  What are database migrations in Laravel?
&lt;/h2&gt;

&lt;p&gt;If you've ever worked with databases, you know how tricky it can be to keep track of schema changes—especially when working on a team. That’s where Laravel migrations come in! Think of them as version control for your database—just like Git for your code.&lt;/p&gt;

&lt;p&gt;Instead of manually writing SQL commands every time you make a change, migrations allow you to define your database structure in PHP. And the best part? You can roll back changes if needed! 🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82w3qlg2i4je1vednh5d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82w3qlg2i4je1vednh5d.jpg" alt="one" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every migration file in Laravel contains two methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;up() → defines what should happen (e.g., creating tables, adding columns, etc.)&lt;/li&gt;
&lt;li&gt;down() → reverses the changes (e.g., dropping tables)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s what a basic migration file looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::create('users', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('username');
            $table-&amp;gt;string('email')-&amp;gt;unique();
            $table-&amp;gt;timestamp('email_verified_at')-&amp;gt;nullable();
            $table-&amp;gt;string('password');
            $table-&amp;gt;rememberToken();
            $table-&amp;gt;timestamps();
        });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;Schema builder&lt;/em&gt; makes it super easy to define tables and columns in a clean, readable way.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle them efficiently?
&lt;/h2&gt;

&lt;p&gt;Before you start writing migrations, it’s a good idea to plan out your database schema. You can sketch it out on paper, but if you want a digital solution, here are some great tools:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://drawsql.app" rel="noopener noreferrer"&gt;DrawSQL&lt;/a&gt;, &lt;br&gt;
&lt;a href="https://www.lucidchart.com/pages/" rel="noopener noreferrer"&gt;Lucidchart&lt;/a&gt;, &lt;br&gt;
&lt;a href="https://www.dbdiagram.io/home" rel="noopener noreferrer"&gt;dbdiagram.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you have a plan, creating migrations in Laravel is super simple thanks to Artisan, Laravel’s command-line tool. Just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:migration create_users_table
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6wsknmm4tdnx8yptlwa.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6wsknmm4tdnx8yptlwa.jpg" alt="two" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the best practices?
&lt;/h2&gt;

&lt;p&gt;To keep things clean and manageable, here are some best practices to follow:&lt;/p&gt;

&lt;p&gt;✅ Use clear, descriptive names → create_posts_table instead of migration1 &lt;br&gt;
✅ Keep migrations small &amp;amp; focused → One change at a time!&lt;br&gt;
✅ Use Laravel’s column modifiers → -&amp;gt;nullable(), -&amp;gt;default(), -&amp;gt;unique()  &lt;a href="https://laravel.com/docs/11.x/migrations#column-modifiers" rel="noopener noreferrer"&gt;Check out the column modifiers&lt;/a&gt;&lt;br&gt;
✅ Stick to Laravel conventions → Table names should be plural (users), while models should be singular (User) &lt;/p&gt;
&lt;h2&gt;
  
  
  How should I configure my database?
&lt;/h2&gt;

&lt;p&gt;Laravel stores database configuration settings in config/database.php, but you don’t need to touch that much. The real magic happens in your .env file. Here’s what you need to configure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_app
DB_USERNAME=root
DB_PASSWORD=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What are advanced migrations?
&lt;/h2&gt;

&lt;p&gt;Once you’re comfortable with basic migrations, you can take things up a notch with advanced migrations. These include:&lt;/p&gt;

&lt;p&gt;🔹 Modifying column types &lt;br&gt;
🔹 Adding database triggers&lt;br&gt;
🔹 Using raw SQL inside migrations&lt;/p&gt;

&lt;p&gt;Example: Changing a column type in a migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::table('users', function (Blueprint $table) {
    $table-&amp;gt;string('username', 100)-&amp;gt;change();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0f8p1l7sqxaz9lozwul.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0f8p1l7sqxaz9lozwul.jpg" alt="three" width="640" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Issues and How to Fix Them
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1️⃣ Foreign Key Issues: Referencing a Table That Doesn't Exist Yet&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you create a table that references another table before that table exists, Laravel will throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::create('orders', function (Blueprint $table) {
    $table-&amp;gt;id();
    $table-&amp;gt;foreignId('user_id')-&amp;gt;constrained(); // This will fail if 'users' isn't created yet!
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure the referenced table is created before you reference it!&lt;/p&gt;

&lt;p&gt;✔ Adjust migration order – Ensure the users table migration runs before orders.&lt;br&gt;
✔ Use unsignedBigInteger instead of constrained() and manually define the foreign key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::create('orders', function (Blueprint $table) {
    $table-&amp;gt;id();
    $table-&amp;gt;unsignedBigInteger('user_id');
    $table-&amp;gt;foreign('user_id')-&amp;gt;references('id')-&amp;gt;on('users')-&amp;gt;onDelete('cascade');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2️⃣ "Badly Constructed Foreign Key Constraint" Error&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Foreign key constraints must match the exact data type of the referenced column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::create('posts', function (Blueprint $table) {
    $table-&amp;gt;id();
});

Schema::create('comments', function (Blueprint $table) {
    $table-&amp;gt;integer('post_id')-&amp;gt;unsigned(); // Wrong data type, should be unsignedBigInteger
    $table-&amp;gt;foreign('post_id')-&amp;gt;references('id')-&amp;gt;on('posts')-&amp;gt;onDelete('cascade');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ Use unsignedBigInteger() for foreign keys instead of integer():&lt;br&gt;
&lt;code&gt;$table-&amp;gt;unsignedBigInteger('post_id');&lt;/code&gt;&lt;br&gt;
✔ Or an even better approach use the foreignId()&lt;br&gt;
&lt;code&gt;$table-&amp;gt;foreignId('post_id')-&amp;gt;constrained()-&amp;gt;onDelete('cascade');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3️⃣ "Cannot Add Column - Table Already Exists" Error&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you try running php artisan migrate after modifying a migration file without rolling back first, Laravel won’t apply the changes.&lt;/p&gt;

&lt;p&gt;✔ If the migration has already run, you can create a new migration for the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:migration add_bio_to_users_table

Schema::table('users', function (Blueprint $table) {
    $table-&amp;gt;text('bio')-&amp;gt;nullable();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ Rollback the existing migration before modifying&lt;br&gt;
✔ Use php artisan migrate:fresh command with every migration, it drops all tables before migrating again&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4️⃣ "Class Not Found" Error When Running Migrations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Laravel sometimes doesn’t recognize migration files, especially after renaming them manually.&lt;/p&gt;

&lt;p&gt;✔ Run dump-autoload to refresh class loading.&lt;br&gt;
✔ Run the migrations with the --path option&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fht48edpmq7u2594qdbis.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fht48edpmq7u2594qdbis.jpg" alt="four" width="640" height="850"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Indexes: Boosting Query Performance
&lt;/h2&gt;

&lt;p&gt;Indexes are &lt;strong&gt;super important&lt;/strong&gt; when working with large datasets. They speed up database queries by allowing the database engine to locate data without scanning the entire table.&lt;/p&gt;
&lt;h3&gt;
  
  
  When to use indexes?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;if you frequently query or filter by a column (WHERE email = ?)&lt;/li&gt;
&lt;li&gt;when sorting (ORDER BY created_at)&lt;/li&gt;
&lt;li&gt;if you use JOIN queries often&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  When NOT to use indexes?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;if the table is small (less than 1,000 rows)&lt;/li&gt;
&lt;li&gt;on columns with highly unique values (e.g., timestamps)&lt;/li&gt;
&lt;li&gt;if the column changes frequently (indexes slow down inserts/updates)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;string('email')-&amp;gt;unique();
$table-&amp;gt;unique(['first_name', 'last_name']);
$table-&amp;gt;string('category')-&amp;gt;index();
$table-&amp;gt;text('description')-&amp;gt;fullText();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Database Events
&lt;/h2&gt;

&lt;p&gt;Database events allow you to automate certain tasks at the database level. These can be useful for automatically updating timestamps, performing scheduled tasks, logging changes without needing Laravel code.&lt;/p&gt;
&lt;h3&gt;
  
  
  Using Database Triggers in Laravel
&lt;/h3&gt;

&lt;p&gt;A trigger is an event that fires before or after an action (insert, update, delete).&lt;br&gt;
You can use raw SQL inside a migration to create a trigger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB::unprepared('
    CREATE TRIGGER before_user_insert
    BEFORE INSERT ON users
    FOR EACH ROW
    SET NEW.created_at = NOW()
');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Handling Events with Laravel’s Job System
&lt;/h3&gt;

&lt;p&gt;Instead of using raw SQL, Laravel’s event-driven architecture lets you handle database events at the application level.&lt;/p&gt;

&lt;p&gt;Example: Dispatching an event when a new user is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Events\UserRegistered;

User::created(function ($user) {
    event(new UserRegistered($user));
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5i3lrwcto1qc4ugifr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5i3lrwcto1qc4ugifr7.png" alt="five" width="800" height="999"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Migrations are one of the most powerful features in Laravel. Just remember: plan first, write clean migrations, and use Laravel’s built-in tools to make your life easier.&lt;/p&gt;

&lt;p&gt;Now go forth and migrate! 🤓🧑‍💻👩‍💻&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Spatie Permissions vs Laravel Policies and Gates: Handling Role-Based Access</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Thu, 30 Jan 2025 16:50:19 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/spatie-permissions-vs-laravel-policies-and-gates-handling-role-based-access-1bdn</link>
      <guid>https://forem.com/cyber_aurora_/spatie-permissions-vs-laravel-policies-and-gates-handling-role-based-access-1bdn</guid>
      <description>&lt;p&gt;Alright, so you’re building a Laravel app and need to handle role-based access control (RBAC). You’ve probably come across Laravel’s built-in Policies and Gates, but then there's also Spatie Permissions. Which one should you go for? Let's explore our options.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What’s the Deal with Laravel Policies and Gates?
&lt;/h2&gt;

&lt;p&gt;Laravel gives you two built-in ways to handle &lt;strong&gt;authorization&lt;/strong&gt;: Gates and Policies. Think of them as lightweight ways to check if a user can do something.&lt;/p&gt;

&lt;h3&gt;
  
  
  Laravel Gates
&lt;/h3&gt;

&lt;p&gt;Gates are like &lt;em&gt;simple yes/no checkpoints&lt;/em&gt; that decide whether a user is allowed to perform an action. You define them in &lt;strong&gt;AuthServiceProvider&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Support\Facades\Gate;

Gate::define('edit-post', function (User $user, Post $post) {
    return $user-&amp;gt;id === $post-&amp;gt;user_id;
});

// then, when you want to check if a user has permission:

if (Gate::allows('edit-post', $post)) {
    // The user can edit the post
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How Gates Work Behind the Scenes
&lt;/h3&gt;

&lt;p&gt;Gates function as simple closure-based authorization checks that Laravel stores in memory during a request. When a Gate is evaluated, Laravel runs the defined logic and determines whether the action is allowed. Gates do not persist permissions in a database, meaning authorization is determined at runtime.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboxea426ov30lnkvcbrb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fboxea426ov30lnkvcbrb.jpg" alt="policies" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Laravel Policies
&lt;/h3&gt;

&lt;p&gt;Policies are like &lt;em&gt;organized rulebooks&lt;/em&gt; that group all the authorization logic for a specific model.&lt;/p&gt;

&lt;p&gt;Making a policy for the Post model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:policy PostPolicy

// now, inside PostPolicy.php, you define rules like this:

public function update(User $user, Post $post) {
    return $user-&amp;gt;id === $post-&amp;gt;user_id;
}

// and to check if a user can update a post

if ($user-&amp;gt;can('update', $post)) {
    // User can update the post
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How Policies Work Behind the Scenes
&lt;/h3&gt;

&lt;p&gt;Policies work similarly to Gates but provide a more structured approach by associating authorization rules with specific models. Laravel automatically resolves the correct policy for a model based on method naming conventions. When a policy method is invoked, Laravel checks if an authenticated user meets the defined conditions before granting or denying access.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Spatie Permissions: The Big Guns for RBAC
&lt;/h2&gt;

&lt;p&gt;Now, if you need something fancier—like full-blown role management—&lt;strong&gt;Spatie Permissions&lt;/strong&gt; has got your back. It lets you store roles and permissions in the database and manage them dynamically.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer require spatie/laravel-permission&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After running migrations, you can create roles and permissions like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

Role::create(['name' =&amp;gt; 'admin']);
Permission::create(['name' =&amp;gt; 'edit posts']);

$admin = User::find(1);
$admin-&amp;gt;assignRole('admin');
$admin-&amp;gt;givePermissionTo('edit posts');

// checking for roles and permissions is easy:

if ($user-&amp;gt;hasRole('admin')) {
    // User is an admin
}

if ($user-&amp;gt;can('edit posts')) {
    // User can edit posts
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Let’s Compare
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60x9u6ujo1n2vvxt6tuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60x9u6ujo1n2vvxt6tuo.png" alt="comparison" width="587" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. So, Which One Should You Use?
&lt;/h2&gt;

&lt;p&gt;Laravel Policies and Gates if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your app just needs simple, per-model access control&lt;/li&gt;
&lt;li&gt;you don’t need roles, just permissions&lt;/li&gt;
&lt;li&gt;you want something lightweight and baked into Laravel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spatie Permissions if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you need full-on roles and permissions stored in the database&lt;/li&gt;
&lt;li&gt;you want to assign roles dynamically without touching the code&lt;/li&gt;
&lt;li&gt;you’re building a bigger app that needs scalable RBAC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ylilv16sc1kl3i5o50x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ylilv16sc1kl3i5o50x.jpg" alt="conclusion" width="640" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;At the end of the day, both Laravel’s built-in authorization and Spatie Permissions are solid choices—it just depends on your needs. If your app is small and doesn’t need roles, stick with Laravel’s built-in tools. But if you need a flexible, database-driven RBAC system, Spatie Permissions is the way to go!&lt;/p&gt;

&lt;p&gt;Hope this clears things up! Which one are you thinking of using?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>spatie</category>
    </item>
    <item>
      <title>Seeder vs Factory: Populating Test Data in Laravel</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Mon, 20 Jan 2025 12:47:59 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/seeder-vs-factory-populating-test-data-in-laravel-2308</link>
      <guid>https://forem.com/cyber_aurora_/seeder-vs-factory-populating-test-data-in-laravel-2308</guid>
      <description>&lt;p&gt;In Laravel, managing data for development and testing is made simple with two powerful tools: &lt;strong&gt;seeders and factories&lt;/strong&gt;. Each has its own unique purpose for populating your database, ensuring you have a solid environment for development and testing. &lt;/p&gt;

&lt;p&gt;But when should you use one over the other? Or can they work together to make your workflow smoother? Let’s dive into the details!&lt;/p&gt;

&lt;p&gt;This article breaks down the differences between seeders and factories, shares real-world examples of when to use each, and offers tips to help you decide the best approach for your project. 😊&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Project Use Cases
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiji1l21238is49posa4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiji1l21238is49posa4j.png" alt="data" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Seeder ---- Predefined Data for Your Application
&lt;/h3&gt;

&lt;p&gt;Seeders are perfect when you need to populate your database with fixed or semi-fixed data that forms the foundation of your application. Think about roles, permissions, countries, or other reference data that’s essential for your app to work.&lt;/p&gt;

&lt;p&gt;Imagine you’re building an &lt;strong&gt;e-commerce platform&lt;/strong&gt;. Here’s where seeders shine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;you can set up predefined product categories like “Electronics” “Clothing” and “Books”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create fixed roles such as “Admin” “Vendor” and “Customer”&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using a seeder ensures that this crucial data is consistent and readily available in every environment—whether it’s local, staging, or production.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why Use a Seeder?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;keeps your core data consistent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;makes deployment easier by offering a single source of truth for essential data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;simplifies setting up defaults for your application&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// database/seeders/CategorySeeder.php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Category;

class CategorySeeder extends Seeder
{
    public function run()
    {
        $categories = ['Electronics', 'Clothing', 'Books'];

        foreach ($categories as $category) {
            Category::create(['name' =&amp;gt; $category]);
        }
    }
}

// Running the seeder
// php artisan db:seed --class=CategorySeeder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Factory: Generating Dynamic Test Data
&lt;/h3&gt;

&lt;p&gt;Factories are your go-to tool when you need a lot of &lt;strong&gt;random, dynamic data&lt;/strong&gt;. They’re a lifesaver for testing and development environments, helping you simulate real-world scenarios with ease.&lt;/p&gt;

&lt;p&gt;For example, in a &lt;strong&gt;blog platform&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;you can use factories to generate 500 users with random names, emails, and profile pictures&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create 1,000 blog posts with randomized titles, content, and authors&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Why Use a Factory?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;great for stress testing and performance evaluation with large datasets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;makes testing more realistic without tedious manual data entry&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;encourages quick iterations by letting you regenerate test data whenever needed&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// database/factories/UserFactory.php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
    protected $model = User::class;

    public function definition()
    {
        return [
            'name' =&amp;gt; $this-&amp;gt;faker-&amp;gt;name,
            'email' =&amp;gt; $this-&amp;gt;faker-&amp;gt;unique()-&amp;gt;safeEmail,
            'email_verified_at' =&amp;gt; now(),
            'password' =&amp;gt; bcrypt('password'), // default password
            'remember_token' =&amp;gt; Str::random(10),
        ];
    }
}

// Using the factory
// User::factory()-&amp;gt;count(500)-&amp;gt;create();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Should You Use Both?
&lt;/h2&gt;

&lt;p&gt;Absolutely! Many projects benefit from combining seeders and factories to create a complete data ecosystem. Here’s how they complement each other:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;seeders handle your app’s foundational data, like roles, categories, or system settings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;factories build on top of that, generating realistic, dynamic data for testing and simulating real-world usage&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2r0kpdstflpanmwv8uj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2r0kpdstflpanmwv8uj.png" alt="success" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;In a &lt;strong&gt;customer relationship management (CRM)&lt;/strong&gt; system:&lt;/p&gt;

&lt;p&gt;Use a seeder to set up default categories like “Lead” or “Customer,” along with predefined admin accounts.&lt;/p&gt;

&lt;p&gt;Use factories to populate the system with thousands of randomized customer profiles and interactions for testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the Right Tool
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use Seeders when your data is static, essential for your app’s functionality, or needs to remain consistent across environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Factories when you’re testing or developing and need realistic, randomized data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Combine Both to create a seamless development environment: seeders for defaults and factories for supplemental data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Seeders and factories are &lt;strong&gt;must-have&lt;/strong&gt; tools for Laravel developers. They each bring something unique to the table, and together, they’re a powerhouse for managing data. Whether you’re setting up fixed roles or generating thousands of test records, these tools ensure you’re prepared for real-world challenges.&lt;/p&gt;

&lt;p&gt;By understanding their strengths and using them wisely, you’ll save time, reduce errors, and build a more efficient development and testing workflow. So, next time you’re populating a database, you’ll know exactly which tool to reach for! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>testing</category>
    </item>
    <item>
      <title>How I Approach Coding, Learning and Self-improvement!</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Thu, 02 Jan 2025 11:34:31 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/how-i-approach-coding-learning-and-self-improvement-14la</link>
      <guid>https://forem.com/cyber_aurora_/how-i-approach-coding-learning-and-self-improvement-14la</guid>
      <description>&lt;h2&gt;
  
  
  The Background Story of My Journey
&lt;/h2&gt;

&lt;p&gt;I think when most people think about words like 'math', 'science', 'coding', hours of suffering comes into mind. Maybe feeling dump, sweating blood trying to understand the essence of it all. &lt;/p&gt;

&lt;p&gt;I'm in the lucky few, who had an &lt;strong&gt;amazing teacher&lt;/strong&gt; in primary school. He made learning an adventure. I felt like I'm in the middle of a story trying to piece it together by searching for answers, doing experiments and thinking. When I finally figured something out by myself, the rush of creating a chain of thought alone got me &lt;strong&gt;addicted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnurjqyru9xu1b2azo3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnurjqyru9xu1b2azo3l.png" alt="aiwoman" width="720" height="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With Schools In My Opinion
&lt;/h2&gt;

&lt;p&gt;What I discovered is, learning is as hard as you make it out to be. If you approach it with enthusiasm, as a way to experience more in this world, to experience walking in the street watching the trees, knowing how they make oxygen for example, seeing in your imagination the microorganisms working in the dirt, seeing the birds dance around their mate, watching the cars accelerate and seeing the invisible forces around them as vectors showing you a hidden universe only a selected, persistent few can see. &lt;/p&gt;

&lt;p&gt;Schools don't teach you that, they don't show you this amazing ability to appreciate the beauty and complexity of our existence. That one unfortunately you have to do for yourself, but believe me the reward is so much more than you can ever imagine. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzpje0u13bjklu19rym9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzpje0u13bjklu19rym9.png" alt="hiddenuniverse" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying It
&lt;/h2&gt;

&lt;p&gt;You must be wondering how coding comes into play. For me programming is the ultimate way to experience life, to experience how it would feel to create a cell for example, such a perfect system. I experience what it is like to be on the maker side of this hidden universe I was describing. To grab math, logic and create something with my brain that captures an essence of something. An essence of existence. &lt;/p&gt;

&lt;p&gt;I realise it seems dreamlike to go through life like this, how could you apply this nonsense anyway? Well, start small. Try finding this beauty in the quadratic equation. Seems crazy huh? But just imagine playing around with it, being able to see it with algebra, with geometry, with functions,  derivatives, integrals, go in deep. Don't let those technical textbooks and pages scare you. Fight for your dreamlike hidden universe. Search until you can finally see that functions aren't there to cause you headache, they are there so you can see the quadratic function in the tunnels, where they use it for designing the proper curvature. Be resilient and don't let the world tell you how you should see science! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6yseh7u7n9ti69wt9cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6yseh7u7n9ti69wt9cr.png" alt="math" width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Coding Essence of It All
&lt;/h2&gt;

&lt;p&gt;I think the proper way to embrace this mindset with coding is to see it in every way possible. See code as a mathematican, physician, business manager etc. Embrace the differences as to how math, physics and IT tells matrices to you. Don't insist on one definition, let yourself be as fluid as possible, this way you can see way more than most people. &lt;/p&gt;

&lt;p&gt;I never understood coding interviews, all those lexical knowledge. In my opinion lexicality is a game for your brain nothing more. The true purpose of your brain regarding this matter is to see the connection and logic through. If for example given a coding language before you, that you never saw in your life to be able to see the logic behind it and write code with it, yes with mistakes of course regarding the syntax and lexical stuff, but still making it happen. How awesome is that! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4pgro17tgzih0566gwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4pgro17tgzih0566gwl.png" alt="paths" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Era of Self-improvement
&lt;/h2&gt;

&lt;p&gt;The biggest kindness you can do to yourself in my opinion is being open to everything around you. Struggling with nightmares? Try a balanced diet, try gym, try meditating, try breathing exercises. Sounds cliché? Maybe. Works? Everytime. You have problems, pitfalls, trauma to work through.. why be so picky and closed off to potential improvements? Be the rock for yourself, be there for yourself and try that muscle relaxation technique you've been avoiding because it's 'bullshit'. Try that zumba class your friends bully you for because of the 40-something mothers there, try that knitting class, go watch Shrek in the cinema even if you are 50-year-old, who cares? You. The only one who cares is you, so why don't just start actively Caring FOR yourself. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueojt19hli5zq50ttpmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueojt19hli5zq50ttpmb.png" alt="self" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In The End We All Be Judged By The Courage Of Our Hearts
&lt;/h2&gt;

&lt;p&gt;Thank you for reading my post, I hope it made you feel several emotions, made you wonder and most importantly made you feel better about yourself. Start 2025 with forgiveness for yourself. Start with courage and kindness.&lt;br&gt;
I wish all of you a productive year! &lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>motivation</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Laravel Nova vs Filament: The Best Admin Panels</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Wed, 13 Nov 2024 19:25:08 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/laravel-nova-vs-filament-the-best-admin-panels-5f9a</link>
      <guid>https://forem.com/cyber_aurora_/laravel-nova-vs-filament-the-best-admin-panels-5f9a</guid>
      <description>&lt;h2&gt;
  
  
  What Are Laravel Nova and Filament?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova&lt;/strong&gt; is the official admin panel developed by the Laravel team. It integrates smoothly with any Laravel application and comes packed with handy features. Nova is known for its sleek, professional interface, ease of use, and straightforward integration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filament&lt;/strong&gt;, on the other hand, is an open-source admin panel focused on simplicity, customization, and flexibility. Filament’s component-based structure gives us lots of freedom to create a unique admin experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ease of Setup and Installation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Because Nova is a &lt;strong&gt;paid package&lt;/strong&gt;, you’ll need a license before installing it. Setup is simple: install it via Composer, run a few Artisan commands, and your admin panel is ready to go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require laravel/nova
php artisan nova:install
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament is free and open-source, so you can add it to your project with a single Composer command. Within a few minutes, you’ll have a functional admin panel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require filament/filament
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fxjrwyf6a04qlo8k5bg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fxjrwyf6a04qlo8k5bg.jpg" alt="ui" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  UI and Customization
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Nova offers a polished, ready-to-go UI that looks professional right out of the box. Customization options are plentiful, and the pre-built components make it a good fit for professional or client-facing dashboards. For example, to add a resource in Nova (like a Product manager), just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan nova:resource Product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament’s UI is minimalistic yet highly customizable, with a layout built on Tailwind CSS that you can easily adjust. If you like a hands-on approach to design and want the flexibility of Tailwind, Filament is perfect. To create a similar Product resource in Filament, you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:filament-resource Product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Learning Curve
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Nova is relatively easy to learn, especially if you’re already familiar with Laravel. Its documentation is extensive, and most tasks can be handled with a few Artisan commands. However, more advanced tweaks might require diving into Laravel's internals.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament is equally beginner-friendly, with clear documentation and a strong focus on usability. It’s especially good for you if you want customization without a steep learning curve.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F50jba2gyd0105vtxn8sv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F50jba2gyd0105vtxn8sv.jpg" alt="plugins" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Extensibility and Ecosystem
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Nova has a solid ecosystem, with plenty of packages that integrate seamlessly with other Laravel features like policies and authorization. It’s built to scale with more complex projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament’s open-source nature has sparked a growing ecosystem of plugins and community extensions. We can contribute our own plugins, leading to a rapidly expanding library of add-ons for things like role management and file uploads. For example, you can set up role-based access with Filament using &lt;em&gt;spatie/laravel-permission&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance and Scalability
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Nova is designed to handle large datasets and complex queries, making it a good choice for enterprise-level applications. However, as a commercial product, you’ll need a license per project, which can add up for larger applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament’s lightweight structure allows it to perform well, even with extensive data. Its architecture is flexible enough to handle larger datasets efficiently. For instance, Filament supports server-side processing for tables, ensuring faster load times with big data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatb75xm5xxhe2ycef5o0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatb75xm5xxhe2ycef5o0.jpg" alt="example" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Examples
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Customer Management System:&lt;/strong&gt; For a small business’s customer management system, Filament’s ease of setup and zero cost make it a great fit. You can build custom forms and actions for each business need without much hassle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enterprise Dashboard:&lt;/strong&gt; For a professional, enterprise-level dashboard (say, a property management platform for a real estate firm), Nova’s polished UI and native data handling capabilities might be more suitable, providing a premium look and feel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inventory Tracking System:&lt;/strong&gt; For a medium-sized business tracking inventory, Filament’s flexibility is ideal. You can build a custom dashboard that displays real-time inventory levels with filters to quickly locate items and manage stock.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pricing Comparison
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova:&lt;/strong&gt; Nova is a commercial product with a one-time fee of around $99 per project for solo developers. While this is a fair investment for a single project, it can add up if you’re working on multiple applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filament:&lt;/strong&gt; Filament is completely free, making it an attractive choice if you are on a budget or for projects where paid solutions are out of reach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0bj2qb46ix2pxk26whe.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0bj2qb46ix2pxk26whe.jpg" alt="snippets" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Snippets: Displaying Resources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel Nova Example:&lt;/strong&gt; Nova lets you define resources in a straightforward way. For example, here’s a simple User resource setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Resource
{
    public function fields(Request $request)
    {
        return [
            ID::make()-&amp;gt;sortable(),
            Text::make('Name')-&amp;gt;sortable(),
            Email::make('Email'),
        ];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Filament Example:&lt;/strong&gt; Filament also makes defining resources easy, but with a modern twist using Tailwind and Alpine.js. Here’s how you might set up a User resource:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserResource extends Resource
{
    protected static string $model = User::class;

    public static function form(Form $form): Form
    {
        return $form-&amp;gt;schema([
            TextInput::make('name')-&amp;gt;required(),
            TextInput::make('email')-&amp;gt;required(),
        ]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Which Should You Choose?
&lt;/h2&gt;

&lt;p&gt;At the end of the day, both Laravel Nova and Filament are fantastic choices with different strengths. Here’s a quick rundown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose Laravel Nova&lt;/strong&gt; if you need a highly polished, ready-made admin interface, solid support, and don’t mind a one-time fee.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose Filament&lt;/strong&gt; if you prefer a budget-friendly, open-source solution with flexibility and customization options.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yoj06sztl2bb06v44y6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yoj06sztl2bb06v44y6.jpg" alt="end" width="640" height="461"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Whether you go with Nova’s premium feel or Filament’s customizable approach, you’re equipped to build a powerful, user-friendly admin panel in Laravel. So go ahead, choose the best fit for you, and get building!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>filament</category>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Mix vs Vite: Why did Laravel Transitioned to Vite</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Fri, 08 Nov 2024 16:45:16 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/laravel-mix-vs-vite-why-did-laravel-transitioned-to-vite-2k25</link>
      <guid>https://forem.com/cyber_aurora_/laravel-mix-vs-vite-why-did-laravel-transitioned-to-vite-2k25</guid>
      <description>&lt;p&gt;&lt;strong&gt;Asset bundling&lt;/strong&gt; is a core part of modern web development, helping optimize and manage CSS, JavaScript, and other resources. For years, Laravel Mix streamlined this process, but as JavaScript tools evolved, Laravel has transitioned to Vite as the default in Laravel 11.&lt;/p&gt;

&lt;p&gt;So why has Laravel made this switch?&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhfu65k0vv2mx4vt35vke.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhfu65k0vv2mx4vt35vke.jpg" alt="speed" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance and Speed
&lt;/h2&gt;

&lt;p&gt;Vite focuses on two main things: development speed and production performance. If you’re tired of waiting around for builds, Vite’s instant feedback will feel like a breath of fresh air. With &lt;a href="https://v2.vitejs.dev/guide/features.html#hot-module-replacement" rel="noopener noreferrer"&gt;&lt;strong&gt;Hot Module Replacement (HMR)&lt;/strong&gt;&lt;/a&gt;, Vite’s development server delivers changes in real time, so you can see updates instantly, without a full page refresh. This immediate feedback creates a faster, more enjoyable development experience.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ulsqq25vkv5qw6hjt0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ulsqq25vkv5qw6hjt0b.png" alt="bundle based" width="550" height="297"&gt;&lt;/a&gt;&lt;br&gt;
Laravel Mix uses this approach, and it requires significant initial bundling time, resulting in a delay before the server is ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8jfd5va5nfc7sga5z0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8jfd5va5nfc7sga5z0l.png" alt="esm-based" width="544" height="332"&gt;&lt;/a&gt;&lt;br&gt;
This ESM-based approach is much faster in development, as it avoids the full bundling process. Vite uses this approach, allowing developers to see changes instantly without waiting for a full bundle.&lt;/p&gt;

&lt;p&gt;(the pictures are from Vite's &lt;a href="https://v2.vitejs.dev/guide/why.html#slow-server-start" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;For production builds, Vite uses Rollup as its bundler, optimizing and minifying your code to ensure fast performance. This allows Vite to offer the best of both worlds: no-bundle development for speed during coding and traditional bundling for optimized production builds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsje0xn7v0hl6g8qbupxy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsje0xn7v0hl6g8qbupxy.jpg" alt="modern" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Modern Javascript Support
&lt;/h2&gt;

&lt;p&gt;Vite also excels at &lt;strong&gt;tree shaking&lt;/strong&gt; and &lt;strong&gt;code splitting&lt;/strong&gt;. By leveraging &lt;strong&gt;Rollup&lt;/strong&gt;, Vite produces optimized production bundles, stripping out unused code and splitting assets as needed. This results in smaller, faster-loading bundles that enhance your app’s performance.&lt;/p&gt;
&lt;h2&gt;
  
  
  Enhanced Developer Experience
&lt;/h2&gt;

&lt;p&gt;Vite’s configuration process is refreshingly straightforward. Unlike Webpack, where configurations can become dense and complex, Vite’s &lt;em&gt;vite.config.js&lt;/em&gt; file is easy to read and customize, even if you don’t have extensive bundler experience.&lt;/p&gt;

&lt;p&gt;Plus, Vite is built to work seamlessly with popular frameworks like Vue and React. Since these frameworks also benefit from Vite’s HMR and optimized development environment, you can work more efficiently on front-end-heavy Laravel projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxbjzb0v9o48nhuz44js.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxbjzb0v9o48nhuz44js.jpg" alt="community" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Future-Proofing and Community Adoption
&lt;/h2&gt;

&lt;p&gt;The JavaScript ecosystem is evolving fast, and Vite has gained a solid foothold. It’s widely adopted and supported, with a large, active community that brings ongoing improvements and a rich resource of plugins. Vite’s design aligns well with where front-end development is headed, making it a smart choice if you want to future-proof your project.&lt;/p&gt;

&lt;p&gt;Laravel’s decision to adopt Vite shows a clear alignment with modern standards, positioning your Laravel projects to stay compatible with the latest front-end technologies.&lt;/p&gt;
&lt;h2&gt;
  
  
  Laravel Integration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Laravel’s Vite Plugin&lt;/strong&gt; includes handy helper functions like @vite for Blade templates, automatically handling asset versioning and cache busting. With just @vite('resources/css/app.css'), you can link your CSS file without worrying about stale cache issues.&lt;/p&gt;

&lt;p&gt;Here’s how you can set up vite.config.js for a Laravel + Vue project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then in the blade:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    @vite('resources/css/app.css')
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Hello, Vite!&amp;lt;/h1&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with Laravel’s official support and documentation now focused on Vite, you’ll find consistent guidance and resources to help you get the most out of this tool.&lt;/p&gt;




&lt;p&gt;In summary, Vite brings the speed, simplicity, and future-ready features that modern Laravel projects need. By making Vite the default in Laravel 11, Laravel is empowering developers to build faster, leaner, and more efficient applications, especially as JavaScript and web development standards continue to evolve. Dive into Laravel’s official &lt;a href="https://laravel.com/docs/11.x/vite" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for more insights and get started with Vite today!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>performance</category>
    </item>
    <item>
      <title>Laravel VS Symfony: The PHP Framework Showdown</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Tue, 05 Nov 2024 17:34:49 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/laravel-vs-symfony-the-php-framework-showdown-269j</link>
      <guid>https://forem.com/cyber_aurora_/laravel-vs-symfony-the-php-framework-showdown-269j</guid>
      <description>&lt;p&gt;When you're starting a new web development project, picking the right PHP framework can set the tone for success. Two of the biggest contenders in the PHP world are Laravel and Symfony. Each brings its own flavor, strengths, and quirks to the table. In this post, we'll explore the key differences between Laravel and Symfony, with real-world examples to help you decide which framework is the best fit for your next project.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fne9wicvvxtsgv0c6i5b2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fne9wicvvxtsgv0c6i5b2.jpg" alt="one" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview: What Sets Them Apart?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;Think of Laravel as the framework that wants to be your best friend. It's all about making your life easier with its &lt;strong&gt;elegant syntax&lt;/strong&gt; and &lt;strong&gt;developer-friendly features&lt;/strong&gt;. From handling routing and authentication to setting up caching, Laravel simplifies the common tasks that can bog you down.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Real-life example:&lt;/u&gt; If you've ever used a SaaS product like &lt;a href="https://invoiceninja.com" rel="noopener noreferrer"&gt;Invoice Ninja&lt;/a&gt;, you've interacted with an application built on Laravel. Its ease of use and rapid development capabilities make it perfect for startups looking to get their MVP off the ground quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;On the other hand, Symfony is like the Swiss Army knife of PHP frameworks. It's designed for &lt;strong&gt;flexibility&lt;/strong&gt; and &lt;strong&gt;scalability&lt;/strong&gt;, letting you pick and choose the components you need. This modular approach makes it a favorite for &lt;strong&gt;complex, enterprise-level projects&lt;/strong&gt;.&lt;br&gt;
&lt;u&gt;&lt;br&gt;
Real-life example:&lt;/u&gt; Many enterprise-level systems, like &lt;a href="https://www.drupal.org" rel="noopener noreferrer"&gt;Drupal&lt;/a&gt;, utilize Symfony components. When you're building a platform that needs to handle large amounts of data and users, Symfony's robust architecture comes in handy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ynqjcrj3zhso30oohr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ynqjcrj3zhso30oohr.jpg" alt="two" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture: The Foundation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;Laravel sticks to the &lt;strong&gt;MVC (Model-View-Controller) architecture&lt;/strong&gt;, keeping your code organized and maintainable. Its built-in &lt;strong&gt;ORM, Eloquent&lt;/strong&gt;, makes interacting with databases a breeze. You write your queries in a way that feels natural, almost like you're talking to your database.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example in practice:&lt;/u&gt; Say you're building a social networking app. Laravel's Eloquent ORM can help you quickly set up relationships between users, posts, and comments, letting you focus more on the app's features and less on the database interactions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;Symfony goes for a &lt;strong&gt;component-based architecture&lt;/strong&gt;, meaning you can use its individual components without committing to the full framework. Its &lt;strong&gt;ORM of choice, Doctrine,&lt;/strong&gt; is incredibly powerful but might feel a bit more complex at first.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example in practice:&lt;/u&gt; If you're developing a custom CRM system where each client has unique business rules, Symfony's flexibility allows you to tailor the architecture precisely to those needs. You can build only what you need, without unnecessary overhead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2v9ekgjsno1f05hdgh0m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2v9ekgjsno1f05hdgh0m.jpg" alt="three" width="640" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance: Speed Matters
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;While Laravel has made strides in performance, it can be slightly slower due to its many features and abstractions. That said, it's more than capable of handling medium-sized applications with ease.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;When to choose Laravel:&lt;/u&gt; For a &lt;strong&gt;blog&lt;/strong&gt; or an &lt;strong&gt;e-commerce site&lt;/strong&gt; where development speed is crucial, Laravel provides the tools to get you up and running quickly. E-commerce platforms like &lt;a href="https://bagisto.com/en/" rel="noopener noreferrer"&gt;Bagisto&lt;/a&gt; are built on Laravel and manage to strike a good balance between performance and usability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;Symfony, known for its performance, especially shines in large-scale applications. Its optimization for production environments can make a noticeable difference when handling heavy loads.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;When to choose Symfony:&lt;/u&gt; Think of a large enterprise resource planning (ERP) system that needs to process massive amounts of data in real-time. Symfony’s efficient use of resources and speed in handling complex tasks makes it a go-to choice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24drcn9v5yqud8ar7jtt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24drcn9v5yqud8ar7jtt.jpg" alt="four" width="640" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Curve: Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Beginner-friendly and approachable&lt;/strong&gt;, Laravel comes with a ton of documentation and tutorials that make it easier for new developers to jump in.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Real-world scenario:&lt;/u&gt; Imagine you're a junior developer tasked with building an internal tool for your company. Laravel's intuitive syntax and supportive community help you get the job done without feeling overwhelmed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;Symfony, while powerful, has a steeper learning curve. Its extensive configuration options can be daunting at first, but for those willing to invest the time, it offers unmatched control and flexibility.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Real-world scenario:&lt;/u&gt; As a seasoned developer at a fintech company, you might need to build a secure, highly-customizable system. Symfony’s comprehensive documentation and fine-tuned control are invaluable for such projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1iumjsm4n84ezuayhdjf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1iumjsm4n84ezuayhdjf.jpg" alt="five" width="640" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases: Where They Shine
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Rapid Development:&lt;/strong&gt; Laravel is perfect for quickly building Minimum Viable Products (MVPs), small to medium-sized applications, or RESTful APIs.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example:&lt;/u&gt; A startup looking to launch a job board might choose Laravel for its simplicity and speed, allowing them to focus on user experience and features rather than complex backend configurations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Symfony
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Enterprise-Level Applications:&lt;/strong&gt; Symfony is best for large-scale, enterprise applications where performance, scalability, and customization are key.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Example:&lt;/u&gt; A multinational corporation might use Symfony to build an internal HR system that needs to integrate with various external services, handle massive user data, and ensure security.&lt;/p&gt;




&lt;p&gt;Both Laravel and Symfony are powerful frameworks, but the right choice depends on your project needs and team's expertise. If you're aiming for rapid development and ease of use, Laravel might be your best bet. For complex, scalable projects that require fine-tuned control, Symfony is a strong contender.&lt;/p&gt;

&lt;p&gt;Remember, there's no one-size-fits-all answer. Consider the nature of your project, your team’s skill level, and long-term maintenance when making your decision. Whatever you choose, both frameworks offer solutions for modern web development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>laravel</category>
      <category>symfony</category>
    </item>
    <item>
      <title>Get The Most From Blade: Laravel's Templating Engine</title>
      <dc:creator>ZoZo</dc:creator>
      <pubDate>Tue, 05 Nov 2024 14:30:32 +0000</pubDate>
      <link>https://forem.com/cyber_aurora_/get-the-most-from-blade-laravels-templating-engine-2pc5</link>
      <guid>https://forem.com/cyber_aurora_/get-the-most-from-blade-laravels-templating-engine-2pc5</guid>
      <description>&lt;h2&gt;
  
  
  What is a Templating Engine?
&lt;/h2&gt;

&lt;p&gt;A templating engine is like a tool that helps you keep your content and layout separate. This makes your code cleaner and easier to manage. Instead of mixing HTML with your data, you create templates that define how your content should look, and the engine takes care of filling in the details.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Blade?
&lt;/h2&gt;

&lt;p&gt;Blade is Laravel’s own templating engine, and it’s designed to make your life easier. Blade templates are stored in the resources/views directory, and each one has a .blade.php extension. The syntax is simple and clean, allowing you to mix HTML with PHP effortlessly. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;Hello, {{ $name }}!&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Blade isn’t just for displaying data. You can also add logic, like loops and conditionals, right in your templates. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if ($user)
    &amp;lt;p&amp;gt;Welcome, {{ $user-&amp;gt;name }}!&amp;lt;/p&amp;gt;
@else
    &amp;lt;p&amp;gt;Please log in.&amp;lt;/p&amp;gt;
@endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See how easy it is to show different content based on whether a user is logged in? Next time you need to loop through a list of users, try using Blade’s &lt;a class="mentioned-user" href="https://dev.to/foreach"&gt;@foreach&lt;/a&gt; directive. It’s straightforward and keeps your code tidy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6fb34vv9jf9gjb8ugwcj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6fb34vv9jf9gjb8ugwcj.jpg" alt="first" width="640" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Template Inheritance
&lt;/h2&gt;

&lt;p&gt;One of Blade’s best features is how it helps you reuse layouts. You can create a master template for your site and then just fill in the unique content for each page. Here’s a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- layout.blade.php --&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;@yield('title')&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="header"&amp;gt;My Website&amp;lt;/div&amp;gt;
    &amp;lt;div class="content"&amp;gt;
        @yield('content')
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="footer"&amp;gt;Footer Information&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This layout has placeholders (@yield) for the title and the main content. Now, let’s say you’re creating a home page. You can extend this layout like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@extends('layout')

@section('title', 'Home Page')

@section('content')
    &amp;lt;h1&amp;gt;Welcome to the Home Page!&amp;lt;/h1&amp;gt;
@endsection
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using @extends, you link to the layout, and &lt;a class="mentioned-user" href="https://dev.to/section"&gt;@section&lt;/a&gt; allows you to fill in the placeholders with your specific content. This keeps your code DRY (Don’t Repeat Yourself) and super manageable. Blade simplifies your workflow, allowing you to focus more on what matters—building great web applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9uo4jy1uybfaxplcy6gh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9uo4jy1uybfaxplcy6gh.png" alt="second" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Blade Components
&lt;/h2&gt;

&lt;p&gt;Blade components are like little building blocks for your UI. Imagine them as Lego pieces—you create a small, reusable part of your interface and can snap it into place wherever you need it. This makes your code cleaner and more maintainable.&lt;/p&gt;

&lt;p&gt;You can define a component once and use it throughout your application. Need a button that looks the same across different pages? Create a Blade component for it! Even better, you can pass attributes to these components to make them flexible and adaptable.&lt;/p&gt;

&lt;p&gt;Here’s a simple example of a button component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- resources/views/components/button.blade.php --&amp;gt;
&amp;lt;button&amp;gt;{{ $slot }}&amp;lt;/button&amp;gt;

&amp;lt;!-- Usage --&amp;gt;
&amp;lt;x-button&amp;gt;Click Me&amp;lt;/x-button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make your components dynamic by using a component class. This lets you pass in attributes like type or class to customize the button’s behavior or style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In a component class
public function render()
{
    return view('components.button', [
        'type' =&amp;gt; $this-&amp;gt;type,
        'class' =&amp;gt; $this-&amp;gt;class,
    ]);
}

// In the Blade component
&amp;lt;button type="{{ $type }}" class="{{ $class }}"&amp;gt;{{ $slot }}&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Including Subviews
&lt;/h2&gt;

&lt;p&gt;Sometimes, you’ll want to break your templates into smaller pieces for better organization and reusability. Blade makes this easy with the &lt;a class="mentioned-user" href="https://dev.to/include"&gt;@include&lt;/a&gt; directive. Think of it as a way to insert a smaller view (or subview) into a larger one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspr93d4ewfahvbeuofzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspr93d4ewfahvbeuofzi.png" alt="third" width="640" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Blade Directives
&lt;/h2&gt;

&lt;p&gt;Blade comes packed with handy directives that make common tasks a breeze. Here are a few:&lt;br&gt;
@csrf: CSRF token to your forms, protecting them from cross-site request forgery attacks&lt;br&gt;
@method: specifies the HTTP method for forms&lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/auth"&gt;@auth&lt;/a&gt;: checks if a user is authenticated&lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/guest"&gt;@guest&lt;/a&gt;: checks if a user is a guest (not authenticated)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="/submit" method="POST"&amp;gt;
    @csrf
    @method('PUT')
    &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Need something more customized? You can create your own Blade directives for reusable logic.&lt;/p&gt;

&lt;p&gt;For instance, let’s say you often need to format dates. You can define a custom directive like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In a service provider
Blade::directive('datetime', function ($expression) {
    return "&amp;lt;?php echo ($expression)-&amp;gt;format('Y-m-d H:i:s'); ?&amp;gt;";
});

// Usage in Blade
@datetime($dateVariable)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckagbpsykglwybescgjl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckagbpsykglwybescgjl.jpg" alt="four" width="640" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Features
&lt;/h2&gt;

&lt;p&gt;Blade comes with some really handy features that make your life as a developer smoother. Let’s dive into a few of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Managing Asset URls
&lt;/h3&gt;

&lt;p&gt;Need to link your CSS or JavaScript files? The asset() helper function has you covered. It generates the correct URL for your assets, so you don’t have to worry about paths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="{{ asset('css/app.css') }}"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Handling Empty Arrays or Collections
&lt;/h3&gt;

&lt;p&gt;Blade’s @forelse directive is a lifesaver when dealing with empty arrays or collections. It lets you loop through items and also handles the case where there are no items elegantly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@forelse ($items as $item)
    &amp;lt;p&amp;gt;{{ $item }}&amp;lt;/p&amp;gt;
@empty
    &amp;lt;p&amp;gt;No items found.&amp;lt;/p&amp;gt;
@endforelse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conditional Content Display
&lt;/h3&gt;

&lt;p&gt;Blade offers several directives to show content based on conditions:&lt;/p&gt;

&lt;p&gt;@isset: checks if a variable is set&lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/empty"&gt;@empty&lt;/a&gt;: checks if a variable is empty&lt;br&gt;
@unless: works like if, but in reverse&lt;br&gt;
Here’s an example using @isset:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@isset($variable)
    &amp;lt;p&amp;gt;{{ $variable }}&amp;lt;/p&amp;gt;
@endisset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Protecting Against XSS
&lt;/h3&gt;

&lt;p&gt;Blade automatically escapes output to protect your app from XSS (Cross-Site Scripting) attacks. But sometimes, you might want to output raw HTML. In that case, use {!! !!}:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{!! $htmlContent !!}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuap4m1d12q9rxrxl3h7o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuap4m1d12q9rxrxl3h7o.jpg" alt="fifth" width="640" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced use
&lt;/h2&gt;

&lt;p&gt;Need to include raw HTML or JavaScript that contains Blade syntax? Use the @verbatim directive to stop Blade from trying to parse it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@verbatim
    &amp;lt;script&amp;gt;
        var example = '{{ $variable }}'; // This will not be parsed by Blade
    &amp;lt;/script&amp;gt;
@endverbatim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Working with APIs? Blade makes it easy to render JSON data directly in your templates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    var data = @json($data);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re using Livewire, Blade works seamlessly with it. You can use Blade components alongside Livewire components for a dynamic, interactive UI without writing much JavaScript.&lt;/p&gt;

&lt;p&gt;The @once directive ensures a block of code runs only one time. Blade lets you create dynamic components that adapt based on the data you pass. This is great for flexible, reusable UI pieces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;x-dynamic-component :component="$componentName" :data="$data" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a class="mentioned-user" href="https://dev.to/error"&gt;@error&lt;/a&gt; directive helps you show error messages for specific fields easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" name="username"&amp;gt;
@error('username')
    &amp;lt;div class="alert alert-danger"&amp;gt;{{ $message }}&amp;lt;/div&amp;gt;
@enderror
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re using Laravel Mix for compiling assets, you can effortlessly integrate the compiled files into your Blade templates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="{{ mix('css/app.css') }}"&amp;gt;
&amp;lt;script src="{{ mix('js/app.js') }}"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;When I first started using Blade, I was a little lost on how many options I have, but shortly after a whole world opened up for me. Now I can't imagine coding without it's versatile features. I hope this article helped you on finding your way into this amazing templating engine.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>laravel</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
