<?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>DEV Community: Raja Hunain</title>
    <description>The latest articles on DEV Community by Raja Hunain (@rajadevsolutions).</description>
    <link>https://dev.to/rajadevsolutions</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4255598%2F419caaeb-6a3d-471c-a6ed-d18051f853d8.png</url>
      <title>DEV Community: Raja Hunain</title>
      <link>https://dev.to/rajadevsolutions</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajadevsolutions"/>
    <language>en</language>
    <item>
      <title>5 MySQL Connection Errors in Laravel (and How I Fix Each One)</title>
      <dc:creator>Raja Hunain</dc:creator>
      <pubDate>Sat, 21 Nov 2026 14:20:01 +0000</pubDate>
      <link>https://dev.to/rajadevsolutions/5-mysql-connection-errors-in-laravel-and-how-i-fix-each-one-3mho</link>
      <guid>https://dev.to/rajadevsolutions/5-mysql-connection-errors-in-laravel-and-how-i-fix-each-one-3mho</guid>
      <description>&lt;p&gt;You run &lt;code&gt;php artisan migrate&lt;/code&gt;, and instead of a clean table list you get a wall of red. MySQL connection errors in Laravel all look similar at first glance, but each SQLSTATE code points at a different problem — and a different fix. These are the five I see most often, what each one actually means, and the fix that works.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. SQLSTATE[HY000] [2002] Connection refused
&lt;/h2&gt;

&lt;p&gt;The most common one. PHP asked the operating system to open a connection to the host and port in your &lt;code&gt;.env&lt;/code&gt; — and nothing answered. No SQL ran, and your username and password were never even checked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; first check whether MySQL is actually running (&lt;code&gt;sudo systemctl status mysql&lt;/code&gt;), then check the host and port. The classic trap is &lt;code&gt;DB_HOST=localhost&lt;/code&gt;, which makes PHP use a Unix socket instead of TCP. Switching to &lt;code&gt;DB_HOST=127.0.0.1&lt;/code&gt; fixes it in most local setups.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. SQLSTATE[HY000] [1045] Access denied
&lt;/h2&gt;

&lt;p&gt;This one is different: the server &lt;em&gt;was&lt;/em&gt; reached, but it rejected your credentials. So this is the only error on this list where the username and password are actually the problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; verify the user exists in MySQL and has rights on your database, and check for typos in &lt;code&gt;DB_USERNAME&lt;/code&gt; and &lt;code&gt;DB_PASSWORD&lt;/code&gt;. Also confirm the user is allowed to connect from your host — a user created for &lt;code&gt;localhost&lt;/code&gt; cannot always connect over &lt;code&gt;127.0.0.1&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. SQLSTATE[HY000] [1049] Unknown database
&lt;/h2&gt;

&lt;p&gt;The server is up, the credentials are right, but the database name in &lt;code&gt;.env&lt;/code&gt; does not exist on that server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; create it (&lt;code&gt;CREATE DATABASE your_database;&lt;/code&gt;) or correct &lt;code&gt;DB_DATABASE&lt;/code&gt;. This one appears constantly after cloning a project on a fresh machine, because the &lt;code&gt;.env&lt;/code&gt; travels with the code but the database does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. SQLSTATE[HY000] [2002] No such file or directory
&lt;/h2&gt;

&lt;p&gt;Same family as error 1, seen from the other side: PHP went looking for the MySQL socket file and it was not there. Again, &lt;code&gt;localhost&lt;/code&gt; is the trigger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; identical — switch &lt;code&gt;DB_HOST&lt;/code&gt; to &lt;code&gt;127.0.0.1&lt;/code&gt; so PHP connects over TCP instead of hunting for a socket file.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. SQLSTATE[HY000] [2006] MySQL server has gone away
&lt;/h2&gt;

&lt;p&gt;Your app connected fine, then the connection died mid-request. Long-running imports, huge queries, or a server that closed an idle connection are the usual causes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; for imports, process rows in chunks instead of loading everything at once. If it happens on idle connections, check the server's &lt;code&gt;wait_timeout&lt;/code&gt; — and in queued jobs, reconnect before heavy work instead of assuming the connection from the start of the job is still alive.&lt;/p&gt;

&lt;h2&gt;
  
  
  The habit that saves the most time
&lt;/h2&gt;

&lt;p&gt;When a connection error appears, do not start by editing &lt;code&gt;.env&lt;/code&gt; at random. Test outside Laravel first — &lt;code&gt;nc -zv 127.0.0.1 3306&lt;/code&gt; tells you in one second whether anything is listening at all. If nothing is listening, Laravel is innocent and the server is the problem. If something &lt;em&gt;is&lt;/em&gt; listening, compare what the MySQL client used with what your &lt;code&gt;.env&lt;/code&gt; says. That one test splits every error on this list in half.&lt;/p&gt;

