<?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: LinaAhmadGhojan</title>
    <description>The latest articles on Forem by LinaAhmadGhojan (@linaahmadghojan).</description>
    <link>https://forem.com/linaahmadghojan</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%2F1045557%2F56040368-ab24-46b6-a6ac-cb7c6ddbe97d.png</url>
      <title>Forem: LinaAhmadGhojan</title>
      <link>https://forem.com/linaahmadghojan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/linaahmadghojan"/>
    <language>en</language>
    <item>
      <title>Laravel Multi Auth using Guards with Example (Api)</title>
      <dc:creator>LinaAhmadGhojan</dc:creator>
      <pubDate>Wed, 15 Mar 2023 07:45:55 +0000</pubDate>
      <link>https://forem.com/linaahmadghojan/laravel-multi-auth-using-guards-with-example-api-5hmb</link>
      <guid>https://forem.com/linaahmadghojan/laravel-multi-auth-using-guards-with-example-api-5hmb</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nXXQJ8c5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjkps0pvxu55v2s2d1mt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nXXQJ8c5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjkps0pvxu55v2s2d1mt.png" alt="Image description" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;install laravel global&lt;br&gt;
&lt;code&gt;composer global require laravel/installer&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;creat project &lt;br&gt;
&lt;code&gt;laravel new example-app&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;run project &lt;br&gt;
&lt;code&gt;cd example-app&lt;br&gt;
php artisan serve&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database Configuration&lt;br&gt;
&lt;code&gt;DB_CONNECTION=mysql&lt;br&gt;
DB_HOST=127.0.0.1&lt;br&gt;
DB_PORT=3306&lt;br&gt;
DB_DATABASE=database_name&lt;br&gt;
DB_USERNAME=database_username&lt;br&gt;
DB_PASSWORD=database_password&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create migration and model for admins&lt;br&gt;
To make the model and migration for admins table, run the following command:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;php artisan make:model Admin -m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will create migration and model both. From the database/migrations directory, open the admin's migrations file, and edit it as follows:&lt;br&gt;
&lt;code&gt;public function up()&lt;br&gt;
{&lt;br&gt;
    Schema::create('admins', function (Blueprint $table) {&lt;br&gt;
        $table-&amp;gt;increments('id');&lt;br&gt;
        $table-&amp;gt;string('name');&lt;br&gt;
        $table-&amp;gt;string('email')-&amp;gt;unique();&lt;br&gt;
        $table-&amp;gt;string('password');&lt;br&gt;
        $table-&amp;gt;rememberToken();&lt;br&gt;
        $table-&amp;gt;timestamps();&lt;br&gt;
    });&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;migrate database&lt;br&gt;
&lt;code&gt;php artisan migrate&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the Admin model in app/Models/Admin.php and add the following:&lt;br&gt;
&lt;code&gt;use Illuminate\Notifications\Notifiable;&lt;br&gt;
use Illuminate\Foundation\Auth\User as Authenticatable;&lt;br&gt;
class Admin extends Authenticatable&lt;br&gt;
{&lt;br&gt;
use Notifiable;&lt;br&gt;
protected $guard = 'admin';&lt;br&gt;
protected $fillable = [&lt;br&gt;
    'name', 'email', 'password',&lt;br&gt;
];&lt;br&gt;
protected $hidden = [&lt;br&gt;
  'password', 'remember_token',&lt;br&gt;
];&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define the guards , open config/auth.php file &lt;br&gt;
before adding a new guard, first, we need to add the provider for that because all authentication drivers have a user provider. Let’s add a user provider for admin users where the driver is eloquent and the model is an Admin model class. After adding the provider array will look like below:&lt;br&gt;
&lt;code&gt;'providers' =&amp;gt; [&lt;br&gt;
'users' =&amp;gt; [&lt;br&gt;
    'driver' =&amp;gt; 'eloquent',&lt;br&gt;
    'model' =&amp;gt; App\Models\User::class,&lt;br&gt;
],&lt;br&gt;
'admins' =&amp;gt; [&lt;br&gt;
    'driver' =&amp;gt; 'eloquent',&lt;br&gt;
    'model' =&amp;gt; App\Models\Admin::class,&lt;br&gt;
]&lt;br&gt;
]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;now we will create a new guard for admin users, where the driver will be session and the provider is admins. after adding that guard array will look like below:&lt;br&gt;
&lt;code&gt;'guards' =&amp;gt; [&lt;br&gt;
    'web' =&amp;gt; [&lt;br&gt;
        'driver' =&amp;gt; 'session',&lt;br&gt;
        'provider' =&amp;gt; 'users',&lt;br&gt;
    ],&lt;br&gt;
    'admin' =&amp;gt; [&lt;br&gt;
        'driver' =&amp;gt; 'session',&lt;br&gt;
        'provider' =&amp;gt; 'admins',&lt;br&gt;
    ],&lt;br&gt;
    'api' =&amp;gt; [&lt;br&gt;
        'driver' =&amp;gt; 'token',&lt;br&gt;
        'provider' =&amp;gt; 'users',&lt;br&gt;
        'hash' =&amp;gt; false,&lt;br&gt;
    ]&lt;br&gt;
]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;now got to (app\Providers\RouteServiceProvider.php)&lt;br&gt;
for make routes admin in file (admin.php)&lt;br&gt;
write :&lt;br&gt;
`  */&lt;br&gt;
public function boot(): void&lt;br&gt;
{&lt;br&gt;
    $this-&amp;gt;configureRateLimiting();&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$this-&amp;gt;routes(function () {
    Route::middleware('api')
        -&amp;gt;prefix('api')
        -&amp;gt;group(base_path('routes/api.php'));

    Route::middleware('web')
        -&amp;gt;group(base_path('routes/web.php'));

            // Admin User
    Route::prefix('admin')-&amp;gt;name('admin.')
    -&amp;gt;namespace($this-&amp;gt;namespace)
    -&amp;gt;group(base_path('routes/admin.php'));

});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;br&gt;
`&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create file route in path (routes\admin.php)&lt;br&gt;
now write all routes for the &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;admin &lt;br&gt;
in this file&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>multi</category>
      <category>auth</category>
      <category>guard</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
