<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laravel 10 Archives - Codeplaners</title>
	<atom:link href="https://codeplaners.com/tag/laravel-10/feed/" rel="self" type="application/rss+xml" />
	<link>https://codeplaners.com/tag/laravel-10/</link>
	<description>Code Solution</description>
	<lastBuildDate>Tue, 24 Jun 2025 12:00:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>

<image>
	<url>https://codeplaners.com/wp-content/uploads/2020/09/cropped-favicon-social-32x32.png</url>
	<title>Laravel 10 Archives - Codeplaners</title>
	<link>https://codeplaners.com/tag/laravel-10/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to send Real-Time Notifications using Pusher Example in Laravel 12</title>
		<link>https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/</link>
					<comments>https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 24 Jun 2025 12:00:07 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 12]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1763</guid>

					<description><![CDATA[<p>Hi Dev, In this article, I will show you how to send real-time notification with Pusher in the laravel 12. we will use a pusher driver to send real-time notification using the echo server in the laravel 12 What is Pusher? Pusher: Pusher lets apps talk to each other in real time — like live &#8230; <a href="https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/" class="more-link">Continue reading<span class="screen-reader-text"> "How to send Real-Time Notifications using Pusher Example in Laravel 12"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/">How to send Real-Time Notifications using Pusher Example in Laravel 12</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi Dev,</p>
<p>In this article, I will show you how to send real-time notification with Pusher in the laravel 12. we will use a pusher driver to send real-time notification using the echo server in the laravel 12</p>
<p><strong> What is Pusher?</strong><br />
<strong>Pusher: </strong><br />
Pusher lets apps talk to each other in real time — like live chat or instant updates on your screen.</p>
<p>It uses WebSockets (a fast, always-on connection) so information flows instantly, not by checking every few seconds.</p>
<p>Think of it as a messenger service: your server sends a message using Pusher, and all connected users get it right away.</p>
<p><strong>Here’s what we’ll build in this example:</strong></p>
<p>1. Create authentication scaffolding using Laravel UI to get login/register dashboards up and running.<br />
2. Set up two user roles: a super admin and a normal user, distinguished via an is_admin flag.<br />
3. Define a posts table with title and body columns, and create corresponding Eloquent models.<br />
4. Enable users to create posts through a form, saving them via a controller.<br />
5. Fire a broadcast event (PostCreated) when a post is saved—this triggers a real-time notification via Pusher to admins.</p>
<p><strong>Step 1:</strong> Install Laravel 12<br />
<strong>Step 2:</strong> Create Auth using Scaffold<br />
<strong>Step 3:</strong> Create Migrations<br />
<strong>Step 4:</strong> Create and Update Models<br />
<strong>Step 5:</strong> Create Pusher App<br />
<strong>Step 6:</strong> Setup Pusher &#038; Echo Server<br />
<strong>Step 7:</strong> Create PostCreate Event<br />
<strong>Step 8:</strong> Create Routes<br />
<strong>Step 9:</strong> create Controller<br />
<strong>Step 10:</strong> create Blade Files<br />
<strong>Step 11:</strong> Create admin User </p>
<h3 class="step_code">Install Laravel 12</h3>
<p>For Installing Laravel application 12 run below command. let&#8217;s start:</p>
<pre class="brush: php; title: ; notranslate">
composer create-project laravel/laravel example-app
</pre>
<h3 class="step_code"> Create Auth using Scaffold</h3>
<p>Now, we will create an auth scaffold command to generate login, register, and dashboard functionalities. So, run the below commands:</p>
<p><strong>Laravel 12 UI Package:</strong></p>
<pre class="brush: php; title: ; notranslate">
composer require laravel/ui 
</pre>
<p><strong>Generate Auth:</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan ui bootstrap --auth 
</pre>
<pre class="brush: php; title: ; notranslate">
npm install
</pre>
<pre class="brush: php; title: ; notranslate">
npm run build
</pre>
<h3 class="step_code">Create Migrations</h3>
<p>Here, we will create posts and add is_admin column to users table. So, run the below commands: </p>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration add_is_admin_column_table
</pre>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration create_posts_table
</pre>
<p><strong>database/migrations/2025_06_23_100454_create_posts_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table(&#039;users&#039;, function (Blueprint $table) {
            $table-&gt;tinyInteger(&#039;is_admin&#039;)-&gt;default(0);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        //
    }
};
</pre>
<p><strong>database/migrations/2025_06_23_100442_add_is_admin_column_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create(&#039;posts&#039;, function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;foreignId(&#039;user_id&#039;)-&gt;constrained()-&gt;onDelete(&#039;cascade&#039;);
            $table-&gt;string(&#039;title&#039;);
            $table-&gt;text(&#039;body&#039;);
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists(&#039;posts&#039;);
    }
};
</pre>
<p>now, Let&#8217;s run the below command:</p>
<pre class="brush: php; title: ; notranslate">
php artisan migrate
</pre>
<h3 class="step_code"> Create and Update Models</h3>
<p>Now, we will create model using below command. we also need to update User model here. we will write relationship and some model function for like and dislike.</p>
<pre class="brush: php; title: ; notranslate">
php artisan make:model Post
</pre>
<p><strong>app/Models/Post.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $fillable = &#x5B;&#039;title&#039;, &#039;body&#039;, &#039;user_id&#039;];

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function user()
    {
        return $this-&gt;belongsTo(User::class);
    }
}
</pre>
<p><strong>app/Models/User.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = &#x5B;
        &#039;name&#039;,
        &#039;email&#039;,
        &#039;password&#039;,
        &#039;is_admin&#039;
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = &#x5B;
        &#039;password&#039;,
        &#039;remember_token&#039;,
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array
     */
    protected function casts(): array
    {
        return &#x5B;
            &#039;email_verified_at&#039; =&gt; &#039;datetime&#039;,
            &#039;password&#039; =&gt; &#039;hashed&#039;,
        ];
    }
}
</pre>
<h3 class="step_code">Create Pusher App</h3>
<p>In this step, we will create account on pusher website. Here, you&#8217;ll find the following credentials: App ID, Key, Secret, Cluster.<br />
1. Create Account on Pusher Platform.<br />
2. Now, Click on Get Started button.</p>
<p><img decoding="async" src="https://codeplaners.com/wp-content/uploads/2025/06/screen-1.jpg" alt="" style="width: 80%;border: 2px solid #e5e7eb;border-radius: 0.75rem;box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);object-fit: cover;" /></p>
<p>3. Fill your app name than click on submit button.</p>
<p><img decoding="async" src="https://codeplaners.com/wp-content/uploads/2025/06/screen-2.jpg" alt="" style="width: 80%;border: 2px solid #e5e7eb;border-radius: 0.75rem;box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);object-fit: cover;" /></p>
<p>4. Now, go to App Keys section and copy details. we will use it latter:</p>
<p><img decoding="async" src="https://codeplaners.com/wp-content/uploads/2025/06/screen-3.jpg" alt="" style="width: 80%;border: 2px solid #e5e7eb;border-radius: 0.75rem;box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);object-fit: cover;" /></p>
<h3 class="step_code">Setup Pusher &#038; Echo Server</h3>
<p>For setup Pusher &#038; Echo server you need to run the following command.<br />
Generate broadcasting:</p>
<pre class="brush: php; title: ; notranslate">
php artisan install:broadcasting
</pre>
<p>Now, we will install pusher-php-server to use driver as pusher, so let&#8217;s run the given command:</p>
<pre class="brush: php; title: ; notranslate">
composer require pusher/pusher-php-server
</pre>
<p>Now, we will install laravel echo server. so, let&#8217;s run the below commands:</p>
<pre class="brush: php; title: ; notranslate">
npm install --save-dev laravel-echo pusher-js
</pre>
<p>Now, you will see echo.js file. where you can make a changes:<br />
<strong>resources/js/echo.js</strong></p>
<pre class="brush: php; title: ; notranslate">
import Echo from &#039;laravel-echo&#039;;
 
import Pusher from &#039;pusher-js&#039;;
window.Pusher = Pusher;
 
window.Echo = new Echo({
    broadcaster: &#039;pusher&#039;,
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
    forceTLS: false
});
</pre>
<p><strong>.env</strong></p>
<pre class="brush: php; title: ; notranslate">
BROADCAST_CONNECTION=pusher

PUSHER_APP_ID=&quot;1822914&quot;
PUSHER_APP_KEY=&quot;b47df8a8ea52a814246e&quot;
PUSHER_APP_SECRET=&quot;5f9397a869...&quot;
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=&quot;https&quot;
PUSHER_APP_CLUSTER=&quot;ap2&quot;

VITE_APP_NAME=&quot;${APP_NAME}&quot;
VITE_PUSHER_APP_KEY=&quot;${PUSHER_APP_KEY}&quot;
VITE_PUSHER_HOST=&quot;${PUSHER_HOST}&quot;
VITE_PUSHER_PORT=&quot;${PUSHER_PORT}&quot;
VITE_PUSHER_SCHEME=&quot;${PUSHER_SCHEME}&quot;
VITE_PUSHER_APP_CLUSTER=&quot;${PUSHER_APP_CLUSTER}&quot;
</pre>
<p>Now again run build command:</p>
<pre class="brush: php; title: ; notranslate">
npm run build
</pre>
<h3 class="step_code"> Create PostCreate Event</h3>
<p>In this step, we need to create &#8220;Event&#8221;. so let&#8217;s run the command below.</p>
<pre class="brush: php; title: ; notranslate">
php artisan make:event PostCreate
</pre>
<p>Now, you will see a new folder created as &#8220;Events&#8221;. where you can make a changes:<br />
<strong>app/Events/PostCreate.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class PostCreate implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

     public $post;

    /**
     * Create a new event instance.
     */
    public function __construct($post)
    {
        $this-&gt;post = $post;
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function broadcastOn()
    {
        return new Channel(&#039;posts&#039;);
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function broadcastAs()
    {
        return &#039;create&#039;;
    }

    /**
     * Get the data to broadcast.
     *
     * @return array
     */
    public function broadcastWith(): array
    {
        return &#x5B;
            &#039;message&#039; =&gt; &quot;&#x5B;{$this-&gt;post-&gt;created_at}] New Post Received with title &#039;{$this-&gt;post-&gt;title}&#039;.&quot;
        ];
    }
}
</pre>
<h3 class="step_code">Create Routes</h3>
<p>now we need to create some routes. so, add some the following routes. </p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PostController;

Route::get(&#039;/&#039;, function () {
    return view(&#039;welcome&#039;);
});

Auth::routes();

Route::get(&#039;/home&#039;, &#x5B;App\Http\Controllers\HomeController::class, &#039;index&#039;])-&gt;name(&#039;home&#039;);

Route::get(&#039;/posts&#039;, &#x5B;PostController::class, &#039;index&#039;])-&gt;name(&#039;posts.index&#039;);
Route::post(&#039;/posts&#039;, &#x5B;PostController::class, &#039;store&#039;])-&gt;name(&#039;posts.store&#039;);
</pre>
<h3 class="step_code">Create Controller</h3>
<p><strong>app/Http/Controllers/PostController.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use App\Events\PostCreate;

class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::get();

        return view(&#039;posts&#039;, compact(&#039;posts&#039;));
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request)
    {
        $this-&gt;validate($request, &#x5B;
             &#039;title&#039; =&gt; &#039;required&#039;,
             &#039;body&#039; =&gt; &#039;required&#039;
        ]);
   
        $post = Post::create(&#x5B;
            &#039;user_id&#039; =&gt; auth()-&gt;id(),
            &#039;title&#039; =&gt; $request-&gt;title,
            &#039;body&#039; =&gt; $request-&gt;body
        ]);

        event(new PostCreate($post));
   
        return back()-&gt;with(&#039;success&#039;,&#039;Post created successfully.&#039;);
    }
}
</pre>
<h3 class="step_code">Create and Update Blade Files</h3>
<p>Now, we will update app.blade.php file and create posts.blade file.<br />
<strong>resources/views/layouts/app.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;!doctype html&gt;
&lt;html lang=&quot;{{ str_replace(&#039;_&#039;, &#039;-&#039;, app()-&gt;getLocale()) }}&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;

    &lt;!-- CSRF Token --&gt;
    &lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;

    &lt;title&gt;{{ config(&#039;app.name&#039;, &#039;Laravel&#039;) }}&lt;/title&gt;

    &lt;!-- Fonts --&gt;
    &lt;link rel=&quot;dns-prefetch&quot; href=&quot;//fonts.bunny.net&quot;&gt;
    &lt;link href=&quot;https://fonts.bunny.net/css?family=Nunito&quot; rel=&quot;stylesheet&quot;&gt;

    &lt;!-- Scripts --&gt;
    @vite(&#x5B;&#039;resources/sass/app.scss&#039;, &#039;resources/js/app.js&#039;])
    
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css&quot; /&gt;
    @yield(&#039;script&#039;)
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;
        &lt;nav class=&quot;navbar navbar-expand-md navbar-light bg-white shadow-sm&quot;&gt;
            &lt;div class=&quot;container&quot;&gt;
                &lt;a class=&quot;navbar-brand&quot; href=&quot;{{ url(&#039;/&#039;) }}&quot;&gt;
                    Laravel Send Realtime Notification using Pusher
                &lt;/a&gt;
                &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#navbarSupportedContent&quot; aria-controls=&quot;navbarSupportedContent&quot; aria-expanded=&quot;false&quot; aria-label=&quot;{{ __(&#039;Toggle navigation&#039;) }}&quot;&gt;
                    &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
                &lt;/button&gt;

                &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent&quot;&gt;
                    &lt;!-- Left Side Of Navbar --&gt;
                    &lt;ul class=&quot;navbar-nav me-auto&quot;&gt;

                    &lt;/ul&gt;

                    &lt;!-- Right Side Of Navbar --&gt;
                    &lt;ul class=&quot;navbar-nav ms-auto&quot;&gt;
                        &lt;!-- Authentication Links --&gt;
                        @guest
                            @if (Route::has(&#039;login&#039;))
                                &lt;li class=&quot;nav-item&quot;&gt;
                                    &lt;a class=&quot;nav-link&quot; href=&quot;{{ route(&#039;login&#039;) }}&quot;&gt;{{ __(&#039;Login&#039;) }}&lt;/a&gt;
                                &lt;/li&gt;
                            @endif

                            @if (Route::has(&#039;register&#039;))
                                &lt;li class=&quot;nav-item&quot;&gt;
                                    &lt;a class=&quot;nav-link&quot; href=&quot;{{ route(&#039;register&#039;) }}&quot;&gt;{{ __(&#039;Register&#039;) }}&lt;/a&gt;
                                &lt;/li&gt;
                            @endif
                        @else
                            &lt;li class=&quot;nav-item&quot;&gt;
                                &lt;a class=&quot;nav-link&quot; href=&quot;{{ route(&#039;posts.index&#039;) }}&quot;&gt;{{ __(&#039;Posts&#039;) }}&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class=&quot;nav-item dropdown&quot;&gt;
                                &lt;a id=&quot;navbarDropdown&quot; class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; role=&quot;button&quot; data-bs-toggle=&quot;dropdown&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot; v-pre&gt;
                                    {{ Auth::user()-&gt;name }}
                                &lt;/a&gt;

                                &lt;div class=&quot;dropdown-menu dropdown-menu-end&quot; aria-labelledby=&quot;navbarDropdown&quot;&gt;
                                    &lt;a class=&quot;dropdown-item&quot; href=&quot;{{ route(&#039;logout&#039;) }}&quot;
                                       onclick=&quot;event.preventDefault();
                                                     document.getElementById(&#039;logout-form&#039;).submit();&quot;&gt;
                                        {{ __(&#039;Logout&#039;) }}
                                    &lt;/a&gt;

                                    &lt;form id=&quot;logout-form&quot; action=&quot;{{ route(&#039;logout&#039;) }}&quot; method=&quot;POST&quot; class=&quot;d-none&quot;&gt;
                                        @csrf
                                    &lt;/form&gt;
                                &lt;/div&gt;
                            &lt;/li&gt;
                        @endguest
                    &lt;/ul&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/nav&gt;

        &lt;main class=&quot;py-4&quot;&gt;
            @yield(&#039;content&#039;)
        &lt;/main&gt;
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;
</pre>
<p><strong>resources/views/posts.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
@extends(&#039;layouts.app&#039;)

@section(&#039;content&#039;)
&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row justify-content-center&quot;&gt;
        &lt;div class=&quot;col-md-12&quot;&gt;
            &lt;div class=&quot;card&quot;&gt;
                &lt;div class=&quot;card-header&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; {{ __(&#039;Posts List&#039;) }}&lt;/div&gt;

                &lt;div class=&quot;card-body&quot;&gt;
                    @session(&#039;success&#039;)
                        &lt;div class=&quot;alert alert-success&quot; role=&quot;alert&quot;&gt; 
                            {{ $value }}
                        &lt;/div&gt;
                    @endsession

                    &lt;div id=&quot;notification&quot;&gt;
                        
                    &lt;/div&gt;
                    
                    @if(!auth()-&gt;user()-&gt;is_admin)
                    &lt;p&gt;&lt;strong&gt;Create New Post&lt;/strong&gt;&lt;/p&gt;
                    &lt;form method=&quot;post&quot; action=&quot;{{ route(&#039;posts.store&#039;) }}&quot; enctype=&quot;multipart/form-data&quot;&gt;
                        @csrf
                        &lt;div class=&quot;form-group&quot;&gt;
                            &lt;label&gt;Title:&lt;/label&gt;
                            &lt;input type=&quot;text&quot; name=&quot;title&quot; class=&quot;form-control&quot; /&gt;
                            @error(&#039;title&#039;)
                                &lt;div class=&quot;text-danger&quot;&gt;{{ $message }}&lt;/div&gt;
                            @enderror
                        &lt;/div&gt;
                        &lt;div class=&quot;form-group&quot;&gt;
                            &lt;label&gt;Body:&lt;/label&gt;
                            &lt;textarea class=&quot;form-control&quot; name=&quot;body&quot;&gt;&lt;/textarea&gt;
                            @error(&#039;body&#039;)
                                &lt;div class=&quot;text-danger&quot;&gt;{{ $message }}&lt;/div&gt;
                            @enderror
                        &lt;/div&gt;
                        &lt;div class=&quot;form-group mt-2&quot;&gt;
                            &lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-block&quot;&gt;&lt;i class=&quot;fa fa-save&quot;&gt;&lt;/i&gt; Submit&lt;/button&gt;
                        &lt;/div&gt;
                    &lt;/form&gt;
                    @endif

                    &lt;p class=&quot;mt-4&quot;&gt;&lt;strong&gt;Post List:&lt;/strong&gt;&lt;/p&gt;
                    &lt;table class=&quot;table table-bordered data-table&quot;&gt;
                        &lt;thead&gt;
                            &lt;tr&gt;
                                &lt;th width=&quot;70px&quot;&gt;ID&lt;/th&gt;
                                &lt;th&gt;Title&lt;/th&gt;
                                &lt;th&gt;Body&lt;/th&gt;
                            &lt;/tr&gt;
                        &lt;/thead&gt;
                        &lt;tbody&gt;
                            @forelse($posts as $post)
                                &lt;tr&gt;
                                    &lt;td&gt;{{ $post-&gt;id }}&lt;/td&gt;
                                    &lt;td&gt;{{ $post-&gt;title }}&lt;/td&gt;
                                    &lt;td&gt;{{ $post-&gt;body }}&lt;/td&gt;
                                &lt;/tr&gt;
                            @empty
                                &lt;tr&gt;
                                    &lt;td colspan=&quot;5&quot;&gt;There are no posts.&lt;/td&gt;
                                &lt;/tr&gt;
                            @endforelse
                        &lt;/tbody&gt;
                    &lt;/table&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
@endsection

@section(&#039;script&#039;)
@if(auth()-&gt;user()-&gt;is_admin)
    &lt;script type=&quot;module&quot;&gt;
            window.Echo.channel(&#039;posts&#039;)
                .listen(&#039;.create&#039;, (data) =&gt; {
                    console.log(&#039;Order status updated: &#039;, data);
                    var d1 = document.getElementById(&#039;notification&#039;);
                    d1.insertAdjacentHTML(&#039;beforeend&#039;, &#039;&lt;div class=&quot;alert alert-success alert-dismissible fade show&quot;&gt;&lt;span&gt;&lt;i class=&quot;fa fa-circle-check&quot;&gt;&lt;/i&gt;  &#039;+data.message+&#039;&lt;/span&gt;&lt;/div&gt;&#039;);
                });
    &lt;/script&gt;
@endif
@endsection
</pre>
<h3 class="step_code"> Create Admin User</h3>
<p>To create admin user we need to create seeder. so run this command.<br />
Let&#8217;s run the migration command:</p>
<pre class="brush: php; title: ; notranslate">
php artisan make:seeder CreateAdminUser
</pre>
<p>Now, we need to do some changes in CreateAdminUser seeder.<br />
<strong>database/seeders/CreateAdminUser.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;

class CreateAdminUser extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        User::create(&#x5B;
            &#039;name&#039; =&gt; &#039;Admin&#039;,
            &#039;email&#039; =&gt; &#039;admin@gmail.com&#039;,
            &#039;password&#039; =&gt; bcrypt(&#039;123456&#039;),
            &#039;is_admin&#039; =&gt; 1
        ]);
    }
}
</pre>
<p>now, the run seeder using the below command:</p>
<pre class="brush: php; title: ; notranslate">
php artisan db:seed --class=CreateAdminUser
</pre>
<p><strong>Run Laravel App:</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan serve
</pre>
<p>Now, you have one admin user and you can register another normal user from registration form.<br />
Now, when you post then admin user will get the notification.</p>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/">How to send Real-Time Notifications using Pusher Example in Laravel 12</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/how-to-send-real-time-notifications-using-pusher-example-in-laravel-12/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Slow &#8220;No Query Results For Model&#8221; Error in Laravel 10</title>
		<link>https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/</link>
					<comments>https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 27 Dec 2024 03:08:36 +0000</pubDate>
				<category><![CDATA[Laravel 10]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1682</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:37 amHi Dev, Today, we will show you how to slow &#8220;no query results for model&#8221; error in laravel 10. This article will give you simple example of how to slow &#8220;no query results for model&#8221; error in laravel 10. Let&#8217;s discuss how to slow &#8230; <a href="https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/" class="more-link">Continue reading<span class="screen-reader-text"> "How To Slow &#8220;No Query Results For Model&#8221; Error in Laravel 10"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/">How To Slow &#8220;No Query Results For Model&#8221; Error in Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:37 am</p><p>Hi Dev,</p>
<p>Today, we will show you how to slow &#8220;no query results for model&#8221; error in laravel 10. This article will give you simple example of how to slow &#8220;no query results for model&#8221; error in laravel 10. Let&#8217;s discuss how to slow &#8220;no query results for model&#8221; error in laravel 10. In this article, we will implement a how to slow &#8220;no query results for model&#8221; error in laravel 10.</p>
<p>We can fix this problem this way, you just need to add the following code to your Handler.php file.</p>
<h3 class="step_code">Controller Method</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ModelController extends Controller
{
    /**
    * Display the specified resource.
    *
    * @param  \App\Models\Model  $Model
    * @return \Illuminate\Http\Response
    */
    public function show(User $user)
    {
        return response()-&gt;json($user-&gt;toArray());
    }
}
</pre>
<p><strong>Error</strong></p>
<pre class="brush: php; title: ; notranslate">
No query results for model &#x5B;App\\User] 1
</pre>
<h3 class="step_code">app/Exceptions/Handler.php</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class Handler extends ExceptionHandler
{
    /**
    * A list of the exception types that are not reported.

	Ezoic
    *
    * @var array
    */
    protected $dontReport = &#x5B;
        //
    ];

    /**
    * A list of the inputs that are never flashed for validation exceptions.
    *
    * @var array
    */
    protected $dontFlash = &#x5B;
        'password',
        'password_confirmation',
    ];

    /**
    * Report or log an exception.
    *
    * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
Ezoic
    *
    * @param  \Exception  $exception
    * @return void
    */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
    * Render an exception into an HTTP response.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Exception  $exception
    * @return \Illuminate\Http\Response
    */
    public function render($request, Exception $exception)
    {
        if ($e instanceof ModelNotFoundException) {
            return response()-&gt;json(&#x5B;'error' =&gt; 'Data not found.']);
        }

        return parent::render($request, $exception);
    }
}
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/">How To Slow &#8220;No Query Results For Model&#8221; Error in Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/how-to-slow-no-query-results-for-model-error-in-laravel-10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Add Blade Components In Laravel 10</title>
		<link>https://codeplaners.com/how-to-add-blade-components-in-laravel-10/</link>
					<comments>https://codeplaners.com/how-to-add-blade-components-in-laravel-10/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 20 Dec 2024 03:10:43 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[blade components in laravel 10]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1664</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:38 amHi Dev, Today, we will show you how to add blade components in laravel 10. This article will give you simple example of how to add blade components in laravel 10. Let&#8217;s discuss how to add blade components in laravel 10. In this article, &#8230; <a href="https://codeplaners.com/how-to-add-blade-components-in-laravel-10/" class="more-link">Continue reading<span class="screen-reader-text"> "How to Add Blade Components In Laravel 10"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-add-blade-components-in-laravel-10/">How to Add Blade Components In Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:38 am</p><p>Hi Dev,</p>
<p>Today, we will show you how to add blade components in laravel 10. This article will give you simple example of how to add blade components in laravel 10. Let&#8217;s discuss how to add blade components in laravel 10. In this article, we will implement a how to add blade components in laravel 10. </p>
<p><strong>What are Laravel blade components?</strong><br />
Laravel Blade components are reusable, self-contained building blocks for your views. They allow you to encapsulate UI elements, making your code cleaner, more maintainable, and promoting the concept of &#8220;Don&#8217;t Repeat Yourself&#8221; (DRY). Components are incredibly versatile and can represent anything from simple buttons to complex form elements.</p>
<h3 class="step_code">Install the Laravel 10</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project --prefer-dist laravel/laravel laravel-app
cd laravel-app
</pre>
<h3 class="step_code">Create a Blade Component</h3>
<p><strong>Now, generate blade component by running the command.</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:component Button
cd laravel-app
</pre>
<p>&#8220;This command will create a new directory in your <strong>resources/views/components</strong> folder that will contain a blade view <strong>(button.blade.php)</strong> and the associated PHP class <strong>(button.php)</strong> within the <strong>views/components directory</strong>.</p>
<h3 class="step_code">Define the Component Blade View</h3>
<p>Navigate to the <strong>button.blade.php</strong> file located in the <strong>resources/views/components</strong> directory. Within this file, define the HTML structure and any necessary logic for your button component. For example</p>
<pre class="brush: php; title: ; notranslate">
&lt;button {{ $attributes-&gt;merge(&#x5B;'class' =&gt; 'bg-blue-500 text-white']) }}&gt;
    {{ $slot }}
&lt;/button&gt;
</pre>
<p>We take advantage of the <strong>$attributes</strong> variable to merge any additional attributes provided to the component. The <strong>$slot</strong> variable represents the content inserted within the component when used in a view.</p>
<h3 class="step_code">Use the Blade Component</h3>
<p>Now that our component is defined, let&#8217;s include it in a view. Open any Blade view (for example, <strong>resources/views/welcome.blade.php</strong>) and include your component using the <x> directive.</p>
<pre class="brush: php; title: ; notranslate">
&lt;x-button&gt;
    Click Me
&lt;/x-button&gt;
</pre>
<h3 class="step_code">Reusable Components with Parameters</h3>
<pre class="brush: php; title: ; notranslate">
&lt;x-button color=&quot;red&quot;&gt;
    Danger
&lt;/x-button&gt;
</pre>
<p>To accomplish this, access the <strong>Button.php</strong> class in the <strong>Views/Components</strong> directory and declare a public property called &#8216;color&#8217;.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Button extends Component
{
    public $color;

    /**
    * Create a new component instance.
    */
    public function __construct($color = 'blue')
    {
        $this-&gt;color = $color;
    }

    /**
    * Get the view / contents that represent the component.
    */
    public function render(): View|Closure|string
    {
        return view('components.button');
    }
}
</pre>
<p>use the $color property inside the <strong>button.blade.php</strong> view.</p>
<pre class="brush: php; title: ; notranslate">
&lt;button {{ $attributes-&gt;merge(&#x5B;'class' =&gt; 'bg-' . $color . ' text-white']) }}&gt;
    {{ $slot }}
&lt;/button&gt;
</pre>
<p>I hope it will assist you…</p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-add-blade-components-in-laravel-10/">How to Add Blade Components In Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/how-to-add-blade-components-in-laravel-10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Laravel 10  Backup Database While Excluding Specific Tables</title>
		<link>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/</link>
					<comments>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 15 Dec 2024 04:04:06 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[laravel 10 backup database]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1661</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:38 amHi Dev, Today, we will show you laravel 10 backup database while excluding specific tables. This article will give you simple example of laravel 10 backup database while excluding specific tables. Let&#8217;s discuss laravel 10 backup database while excluding specific tables. In this article, &#8230; <a href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10  Backup Database While Excluding Specific Tables"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/">Laravel 10  Backup Database While Excluding Specific Tables</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:38 am</p><p>Hi Dev,</p>
<p>Today, we will show you laravel 10  backup database while excluding specific tables. This article will give you simple example of laravel 10  backup database while excluding specific tables. Let&#8217;s discuss laravel 10  backup database while excluding specific tables. In this article, we will implement a laravel 10  backup database while excluding specific tables. </p>
<p>So let’s follow few step to create example of laravel 10  backup database while excluding specific tables.</p>
<h3 class="step_code">Install the Laravel 10</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project laravel/laravel example-app
</pre>
<h3 class="step_code">Database Configuration</h3>
<p><strong>.env</strong></p>
<pre class="brush: php; title: ; notranslate">
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database name(laravel)
DB_USERNAME=username(root)
DB_PASSWORD=password(root)
</pre>
<h3 class="step_code">Create Migration</h3>
<p><strong>Create Products Table</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration create_products_table --create=products
</pre>
<p><strong>database\migrations\create_products_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('name');
            $table-&gt;text('detail');
            $table-&gt;string('price');
            $table-&gt;string('image');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
</pre>
<p><strong>Create Orders Table</strong><br />
<strong>database\migrations\create_orders_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration create_orders_table --create=orders
</pre>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('detail');
            $table-&gt;string('quantity');
            $table-&gt;string('total');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
</pre>
<h3 class="step_code">run this migration by following the command</h3>
<pre class="brush: php; title: ; notranslate">
php artisan migrate
</pre>
<h3 class="step_code">Create Scheduler Command</h3>
<p><strong>We need to create a scheduler command to backup the database ignoring specific tables. To create a scheduler command, run the following command:</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:command DatabaseBackup --command=databasebackup:cron
</pre>
<p><strong>app/Console/Commands/DatabaseBackup.php</strong><br />
Set code to &#8220;DatabaseBackup.php&#8221; file.</p>
<p><strong>DatabaseBackup.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Redirect;
use Carbon\Carbon;
use Carbon\CarbonPeriod;

class DatabaseBackup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'databasebackup:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // Genrate backup files 
        $filename = \Carbon\Carbon::now()-&gt;format('m-d-Y').&quot;-backup.sql.gz&quot;;

        $command = &quot;mysqldump --user=&quot; . env('DB_USERNAME') .&quot; --password=&quot; . env('DB_PASSWORD') . &quot; --host=&quot; . env('DB_HOST') . &quot; \
            --ignore-table=&quot;.env('DB_DATABASE').&quot;.tablename_to_ignore \
            --ignore-table=&quot;.env('DB_DATABASE').&quot;.tablename_to_ignore &quot; . env('DB_DATABASE') . &quot; | gzip &gt; &quot; .storage_path(&quot;app/backup/&quot;. $filename);

        $returnVar = NULL;
        $output  = NULL;
  
        exec($command, $output, $returnVar);

        // Delete existing backup files 
        $startDate = Carbon::now()-&gt;subDays(30);
        $endDate = Carbon::now();
  
        $dateRange = CarbonPeriod::create($startDate, $endDate);
        $dates = &#x5B;'.gitignore'];
        foreach ($dateRange-&gt;toArray() as $key =&gt; $value) {
            $dates&#x5B;] = $value-&gt;format('m-d-Y').&quot;-backup.sql.gz&quot;;
        }

        $mediaPath = storage_path('app/backup');
        $files = File::allFiles($mediaPath);

        foreach ($files as $key =&gt; $value) {
            $fileName = $value-&gt;getRelativePathname();
            if (!in_array($fileName, $dates)) {
                File::delete($value);
            }
        }
    }
}
</pre>
<p><strong>Set up a daily cron job for automatic backups in file path &#8220;app/Console/Kernel.php&#8221;.</strong><br />
<strong>Kernel.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule-&gt;command('databasebackup:cron')-&gt;daily();
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this-&gt;load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
</pre>
<p>execute the scheduler code, run the command below.</p>
<pre class="brush: php; title: ; notranslate">
php artisan databasebackup:cron
</pre>
<p><strong>The backup file will be generated at the path &#8216;storage/app/backup&#8217;.</strong></p>
<p>I hope it will assist you…</p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/">Laravel 10  Backup Database While Excluding Specific Tables</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Laravel 10 Implement User Role and Permission Example</title>
		<link>https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/</link>
					<comments>https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 14 Dec 2024 12:55:20 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1658</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:38 amHi Dev, Today, we will show you laravel 10 implement user role and permission example. This article will give you simple example of laravel 10 implement user role and permission example. Let&#8217;s discuss how to set role permissions. In this article, we will implement &#8230; <a href="https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10 Implement User Role and Permission Example"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/">Laravel 10 Implement User Role and Permission Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:38 am</p><p>Hi Dev,</p>
<p>Today, we will show you laravel 10 implement user role and permission example. This article will give you simple example of laravel 10 implement user role and permission example. Let&#8217;s discuss how to set role permissions. In this article, we will implement a laravel 10 implement user role and permission example. </p>
<p>So let’s follow few step to create example of laravel 10 implement user role and permission example.</p>
<h3 class="step_code">Install the Spatie Permission package.</h3>
<pre class="brush: php; title: ; notranslate">
composer require spatie/laravel-permission
</pre>
<h3 class="step_code">Publish the package&#8217;s configuration and migration files.</h3>
<pre class="brush: php; title: ; notranslate">
php artisan vendor:publish --provider=&quot;Spatie\Permission\PermissionServiceProvider&quot;
</pre>
<h3 class="step_code">Migrate the database.</h3>
<pre class="brush: php; title: ; notranslate">
php artisan migrate
</pre>
<h3 class="step_code">Create the Role and Permission models.</h3>
<pre class="brush: php; title: ; notranslate">
php artisan make:model Role -m
php artisan make:model Permission -m
</pre>
<h3 class="step_code">Update User Model File</h3>
<pre class="brush: php; title: ; notranslate">
class User extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = &#x5B;
        'name',
        'email',
        'password',
    ];

    protected $hidden = &#x5B;
        'password',
        'remember_token',
    ];

    protected $casts = &#x5B;
        'email_verified_at' =&gt; 'datetime',
    ];

    public function roles()
    {
        return $this-&gt;belongsToMany(Role::class);
    }

    public function permissions()
    {
        return $this-&gt;belongsToMany(Permission::class);
    }
}
</pre>
<h3 class="step_code">Create Role and Permission seeders</h3>
<pre class="brush: php; title: ; notranslate">
php artisan make:seeder RoleSeeder
php artisan make:seeder PermissionSeeder
</pre>
<h3 class="step_code">RoleSeeder create some roles and add them to the database</h3>
<pre class="brush: php; title: ; notranslate">
class RoleSeeder extends Seeder
{
    public function run()
    {
        $adminRole = Role::create(&#x5B;'name' =&gt; 'admin']);
        $userRole = Role::create(&#x5B;'name' =&gt; 'user']);

        $adminRole-&gt;givePermissionTo('all');
    }
}
</pre>
<h3 class="step_code">PermissionSeeder create certain permissions and then seed them into the database.</h3>
<pre class="brush: php; title: ; notranslate">
class PermissionSeeder extends Seeder
{
    public function run()
    {
        Permission::create(&#x5B;'name' =&gt; 'view users']);
        Permission::create(&#x5B;'name' =&gt; 'create users']);
        Permission::create(&#x5B;'name' =&gt; 'edit users']);
        Permission::create(&#x5B;'name' =&gt; 'delete users']);
    }
}
</pre>
<h3 class="step_code">Run the seeders</h3>
<pre class="brush: php; title: ; notranslate">
php artisan db:seed
</pre>
<h3 class="step_code">Assign roles to users based on their responsibilities or access levels.</h3>
<pre class="brush: php; title: ; notranslate">
$user = User::find(1);
$user-&gt;assignRole('admin');
</pre>
<h3 class="step_code">Verify whether the user has a specific role or permission.</h3>
<pre class="brush: php; title: ; notranslate">
$user = User::find(1);

if ($user-&gt;hasRole('admin')) {
    // do something
}

if ($user-&gt;can('view users')) {
    // do something
}
</pre>
<h3 class="step_code">You can also use middleware for security based on user roles. For example, the following middleware will allow access to the /users root exclusively for users with the &#8216;admin&#8217; role.</h3>
<pre class="brush: php; title: ; notranslate">
Route::middleware('role:admin')-&gt;get('/users', function () {
    // ...
});
</pre>
<p>I hope it will assist you…</p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/">Laravel 10 Implement User Role and Permission Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-implement-user-role-and-permission-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Set Indian Timezone in Laravel</title>
		<link>https://codeplaners.com/set-indian-timezone-in-laravel/</link>
					<comments>https://codeplaners.com/set-indian-timezone-in-laravel/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 06 Dec 2024 15:53:58 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[Laravel 11]]></category>
		<category><![CDATA[timezone]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1740</guid>

					<description><![CDATA[<p>Hi Dev, In this post, I will show you how to set Indian time zone in Laravel By default, Laravel uses UTC time zone. We can set it for India or any other country. Here we are showing you how to set Indian time zone. Let&#8217;s set the Indian time zone to `Asia/Kolkata`. We can &#8230; <a href="https://codeplaners.com/set-indian-timezone-in-laravel/" class="more-link">Continue reading<span class="screen-reader-text"> "Set Indian Timezone in Laravel"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/set-indian-timezone-in-laravel/">Set Indian Timezone in Laravel</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi Dev,</p>
<p>In this post, I will show you how to set Indian time zone in Laravel</p>
<p>By default, Laravel uses UTC time zone. We can set it for India or any other country. Here we are showing you how to set Indian time zone. Let&#8217;s set the Indian time zone to `Asia/Kolkata`. We can apply this time zone in two ways:</p>
<h3 class="step_code">Step 1: Update timezone .env file</h3>
<p>.env</p>
<pre class="brush: php; title: ; notranslate">
APP_TIMEZONE=&quot;Asia/Kolkata&quot;
</pre>
<h3 class="step_code">Step 2: Update timezone config file</h3>
<pre class="brush: php; title: ; notranslate">
'timezone' =&gt; 'Asia/Kolkata',
</pre>
<p>You can find out if the time zone is set correctly by following route code</p>
<pre class="brush: php; title: ; notranslate">
Route::get('/', function () {
    dd(now());
});
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/set-indian-timezone-in-laravel/">Set Indian Timezone in Laravel</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/set-indian-timezone-in-laravel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Add DomPDF Add QR Code to PDF in Laravel</title>
		<link>https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/</link>
					<comments>https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 09 Nov 2024 03:43:45 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[DomPDF]]></category>
		<category><![CDATA[DomPDF in laravel]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1653</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:40 amHi dev, Today, i show you how to add DomPDF add QR Code to PDF in laravel. In this article will tell you how to add DomPDF add QR Code to PDF in laravel. you will how to add DomPDF add QR Code to &#8230; <a href="https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/" class="more-link">Continue reading<span class="screen-reader-text"> "How to Add DomPDF Add QR Code to PDF in Laravel"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/">How to Add DomPDF Add QR Code to PDF in Laravel</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:40 am</p><p>Hi dev,</p>
<p>Today, i show you how to add DomPDF add QR Code to PDF in laravel. In this article will tell you how to add DomPDF add QR Code to PDF in laravel. you will how to add DomPDF add QR Code to PDF in laravel. I will show in this example how to integrate QR code PDF. For this we will use two composer packages.</p>
<p>You can also use this example with Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10 versions.</p>
<p>So, let’s follow few steps to create example of how to add DomPDF add QR Code to PDF in laravel.</p>
<h3 class="step_code">Step 1: Install Laravel App</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project laravel/laravel example-qrcode
</pre>
<h3 class="step_code">Step 2: Install DomPDF Package</h3>
<pre class="brush: php; title: ; notranslate">
composer require barryvdh/laravel-dompdf
</pre>
<pre class="brush: php; title: ; notranslate">
composer require simplesoftwareio/simple-qrcode
</pre>
<h3 class="step_code">Step 3: Create Controller</h3>
<pre class="brush: php; title: ; notranslate">
php artisan make:controller PDFController
</pre>
<p><strong>app/Http/Controllers/PDFController.php</strong></p>
<pre class="brush: php; title: ; notranslate">
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use PDF;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
    
class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function generatePDF()
    {
        $qrcode = base64_encode(QrCode::format('svg')-&gt;size(200)-&gt;errorCorrection('H')-&gt;generate('string'));
        $data = &#x5B;
            'title' =&gt; 'Welcome to Codeplaners.com',
            'qrcode' =&gt; $qrcode
        ]; 
              
        $pdf = PDF::loadView('myPDF', $data);
  
        return $pdf-&gt;download('codeplaners.pdf');
    }
}
</pre>
<h3 class="step_code">Step 4: Add Route</h3>
<p><strong>routes/web.php</strong></p>
<pre class="brush: php; title: ; notranslate">
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;

  
Route::get('generate-pdf', &#x5B;PDFController::class, 'generatePDF']);
</pre>
<h3 class="step_code">Step 5: Create View File</h3>
<p><strong>resources/views/myPDF.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;How to Add DomPDF Add QR Code to PDF in Laravel - codeplaners.com&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  
&lt;div&gt;
    &lt;h1&gt;How to Add DomPDF Add QR Code to PDF in Laravel&lt;/h1&gt;
      
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

Ezoic
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
  
    &lt;img src=&quot;data:image/png;base64,{{ $qrcode }}&quot; alt=&quot;&quot;&gt;
    
&lt;/div&gt;
    
&lt;/body&gt;
&lt;/html&gt;	
</pre>
<h3 class="step_code">Run Laravel App:</h3>
<p><strong>routes/web.php</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan serve
</pre>
<pre class="brush: php; title: ; notranslate">
http://localhost:8000/generate-pdf
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/">How to Add DomPDF Add QR Code to PDF in Laravel</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/how-to-add-dompdf-add-qr-code-to-pdf-in-laravel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Laravel 10 map() function Add Attribute Example</title>
		<link>https://codeplaners.com/laravel-10-map-function-add-attribute-example/</link>
					<comments>https://codeplaners.com/laravel-10-map-function-add-attribute-example/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 07 Nov 2024 03:08:06 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[map() function]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1650</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:40 amHi dev, Today, i show you laravel 10 map() function add attribute example. In this article will tell you laravel 10 map() function add attribute example. you will laravel 10 map() function add attribute example. You can also use this example with Laravel 6, &#8230; <a href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10 map() function Add Attribute Example"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/">Laravel 10 map() function Add Attribute Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:40 am</p><p>Hi dev,</p>
<p>Today, i show you laravel 10 map() function add attribute example. In this article will tell you laravel 10 map() function add attribute example. you will laravel 10 map() function add attribute example. </p>
<p>You can also use this example with Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10 versions.</p>
<p>If we are posting with status and you need to set label on status like &#8220;0&#8221; means &#8220;Inactive&#8221; and &#8220;1&#8221; means &#8220;Active&#8221;, then you can do it using laravel collection map() function.</p>
<p>So, let’s follow few steps to create example of laravel 10 map() function add attribute example.</p>
<h3 class="step_code">Example 1: setAttribute()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $user = User::first();
  
        $user-&gt;setAttribute('position', 'Web Developer');
  
        dd($user-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
array:8 &#x5B;

  &quot;id&quot; =&gt; 1

  &quot;name&quot; =&gt; &quot;Mohan&quot;

  &quot;email&quot; =&gt; &quot;codeplaners@gmail.com&quot;

  &quot;email_verified_at&quot; =&gt; null

  &quot;created_at&quot; =&gt; &quot;2022-06-23T07:02:17.000000Z&quot;

  &quot;updated_at&quot; =&gt; &quot;2022-06-23T07:02:17.000000Z&quot;

  &quot;birthdate&quot; =&gt; &quot;1998-11-15&quot;

  &quot;position&quot; =&gt; &quot;Web Developer&quot;

]
</pre>
<h3 class="step_code">Example 2: map()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Str;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = collect(&#x5B;
            &#x5B;'id'=&gt;1,'title'=&gt;'Post One'],
            &#x5B;'id'=&gt;2,'title'=&gt;'Post Two'],
            &#x5B;'id'=&gt;3,'title'=&gt;'Post Three'],
            &#x5B;'id'=&gt;4,'title'=&gt;'Post Four'],
        ]);
   
        $posts = $posts-&gt;map(function ($post) {
            $post&#x5B;'url'] = url(&quot;post/&quot;. Str::slug($post&#x5B;'title']));
            return $post;
        });
  
        dd($posts-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;0] =&gt; Array

        (

            &#x5B;id] =&gt; 1

            &#x5B;title] =&gt; Post One

            &#x5B;url] =&gt; http://localhost:8000/post/post-one

        )

    &#x5B;1] =&gt; Array

        (

            &#x5B;id] =&gt; 2

            &#x5B;title] =&gt; Post Two

            &#x5B;url] =&gt; http://localhost:8000/post/post-two

        )

    &#x5B;2] =&gt; Array

        (

            &#x5B;id] =&gt; 3

            &#x5B;title] =&gt; Post Three

            &#x5B;url] =&gt; http://localhost:8000/post/post-three

        )

    &#x5B;3] =&gt; Array

        (

            &#x5B;id] =&gt; 4

            &#x5B;title] =&gt; Post Four

            &#x5B;url] =&gt; http://localhost:8000/post/post-four

        )

)
</pre>
<h3 class="step_code">Example 3: Laravel 10 Eloquent Collection Add Attribute using map()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select(&quot;id&quot;, &quot;title&quot;, &quot;status&quot;)-&gt;take(5)-&gt;get();
  
        $posts = $posts-&gt;map(function ($post) {
            $post-&gt;status_label = $post-&gt;status == 1 ? 'Active' : 'InActive';
            return $post;
        });
  
        dd($posts-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;0] =&gt; Array

        (

            &#x5B;id] =&gt; 2

            &#x5B;title] =&gt; Demo Updated

            &#x5B;status] =&gt; 1

            &#x5B;status_label] =&gt; Active

        )

    &#x5B;1] =&gt; Array

        (

            &#x5B;id] =&gt; 3

            &#x5B;title] =&gt; Dr.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;2] =&gt; Array

        (

            &#x5B;id] =&gt; 4

            &#x5B;title] =&gt; Miss

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;3] =&gt; Array

        (

            &#x5B;id] =&gt; 5

            &#x5B;title] =&gt; Dr.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;4] =&gt; Array

        (

            &#x5B;id] =&gt; 6

            &#x5B;title] =&gt; Mrs.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

)
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/">Laravel 10 map() function Add Attribute Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-map-function-add-attribute-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Laravel 10 Sort By Date Example</title>
		<link>https://codeplaners.com/laravel-10-sort-by-date-example/</link>
					<comments>https://codeplaners.com/laravel-10-sort-by-date-example/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 07 Nov 2024 02:42:29 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[Laravel 10 Sort By Date]]></category>
		<category><![CDATA[Sort By Date]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1647</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:40 amHi dev, Today, i show you laravel 10 sort by date example. In this article will tell you laravel 10 sort by date example. you will laravel 10 sort by date example. We will use sortBy() method to sort by date in Laravel 10. &#8230; <a href="https://codeplaners.com/laravel-10-sort-by-date-example/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10 Sort By Date Example"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-sort-by-date-example/">Laravel 10 Sort By Date Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:40 am</p><p>Hi dev,</p>
<p>Today, i show you laravel 10 sort by date example. In this article will tell you laravel 10 sort by date example. you will laravel 10 sort by date example. </p>
<p>We will use sortBy() method to sort by date in Laravel 10. I will give you simple two examples with output so that you can understand from tick.</p>
<p>So, let’s follow few steps to create example of laravel 10 sort by date example.</p>
<h3 class="step_code">Example 1:</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect(&#x5B;
            &#x5B;'id' =&gt; 1, 'name' =&gt; 'codeplaners', 'created_at' =&gt; '2023-11-07'],
            &#x5B;'id' =&gt; 2, 'name' =&gt; 'mohan', 'created_at' =&gt; '2023-11-08'],
            &#x5B;'id' =&gt; 3, 'name' =&gt; 'brijesh', 'created_at' =&gt; '2023-11-05'],
            &#x5B;'id' =&gt; 4, 'name' =&gt; 'krishan', 'created_at' =&gt; '2023-11-04'],
        ]);
  
        $sorted = $collection-&gt;sortBy('created_at');
  
        $sorted = $sorted-&gt;all();
      
        dd($sorted);
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;3] =&gt; Array

        (

            &#x5B;id] =&gt; 4

            &#x5B;name] =&gt; krishan

            &#x5B;created_at] =&gt; 2023-11-04

        )

    &#x5B;2] =&gt; Array

        (

            &#x5B;id] =&gt; 3

            &#x5B;name] =&gt; brijesh

            &#x5B;created_at] =&gt; 2023-11-05

        )

    &#x5B;0] =&gt; Array

        (

            &#x5B;id] =&gt; 1

            &#x5B;name] =&gt; codeplaners

            &#x5B;created_at] =&gt; 2023-11-07

        )

    &#x5B;1] =&gt; Array

        (

            &#x5B;id] =&gt; 2

            &#x5B;name] =&gt; mohan

            &#x5B;created_at] =&gt; 2023-11-08

        )

)
</pre>
<h3 class="step_code">Example 2:</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect(&#x5B;'2023-11-07', '2023-11-08', '2023-11-05', '2023-11-04']);
    
        $sorted = $collection-&gt;sortBy(function ($date) {
            return \Carbon\Carbon::createFromFormat('Y-m-d', $date);
        });
      
        $sorted = $sorted-&gt;all();
      
        dd($sorted);
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;3] =&gt; 2023-11-04

    &#x5B;2] =&gt; 2023-11-05

    &#x5B;0] =&gt; 2023-11-07

    &#x5B;1] =&gt; 2023-11-08

)
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-sort-by-date-example/">Laravel 10 Sort By Date Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-sort-by-date-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Upload Files to MySQL Database in Laravel 10</title>
		<link>https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/</link>
					<comments>https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 21 Sep 2024 05:27:00 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1633</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:40 amHi dev, Today, i show you how to upload files to MySQL database in laravel 10. In this article will tell you how to upload files to MySQL database in laravel 10. you will how to upload files to MySQL database in laravel 10. &#8230; <a href="https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/" class="more-link">Continue reading<span class="screen-reader-text"> "How to Upload Files to MySQL Database in Laravel 10"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/">How to Upload Files to MySQL Database in Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:40 am</p><p>Hi dev,</p>
<p>Today, i show you how to upload files to MySQL database in laravel 10. In this article will tell you how to upload files to MySQL database in laravel 10. you will how to upload files to MySQL database in laravel 10. </p>
<p>So, let’s follow few steps to create example of how to upload files to MySQL database in laravel 10.</p>
<h3 class="step_code">Step 1: Install Laravel</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project laravel/laravel example-blog
</pre>
<h3 class="step_code">Step 2: Create Table and Model</h3>
<pre class="brush: php; title: ; notranslate">
php artisan create_files_table
</pre>
<p><strong>database/migrations/migration_file.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('files', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('name');
            $table-&gt;string('mime');
            $table-&gt;timestamps();
        });
  
        DB::statement(&quot;ALTER TABLE files ADD file MEDIUMBLOB&quot;);
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('files');
    }
};
</pre>
<h3 class="step_code">Step 3: Create Controller</h3>
<pre class="brush: php; title: ; notranslate">
php artisan make:controller FileController
</pre>
<p><strong>app/Http/Controllers/FileController.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
      
namespace App\Http\Controllers;
       
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use App\Models\File;
      
class FileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(): View
    {
        $files = File::latest()-&gt;get();
        return view('fileUpload', compact('files'));
    }
        
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $request-&gt;validate(&#x5B;
            'file' =&gt; 'mimes:jpeg,bmp,png,pdf,jpg',
            'name' =&gt; 'required|max:255',
        ]);
        
        if ($request-&gt;hasFile('file')) {           
            $path = $request-&gt;file('file')-&gt;getRealPath();
            $ext = $request-&gt;file-&gt;extension();
            $doc = file_get_contents($path);
            $base64 = base64_encode($doc);
            $mime = $request-&gt;file('file')-&gt;getClientMimeType();
  
            File::create(&#x5B;
                'name'=&gt; $request-&gt;name .'.'.$ext,
                'file' =&gt; $base64,
                'mime'=&gt; $mime,
            ]);
        }
         
        return back()
            -&gt;with('success','You have successfully upload file.');
     
    }
}
</pre>
<h3 class="step_code">Step 4: Create Add Routes</h3>
<p><strong>routes/web.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\FileController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the &quot;web&quot; middleware group. Now create something great!
|
*/
  
Route::get('file-upload', &#x5B;FileController::class, 'index']);
Route::post('file-upload', &#x5B;FileController::class, 'store'])-&gt;name('file.store');
</pre>
<h3 class="step_code">Step 5: Create Blade File</h3>
<p>resources/views/fileUpload.blade.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;How to Upload Files to MySQL Database in Laravel 10 - codeplaners.com&lt;/title&gt;
    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;
        
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
         
    &lt;div class=&quot;panel panel-primary&quot;&gt;
    
      &lt;div class=&quot;panel-heading&quot;&gt;
        &lt;h2&gt;How to Upload Files to MySQL Database in Laravel 10 - codeplaners.com&lt;/h2&gt;
      &lt;/div&gt;
    
      &lt;div class=&quot;panel-body&quot;&gt;
         
        @if ($message = Session::get('success'))
            &lt;div class=&quot;alert alert-success alert-block&quot;&gt;
                &lt;strong&gt;{{ $message }}&lt;/strong&gt;
            &lt;/div&gt;
        @endif
  
        &lt;div class=&quot;row&quot;&gt;  
            @if ($files-&gt;count())
            &lt;h3&gt;Files&lt;/h3&gt;
            &lt;table class=&quot;table table-striped table-bordered table-hover&quot;&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Name&lt;/td&gt;
                        &lt;td&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                @foreach ($files as $file)
                &lt;tr&gt;
                    &lt;td&gt;{{ $file-&gt;name }}&lt;/td&gt;
                    &lt;td&gt;&lt;img src=&quot;data:{{ $file-&gt;mime }};base64,{{ $file-&gt;file }}&quot; style=&quot;height: 100px; width: auto&quot;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;/tbody&gt;
                @endforeach
            &lt;/table&gt;
            @endif
        &lt;/div&gt;
          
        &lt;h3&gt;Upload File:&lt;/h3&gt;
        &lt;form action=&quot;{{ route('file.store') }}&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
            @csrf
  
            &lt;div class=&quot;mb-3&quot;&gt;
                &lt;label class=&quot;form-label&quot; for=&quot;inputFile&quot;&gt;Name:&lt;/label&gt;
                &lt;input 
                    type=&quot;text&quot; 
                    name=&quot;name&quot; 
                    id=&quot;inputFile&quot;
                    class=&quot;form-control @error('name') is-invalid @enderror&quot;&gt;
    
                @error('name')
                    &lt;span class=&quot;text-danger&quot;&gt;{{ $message }}&lt;/span&gt;
                @enderror
            &lt;/div&gt;
    
            &lt;div class=&quot;mb-3&quot;&gt;
                &lt;label class=&quot;form-label&quot; for=&quot;inputFile&quot;&gt;File:&lt;/label&gt;
                &lt;input 
                    type=&quot;file&quot; 
                    name=&quot;file&quot; 
                    id=&quot;inputFile&quot;
                    class=&quot;form-control @error('file') is-invalid @enderror&quot;&gt;
    
                @error('file')
                    &lt;span class=&quot;text-danger&quot;&gt;{{ $message }}&lt;/span&gt;
                @enderror
            &lt;/div&gt;
     
            &lt;div class=&quot;mb-3&quot;&gt;
                &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Upload&lt;/button&gt;
            &lt;/div&gt;
         
        &lt;/form&gt;
        
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
      
&lt;/html&gt;
</pre>
<h3 class="step_code">Run Laravel App:</h3>
<pre class="brush: php; title: ; notranslate">
php artisan serve
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/">How to Upload Files to MySQL Database in Laravel 10</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/how-to-upload-files-to-mysql-database-in-laravel-10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