&lt;p&gt;One more habit: after any &lt;code&gt;.env&lt;/code&gt; change, run &lt;code&gt;php artisan config:clear&lt;/code&gt;. A cached config makes Laravel ignore your &lt;code&gt;.env&lt;/code&gt; completely, and you will keep debugging a problem you already fixed.&lt;/p&gt;

&lt;p&gt;If you want the full walkthrough for the first error on this list — all six causes of Connection refused, including the Docker trap and the startup race — it is here: &lt;a href="https://rajadevsolutions.online/errors/how-to-fix-sqlstate-hy000-2002-connection-refused-laravel" rel="noopener noreferrer"&gt;https://rajadevsolutions.online/errors/how-to-fix-sqlstate-hy000-2002-connection-refused-laravel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which of these five wasted the most of your time? I am curious which one I missed.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>mysql</category>
      <category>debugging</category>
    </item>
    <item>
      <title>6 Laravel API Errors I Keep Fixing (and the One-Line Fix for Each)</title>
      <dc:creator>Raja Hunain</dc:creator>
      <pubDate>Sat, 21 Nov 2026 13:09:58 +0000</pubDate>
      <link>https://dev.to/rajadevsolutions/6-laravel-api-errors-i-keep-fixing-and-the-one-line-fix-for-each-1mcl</link>
      <guid>https://dev.to/rajadevsolutions/6-laravel-api-errors-i-keep-fixing-and-the-one-line-fix-for-each-1mcl</guid>
      <description>&lt;p&gt;You build your first Laravel API, hit send in Postman, and instead of JSON you get an error page. I have been there. These are the six errors that show up for almost every beginner, what each one actually means, and the fix — usually one line.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Route [login] not defined
&lt;/h2&gt;

&lt;p&gt;This is the most confusing one, because it is not a routing problem at all. Laravel thought your request came from a browser and tried to redirect you to a login page that does not exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; send the &lt;code&gt;Accept: application/json&lt;/code&gt; header with every API request. That single header tells Laravel this is an API call, and it answers with JSON instead of a redirect.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. MassAssignmentException
&lt;/h2&gt;

&lt;p&gt;You POST a new record and Laravel refuses, complaining about mass assignment. It is protecting you: by default no field is mass-assignable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; add the fields to &lt;code&gt;$fillable&lt;/code&gt; in your model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$fillable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'body'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. 401 Unauthenticated
&lt;/h2&gt;

&lt;p&gt;Your token is missing, expired, or not being sent in the format Laravel expects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; send it as a Bearer token — &lt;code&gt;Authorization: Bearer &amp;lt;your-token&amp;gt;&lt;/code&gt; — and log in again to get a fresh one if it expired. With Sanctum, &lt;code&gt;currentAccessToken()-&amp;gt;delete()&lt;/code&gt; on logout means old tokens stop working immediately.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. This action is unauthorized
&lt;/h2&gt;

&lt;p&gt;Your request reaches the Form Request, and its &lt;code&gt;authorize()&lt;/code&gt; method returns &lt;code&gt;false&lt;/code&gt;. Laravel's generated Form Requests default to &lt;code&gt;false&lt;/code&gt;, so this bites everyone exactly once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; return &lt;code&gt;true&lt;/code&gt; from &lt;code&gt;authorize()&lt;/code&gt; — or put your real authorization logic there, like checking the user owns the record.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. 404 on a record that should exist
&lt;/h2&gt;

&lt;p&gt;Two usual causes: the id genuinely does not exist, or the route is wrong and Laravel matched a different route. Route model binding turns a missing model into a clean JSON 404, which is the correct behaviour — so first check the id you sent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; verify the id, then run &lt;code&gt;php artisan route:list --path=api&lt;/code&gt; and confirm the route, verb, and parameter name match what you called.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. 405 Method Not Allowed
&lt;/h2&gt;

&lt;p&gt;You called the route with the wrong HTTP verb — a POST against a GET-only route, or a PUT where the route expects PATCH.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; match the verb to the action: GET reads, POST creates, PUT/PATCH updates, DELETE deletes. A &lt;code&gt;Route::apiResource&lt;/code&gt; gives you all five with the right verbs already wired up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pattern behind all six
&lt;/h2&gt;

&lt;p&gt;Five of these six errors are not bugs in your code. They are Laravel telling you, in its own way, that one small piece of the contract is missing: a header, a fillable field, a token, a &lt;code&gt;true&lt;/code&gt;, the right id, or the right verb. Fix the contract and the error goes away.&lt;/p&gt;

&lt;p&gt;If you want the full walkthrough these fixes come from — building the whole API with Sanctum auth, API Resources, validation and curl tests — it is here: &lt;a href="https://rajadevsolutions.online/blog/how-to-create-a-rest-api-in-laravel-13" rel="noopener noreferrer"&gt;https://rajadevsolutions.online/blog/how-to-create-a-rest-api-in-laravel-13&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What error wasted the most of your time on your first API? I am curious which one I missed.&lt;/p&gt;

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