<?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: Uthman Ehsan</title>
    <description>The latest articles on Forem by Uthman Ehsan (@uthman_ehsan_23f3a68c03d7).</description>
    <link>https://forem.com/uthman_ehsan_23f3a68c03d7</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%2F2138697%2F20ff5bc7-e364-4a3c-b984-115091a175e5.png</url>
      <title>Forem: Uthman Ehsan</title>
      <link>https://forem.com/uthman_ehsan_23f3a68c03d7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/uthman_ehsan_23f3a68c03d7"/>
    <language>en</language>
    <item>
      <title>Migrations in Laravel</title>
      <dc:creator>Uthman Ehsan</dc:creator>
      <pubDate>Sat, 28 Sep 2024 12:17:11 +0000</pubDate>
      <link>https://forem.com/uthman_ehsan_23f3a68c03d7/migrations-in-laravel-4k52</link>
      <guid>https://forem.com/uthman_ehsan_23f3a68c03d7/migrations-in-laravel-4k52</guid>
      <description>&lt;p&gt;Migrations are like version control for your database, allowing your team to define and share the application's database schema definition. &lt;/p&gt;

&lt;p&gt;If you have ever had to tell a teammate to manually add a column to their local database schema after pulling in your changes from source control, you've faced the problem that database migrations solve.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Making Migrations &lt;br&gt;
*&lt;/em&gt; &lt;br&gt;
One can generate migrations using below artisan commands&lt;br&gt;
&lt;/p&gt;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Laravel is smart enough to generate up and down functions under migrations folder with date and migration name  under&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; database/migrations/2023_09_28_create_multisign_table.php (File)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with auto code generated for up and down methods. &lt;/p&gt;

&lt;p&gt;** Running Migrations&lt;br&gt;
** &lt;br&gt;
 To run all of your outstanding migrations, execute the migrate Artisan command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you created more migrations and they are not migrated yet, to run only a specific migration use this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate --path=/database/migrations/2023_09_28_create_multisign_table.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Above migrations will only run if there is no entry before in the migrations table.**&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;_ Check migrations table , if there is an entry for 2023_09_28_create_multisign_table than this particular migration &lt;br&gt;
 wont run at all even running either of above commands. *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In order to run the migration , one has to delete the entry from database manually and execute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; php artisan migrate:refresh

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Coding !&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>php</category>
      <category>laravel</category>
    </item>
    <item>
      <title>405 Error Laravel Sanctum</title>
      <dc:creator>Uthman Ehsan</dc:creator>
      <pubDate>Sat, 28 Sep 2024 11:31:38 +0000</pubDate>
      <link>https://forem.com/uthman_ehsan_23f3a68c03d7/405-error-laravel-sanctum-h1l</link>
      <guid>https://forem.com/uthman_ehsan_23f3a68c03d7/405-error-laravel-sanctum-h1l</guid>
      <description>&lt;p&gt;Laravel Sanctum provides a featherweight authentication system for SPAs (single page applications), mobile applications, and simple, token based APIs. &lt;/p&gt;

&lt;p&gt;Sanctum allows each user of your application to generate multiple API tokens for their account. &lt;/p&gt;

&lt;p&gt;However it differs from JWT tokens where the user information is encoded in a string and passed onto server every time request is sent back to server. &lt;/p&gt;

&lt;p&gt;Whereas sanctum is more like a stateful session with limited or life time expiration. One can set the expiration under&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config/sanctum.php  
'expiration' =&amp;gt; null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One can guard or protect the the respective methods under guard configure that would need to send authentication token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Request the resource to get token 

Route::post('/users_new', [UserController::class, 'store']);

public users_new(){

   $user = User::create( $request-&amp;gt;user_data );

   $token = $user-&amp;gt;createToken('api-token')-&amp;gt;plainTextToken;

   return response()-&amp;gt;json([
               'token' =&amp;gt; $token,
                'data' =&amp;gt; $user,
            ], 200);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to update the user data , it will have to pass the token along with request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::middleware('auth:sanctum')-&amp;gt;group(function () {
  // all the requests coming inside this middleware will have to pass the token.

//User
Route::post('/updateUser', [UserController::class, 'upgradeUser']);

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If above is executed , it might throw an &lt;strong&gt;405 Authorization error&lt;/strong&gt; because at this moment our webserver doesnot accepts&lt;br&gt;
any header tokens . To achieve this we need to add below .**htaccess **file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;(right after the RewriteBase)&lt;/strong&gt; so our htaccess code &lt;br&gt;
becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;IfModule mod_rewrite.c&amp;gt;
    RewriteEngine On
    RewriteBase /
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
&amp;lt;/IfModule&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we need to test above in PostMan , one must add the BearerToken as HeaderAuthorization as depicted in below diagram.&lt;/p&gt;

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

&lt;p&gt;Happy Coding !&lt;/p&gt;

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