<?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: Mohamed Fakhr</title>
    <description>The latest articles on DEV Community by Mohamed Fakhr (@mohamed_fakhr_eldin).</description>
    <link>https://dev.to/mohamed_fakhr_eldin</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%2F4031725%2F883b6001-be89-4ff4-bb2e-bcde82653847.jpeg</url>
      <title>DEV Community: Mohamed Fakhr</title>
      <link>https://dev.to/mohamed_fakhr_eldin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohamed_fakhr_eldin"/>
    <language>en</language>
    <item>
      <title>SaaS (Software as a Service)</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Sat, 29 Aug 2026 15:06:10 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/saas-software-as-a-service-32oa</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/saas-software-as-a-service-32oa</guid>
      <description>&lt;p&gt;When building a SaaS (Software as a Service) project, the database architecture is one of the most important decisions.&lt;br&gt;
There are two common approaches to multi‑tenant SaaS database design:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1:&lt;/strong&gt; &lt;br&gt;
One database per client (separate database per tenant)&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
Each client (tenant) gets their own database.&lt;/p&gt;

&lt;p&gt;Your application (same codebase) connects dynamically to the tenant’s database based on their domain, subdomain, or login.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strong data isolation – clients’ data is completely separated.&lt;/li&gt;
&lt;li&gt;Easier to migrate/export data or comply with regulations (e.g., GDPR).&lt;/li&gt;
&lt;li&gt;Less risk that a bug or query exposes other tenants’ data.&lt;/li&gt;
&lt;li&gt;You can scale some clients separately if needed (e.g., move a big client’s DB to a bigger server).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Harder to manage upgrades or schema changes (you must migrate many databases).&lt;/li&gt;
&lt;li&gt;Higher infrastructure cost (many DB instances).&lt;/li&gt;
&lt;li&gt;More complex DevOps (backups, monitoring, migrations for each DB).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Option 2:&lt;/strong&gt; &lt;br&gt;
All clients share the same database (single database, multi‑tenant)&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
One database contains all tenants’ data.&lt;/p&gt;

&lt;p&gt;Every table that stores tenant-specific data has a tenant_id (or company_id) column to separate data logically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easier to manage (only one DB schema to upgrade).&lt;/li&gt;
&lt;li&gt;Cheaper to host (one DB server).&lt;/li&gt;
&lt;li&gt;Easier to run analytics across tenants.&lt;/li&gt;
&lt;li&gt;Simpler CI/CD pipeline.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security risk if not done carefully – you must always filter by tenant_id in every query, otherwise data leaks between tenants.&lt;/li&gt;
&lt;li&gt;Harder to export one client’s data separately.&lt;/li&gt;
&lt;li&gt;A large single DB can become a bottleneck if you have thousands of tenants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What to consider if using a single database:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add tenant_id to all tenant-specific tables.&lt;/li&gt;
&lt;li&gt;Apply row-level security or strict query filters (e.g., in Laravel use global scopes or middleware).&lt;/li&gt;
&lt;li&gt;Use indexes on tenant_id to keep queries fast.&lt;/li&gt;
&lt;li&gt;Be careful with shared tables (like product catalog) vs tenant-specific tables (like orders).&lt;/li&gt;
&lt;li&gt;Backups: you’re backing up everyone’s data together, so plan recovery carefully.&lt;/li&gt;
&lt;li&gt;Scaling: eventually you may need read replicas, sharding by tenant_id, or moving heavy tenants to their own DB.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Which one should you choose:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you expect a few big enterprise clients that might demand strict isolation or their own backups → use separate databases per client.&lt;/li&gt;
&lt;li&gt;If you expect many small clients (e.g., hundreds/thousands of small shops) → use one shared database with tenant_id (multi‑tenant).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Single DB:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add tenant_id to tables.&lt;/li&gt;
&lt;li&gt;Use middleware to set where('tenant_id', $tenantId) globally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Multi DB:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a tenants table in a main DB storing DB connection info (host, username, password).&lt;/li&gt;
&lt;li&gt;On request, resolve the tenant and dynamically set the DB connection (using DB::purge() / DB::reconnect()).&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>saas</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Laravel Application Optimization</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Mon, 24 Aug 2026 17:46:52 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/laravel-application-optimization-1pko</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/laravel-application-optimization-1pko</guid>
      <description>&lt;p&gt;A. &lt;u&gt;&lt;strong&gt;Database &amp;amp; Eloquent Optimization&lt;/strong&gt;&lt;br&gt;
&lt;/u&gt;1- &lt;strong&gt;Eager Loading (N+1 Problem):&lt;/strong&gt;&lt;br&gt;
Avoid the N+1 query problem by eager loading relationships when querying models.&lt;br&gt;
&lt;u&gt;Bad (N+1):&lt;/u&gt;&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="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Each user call is a new query&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Good (Eager Loading):&lt;/u&gt;&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="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Only 2 queries (Post + User)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- &lt;strong&gt;Select Specific Columns:&lt;/strong&gt;&lt;br&gt;
Don't select all columns &lt;code&gt;select('*')&lt;/code&gt; if you only need a few.&lt;br&gt;
  &lt;code&gt;$users = User::select('id', 'name', 'email')-&amp;gt;get();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3- &lt;strong&gt;Use Indexes:&lt;/strong&gt;&lt;br&gt;
Ensure your database columns that are frequently used in &lt;code&gt;WHERE, JOIN, ORDER BY&lt;/code&gt; clauses have proper indexes.&lt;br&gt;
Add indexes in your migrations:&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="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Unique automatically creates an index&lt;/span&gt;
&lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'category_id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Custom index&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- &lt;strong&gt;Pagination for Large Datasets:&lt;/strong&gt;&lt;br&gt;
Never load all records if you have a large table. Use &lt;code&gt;paginate()&lt;/code&gt; or &lt;code&gt;simplePaginate()&lt;/code&gt;.&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="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5- &lt;strong&gt;chunk() for Processing Large Datasets:&lt;/strong&gt;&lt;br&gt;
When processing many records (e.g., for background jobs), use &lt;code&gt;chunk()&lt;/code&gt; or &lt;code&gt;cursor()&lt;/code&gt; to avoid loading all records into memory at once.&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="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// Process post&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;p&gt;6- &lt;strong&gt;exists() instead of count() for existence checks:&lt;/strong&gt;&lt;br&gt;
If you just need to know if a record exists, &lt;code&gt;exists()&lt;/code&gt; is more efficient than &lt;code&gt;count()&lt;/code&gt; as it stops at the first match.&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;B. &lt;u&gt;&lt;strong&gt;Caching&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
1- &lt;strong&gt;Route Caching:&lt;/strong&gt;&lt;br&gt;
If you have many routes, caching them significantly speeds up route registration.&lt;br&gt;
Run in production: &lt;code&gt;php artisan route:cache&lt;/code&gt;&lt;br&gt;
Remember to clear it &lt;code&gt;php artisan route:clear&lt;/code&gt; and re-cache after any route changes.&lt;br&gt;
2- &lt;strong&gt;Configuration Caching:&lt;/strong&gt;&lt;br&gt;
Combines all your configuration files into a single file for faster loading.&lt;br&gt;
Run in production: &lt;code&gt;php artisan config:cache&lt;/code&gt;&lt;br&gt;
Clear &lt;code&gt;php artisan config:clear&lt;/code&gt; and re-cache after config changes.&lt;br&gt;
3- &lt;strong&gt;View Caching:&lt;/strong&gt;&lt;br&gt;
Caches your Blade compiled views, so they don't have to be recompiled on every request.&lt;br&gt;
Run in production: &lt;code&gt;php artisan view:cache&lt;/code&gt;&lt;br&gt;
Clear &lt;code&gt;php artisan view:clear&lt;/code&gt; after view changes.&lt;br&gt;
4- &lt;strong&gt;Application Data Caching:&lt;/strong&gt;&lt;br&gt;
Cache frequently accessed data that doesn't change often (e.g., settings, categories, static content).&lt;br&gt;
Use &lt;code&gt;Cache::remember()&lt;/code&gt;, &lt;code&gt;Cache::put()&lt;/code&gt;, &lt;code&gt;Cache::get()&lt;/code&gt; with a suitable driver (Redis or Memcached are much faster than file-based).&lt;/p&gt;




&lt;p&gt;C. &lt;u&gt;&lt;strong&gt;Queues (Background Processing)&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;Offload Heavy Tasks:&lt;/strong&gt;&lt;br&gt;
Move time-consuming tasks (email sending, image processing, video encoding, complex calculations, third-party API calls) to background queues.&lt;br&gt;
This allows your web requests to return a response quickly, improving user experience.&lt;br&gt;
Example:&lt;br&gt;
&lt;u&gt;Instead of:&lt;/u&gt;&lt;br&gt;
&lt;code&gt;Mail::to($user-&amp;gt;email)-&amp;gt;send(new WelcomeEmail($user));&lt;/code&gt;&lt;br&gt;
&lt;u&gt;Use a job:&lt;/u&gt;&lt;br&gt;
&lt;code&gt;dispatch(new App\Jobs\SendWelcomeEmail($user));&lt;/code&gt;&lt;br&gt;
Remember to configure a queue driver (e.g., database, redis, sqs) and run a queue worker &lt;code&gt;php artisan queue:work&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;D. &lt;u&gt;&lt;strong&gt;Asset Optimization&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
1- &lt;strong&gt;CDN (Content Delivery Network):&lt;/strong&gt;&lt;br&gt;
Serve static assets (images, CSS, JS) from a CDN to reduce latency for users geographically distant from your server.&lt;br&gt;
2- &lt;strong&gt;Image Optimization:&lt;/strong&gt;&lt;br&gt;
Compress images before deploying. (using tools like TinyPNG, ImageOptim, or build tools) &lt;br&gt;
3- &lt;strong&gt;Minification:&lt;/strong&gt;&lt;br&gt;
Combine multiple CSS files into one and multiple JS files into one.&lt;br&gt;
Minify CSS and JavaScript to remove whitespace and comments.&lt;/p&gt;




&lt;p&gt;E. &lt;u&gt;&lt;strong&gt;PHP &amp;amp; Server Environment&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
1- &lt;strong&gt;Use the Latest PHP Version:&lt;/strong&gt;&lt;br&gt;
Always use the latest stable PHP version. Each new version brings significant performance improvements.&lt;br&gt;
2- &lt;strong&gt;Enable OPcache:&lt;/strong&gt;&lt;br&gt;
OPcache is a PHP extension that caches precompiled script bytecode in memory, avoiding the parsing and compilation on every request. It's crucial for PHP application performance.&lt;br&gt;
Ensure it's enabled and configured correctly in your php.ini.&lt;br&gt;
3- &lt;strong&gt;Composer Optimization:&lt;/strong&gt;&lt;br&gt;
In production, install Composer dependencies without dev dependencies:&lt;br&gt;
&lt;code&gt;composer install --no-dev --optimize-autoloader&lt;br&gt;
&lt;/code&gt; --optimize-autoloader builds a faster class map.&lt;br&gt;
4- &lt;strong&gt;JIT Compiler (PHP 8+):&lt;/strong&gt;&lt;br&gt;
PHP 8's JIT (Just-In-Time) compiler can provide significant performance boosts for CPU-bound tasks.&lt;br&gt;
Ensure it's enabled and configured in php.ini if your workload can benefit from it.&lt;br&gt;
5- &lt;strong&gt;HTTP/2 and GZIP Compression:&lt;/strong&gt;&lt;br&gt;
Configure your web server (Nginx or Apache) to use HTTP/2 and GZIP compression.&lt;br&gt;
&lt;u&gt;HTTP/2:&lt;/u&gt; Allows multiple requests/responses over a single connection, reducing latency.&lt;br&gt;
&lt;u&gt;GZIP Compression:&lt;/u&gt; Compresses text-based assets (HTML, CSS, JS) before sending them to the client, reducing bandwidth usage.&lt;br&gt;
6- &lt;strong&gt;Proper Server Configuration:&lt;/strong&gt;&lt;br&gt;
Use a fast web server like &lt;strong&gt;Nginx&lt;/strong&gt; (preferred over Apache for performance).&lt;br&gt;
Tune &lt;strong&gt;PHP-FPM&lt;/strong&gt; settings (e.g., pm.max_children, pm.start_servers) based on your server's resources and traffic.&lt;br&gt;
7- &lt;strong&gt;Choose a Good Hosting Provider:&lt;/strong&gt;&lt;br&gt;
A cheap, overloaded shared host will negate many of your optimization efforts. Invest in a good VPS or dedicated server.&lt;/p&gt;




&lt;p&gt;F. &lt;u&gt;&lt;strong&gt;Code Quality &amp;amp; Best Practices:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
1- &lt;strong&gt;Remove Unused Packages &amp;amp; Services:&lt;/strong&gt;&lt;br&gt;
Audit your composer.json and config/app.php. Remove any packages or service providers you don't actually need.&lt;br&gt;
2- &lt;strong&gt;Limit Middleware Usage:&lt;/strong&gt;&lt;br&gt;
Middleware adds overhead. Only apply middleware where absolutely necessary.&lt;br&gt;
3- &lt;strong&gt;Don't Use APP_DEBUG=true in Production:&lt;/strong&gt;&lt;br&gt;
This is a major performance killer. Set APP_DEBUG=false in your .env for production.&lt;br&gt;
4- &lt;strong&gt;Minimize Logging:&lt;/strong&gt;&lt;br&gt;
Adjust LOG_LEVEL in production (e.g., warning or error) to reduce disk I/O from excessive logging.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>performance</category>
    </item>
    <item>
      <title>Security Features in Laravel</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Sun, 23 Aug 2026 18:11:04 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/security-features-in-laravel-g45</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/security-features-in-laravel-g45</guid>
      <description>&lt;p&gt;1- &lt;strong&gt;Authentication&lt;/strong&gt;&lt;br&gt;
Laravel offers a complete authentication system that can be customized to fit your needs. The built-in &lt;strong&gt;Auth&lt;/strong&gt; facade provides methods to manage user registration, login, logout, password reset, and email verification.&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="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Check if the user is authenticated&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The user is logged in&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;2- &lt;strong&gt;Authorization&lt;/strong&gt;&lt;br&gt;
Laravel includes a simple and easy-to-use authorization system. You can use policies and gates to control user access to various parts of your application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using policy:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a policy&lt;/span&gt;
&lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="n"&gt;artisan&lt;/span&gt; &lt;span class="n"&gt;make&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;policy&lt;/span&gt; &lt;span class="nc"&gt;PostPolicy&lt;/span&gt;

&lt;span class="c1"&gt;// Register the policy in AuthServiceProvider&lt;/span&gt;
&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$policies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'App\Models\Post'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'App\Policies\PostPolicy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Define a method in PostPolicy&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Post&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user_id&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;ul&gt;
&lt;li&gt;Using gates:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Gate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Gate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'update-post'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Gate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;allows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'update-post'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The user can update the post&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;3- &lt;strong&gt;CSRF Protection&lt;/strong&gt;&lt;br&gt;
Laravel automatically generates a CSRF token for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In HTML forms:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/example"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      @csrf
      &lt;span class="c"&gt;&amp;lt;!-- Your form inputs here --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;4- &lt;strong&gt;Hashing&lt;/strong&gt;&lt;br&gt;
Laravel provides a secure way to hash passwords using the &lt;strong&gt;Hash&lt;/strong&gt; facade.&lt;/p&gt;

&lt;p&gt;Usage:&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="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Hashing a password&lt;/span&gt;
&lt;span class="nv"&gt;$hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Checking a password against a hash&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$hashedPassword&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The passwords match...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;5- &lt;strong&gt;Input Validation&lt;/strong&gt;&lt;br&gt;
Laravel provides a powerful validation mechanism to ensure that the data received from the user is valid before it is processed.&lt;/p&gt;

&lt;p&gt;Usage:&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="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Http\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$validated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required|max:255'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'body'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="c1"&gt;// The data is valid...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;6- &lt;strong&gt;SQL Injection Protection&lt;/strong&gt;&lt;br&gt;
By using Eloquent ORM or the query builder, Laravel protects your application from SQL injection attacks.&lt;/p&gt;

&lt;p&gt;Usage:&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="c1"&gt;// Using Eloquent ORM&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Using query builder&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;7- &lt;strong&gt;XSS Protection&lt;/strong&gt;&lt;br&gt;
Laravel automatically escapes data that is output in views to prevent cross-site scripting (XSS) attacks.&lt;/p&gt;

&lt;p&gt;Usage: Blade&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Blade template --&amp;gt;&lt;/span&gt;
{{ $userInput }} &lt;span class="c"&gt;&amp;lt;!-- This will be escaped --&amp;gt;&lt;/span&gt;
{!! $userInput !!} &lt;span class="c"&gt;&amp;lt;!-- This will not be escaped --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;8- &lt;strong&gt;Rate Limiting&lt;/strong&gt;&lt;br&gt;
Laravel provides rate limiting to prevent abuse of your application by limiting the number of requests a user can make to your application.&lt;/p&gt;

&lt;p&gt;Usage:&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="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\RateLimiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;RateLimiter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'global'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Limit&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;perMinute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&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;p&gt;9- &lt;strong&gt;File Upload Security&lt;/strong&gt;&lt;br&gt;
When handling file uploads, Laravel provides methods to ensure that the uploaded files are of expected types and sizes.&lt;/p&gt;

&lt;p&gt;Usage:&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="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'photo'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required|file|mimes:jpeg,png,jpg,gif|max:2048'&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;p&gt;&lt;strong&gt;Best Practices:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Always use the latest version of Laravel to ensure you have the latest security updates.&lt;/li&gt;
&lt;li&gt;Regularly update your dependencies using Composer.&lt;/li&gt;
&lt;li&gt;Use environment variables to manage sensitive information.&lt;/li&gt;
&lt;li&gt;Set proper permissions on your server to restrict access to files and directories.&lt;/li&gt;
&lt;li&gt;Regularly back up your data and test your backup strategy.&lt;/li&gt;
&lt;li&gt;Monitor and log your application for unusual activity and sensitive actions.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>security</category>
    </item>
    <item>
      <title>RESTful APIs</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Sat, 15 Aug 2026 22:05:11 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/restful-apis-452h</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/restful-apis-452h</guid>
      <description>&lt;p&gt;First, what is the difference between RESTful APIs and REST APIs?&lt;br&gt;
&lt;strong&gt;REST API:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used for simple projects and doesn't require a complex system.&lt;/li&gt;
&lt;li&gt;It offers flexibility and is easy to implement, especially for smaller applications or quick prototypes.&lt;/li&gt;
&lt;li&gt;Based on REST principles, but not strictly following all constraints.&lt;/li&gt;
&lt;li&gt;Might use POST or GET for all data transfers.&lt;/li&gt;
&lt;li&gt;Can represent resources in various formats (XML, JSON, HTML, etc.)&lt;/li&gt;
&lt;li&gt;Can be flexible and varied (e.g., /deleteUser?id=5).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;RESTful APIs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used for large-scale, Complex Applications.&lt;/li&gt;
&lt;li&gt;For performance and scalability if the project requires optimized performance with high scalability and handles large volumes of data and high traffic.&lt;/li&gt;
&lt;li&gt;Strictly according to REST standards.&lt;/li&gt;
&lt;li&gt;Strictly uses HTTP methods in a RESTful manner (GET, POST, PUT, DELETE)&lt;/li&gt;
&lt;li&gt;Follows a uniform format for resources, typically JSON or XML&lt;/li&gt;
&lt;li&gt;Uses a standardized and uniform URL structure (e.g., DELETE /users/5).&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Representational state transfer (REST)&lt;/strong&gt; is a set of &lt;u&gt;architectural principles&lt;/u&gt; that define how to build application programming interfaces (APIs), which allow data to be communicated between web applications.&lt;/p&gt;

&lt;p&gt;When a request for data is sent to a REST API, it’s usually done through hypertext transfer protocol (commonly referred to as HTTP). &lt;br&gt;
Once a request is received, APIs designed for REST (called RESTful APIs or RESTful web services) can return messages in a variety of formats: HTML, XML, plain text, and JSON. &lt;br&gt;
In this way, RESTful APIs are more flexible and can be easier to set up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; JSON (JavaScript Object Notation) is favored as a message format because it can be read by any programming language, is human- and machine-readable, and is lightweight.&lt;/p&gt;

&lt;p&gt;REST supports all HTTP methods: GET, POST, PUT &amp;amp; DELETE.&lt;br&gt;
&lt;strong&gt;GET:&lt;/strong&gt; Retrieve particular resources by an ID&lt;br&gt;
&lt;strong&gt;POST:&lt;/strong&gt; Create a new resource.&lt;br&gt;
&lt;strong&gt;PUT:&lt;/strong&gt; Update a particular resource by an ID.&lt;br&gt;
&lt;strong&gt;DELETE:&lt;/strong&gt; Remove a particular resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note about PUT &amp;amp; PATCH:&lt;/strong&gt;&lt;br&gt;
PUT is used when you have all the updated data, while PATCH is suitable for partial updates.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>backend</category>
      <category>api</category>
    </item>
    <item>
      <title>Laravel Eloquent ORM (Object-Relational Mapper) Relationships</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Thu, 13 Aug 2026 10:21:05 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/laravel-eloquent-orm-object-relational-mapper-relationships-2aea</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/laravel-eloquent-orm-object-relational-mapper-relationships-2aea</guid>
      <description>&lt;p&gt;&lt;u&gt;One-to-One Relationship:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;users&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;phones&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
user_id - integer&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;User&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Phone&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;In the &lt;strong&gt;Phone&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;&lt;u&gt;One-to-Many Relationship:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;posts&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;comments&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
post_id - integer&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Post&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;In the &lt;strong&gt;Comment&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;&lt;u&gt;Many-to-many Relationship:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;users&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;roles&lt;/strong&gt;&lt;br&gt;
id - integer&lt;br&gt;
name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;role_user&lt;/strong&gt; (Pivot table)&lt;br&gt;
user_id - integer&lt;br&gt;
role_id - integer&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;User&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;roles&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'role_user'&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;p&gt;In &lt;strong&gt;Role&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'role_user'&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;p&gt;Important Note: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The pivot table is always singular; if you give it another name, make it plural.&lt;/li&gt;
&lt;li&gt;The name of the pivot table is ordered alphabetically.&lt;/li&gt;
&lt;li&gt;If the pivot table has more than two foreign keys, you use it as a normal table and create a model for it.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;u&gt;One-to-One (Polymorphic):&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;posts&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;users&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;images&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    url - string&lt;br&gt;
    imageable_id - integer&lt;br&gt;
    imageable_type - string&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Image&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;imageable&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphTo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphTo&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;p&gt;In the &lt;strong&gt;User&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphOne&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'imageable'&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;p&gt;In the &lt;strong&gt;Post&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphOne&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'imageable'&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;p&gt;&lt;u&gt;One-to-Many (Polymorphic):&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;posts&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    title - string&lt;br&gt;
    body - text&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;videos&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    title - string&lt;br&gt;
    url - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;comments&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    body - text&lt;br&gt;
    commentable_id - integer&lt;br&gt;
    commentable_type - string&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Comment&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;commentable&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphTo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphTo&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;p&gt;In the &lt;strong&gt;Video&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphOne&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'commentable'&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;p&gt;In the &lt;strong&gt;Post&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphOne&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'commentable'&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;p&gt;&lt;u&gt;Many-to-Many (Polymorphic):&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;posts&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;videos&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;tags&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;taggables&lt;/strong&gt;&lt;br&gt;
    tag_id - integer&lt;br&gt;
    taggable_id - integer&lt;br&gt;
    taggable_type - string&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Tag&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;videos&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphToMany&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphedByMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Video&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphToMany&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphedByMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&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;p&gt;In the &lt;strong&gt;Video&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphToMany&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&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;p&gt;In the &lt;strong&gt;Post&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;MorphToMany&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&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;p&gt;&lt;u&gt;Has One Through Relationship:&lt;/u&gt;&lt;br&gt;
The "has-one-through" relationship defines a one-to-one relationship with another model. &lt;br&gt;
&lt;strong&gt;mechanics&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;cars&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    model - string&lt;br&gt;
    mechanic_id - integer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;owners&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;br&gt;
    car_id - integer&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Mechanic&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;carOwner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;HasOneThrough&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasOneThrough&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Owner&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;&lt;u&gt;Has Many Through Relationship:&lt;/u&gt;&lt;br&gt;
The "has-many-through" relationship defines a many-to-many relationship with another model. &lt;br&gt;
&lt;strong&gt;projects&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;environments&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    project_id - integer&lt;br&gt;
    name - string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;deployments&lt;/strong&gt;&lt;br&gt;
    id - integer&lt;br&gt;
    environment_id - integer&lt;br&gt;
    commit_hash - string&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Project&lt;/strong&gt; 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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;deployments&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;HasManyThrough&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasManyThrough&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Deployment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Environment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;Choosing between Eloquent ORM and Query Builder:&lt;/p&gt;

&lt;p&gt;Use Eloquent when you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Want readable, expressive, object-oriented code.&lt;/li&gt;
&lt;li&gt;Are working with relationships (hasMany, belongsTo, etc.).&lt;/li&gt;
&lt;li&gt;Want to take advantage of features like accessors/mutators, events, casts, etc.&lt;/li&gt;
&lt;li&gt;Don’t need extreme query performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use Query Builder when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need complex joins, groupings, aggregates.&lt;/li&gt;
&lt;li&gt;You care more about raw performance.&lt;/li&gt;
&lt;li&gt;You don’t need to manipulate model objects (e.g., User model).&lt;/li&gt;
&lt;li&gt;You’re building reports, analytics, or API endpoints returning raw JSON.&lt;/li&gt;
&lt;li&gt;You still get Laravel’s database abstraction and security (no SQL injection risk).&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>OOP Object-Oriented Programming</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Tue, 11 Aug 2026 18:35:31 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/oop-object-oriented-programming-42mg</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/oop-object-oriented-programming-42mg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Advantages of using OOP:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is faster and easier to execute.&lt;/li&gt;
&lt;li&gt;Provides a clear structure for the programs.&lt;/li&gt;
&lt;li&gt;Helps to keep code DRY "Don't Repeat Yourself" and makes code easier to maintain, modify, and debug.&lt;/li&gt;
&lt;li&gt;Makes it possible to create fully reusable applications with less code and shorter development time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Define a Class:&lt;/strong&gt;&lt;br&gt;
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces &lt;code&gt;{}&lt;/code&gt;. All its properties and methods go inside the braces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delegation:&lt;/strong&gt;&lt;br&gt;
Delegation means that you use an object of another class as an instance variable.&lt;br&gt;
We can create multiple objects from a class. Each object has all the variables and functions defined in the class. An object of a class is made using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;$this&lt;/code&gt; keyword refers to the current class and is only available inside methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;__construct()&lt;/code&gt; function:&lt;/strong&gt;&lt;br&gt;
Automatically runs at the beginning of the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;__destruct()&lt;/code&gt; function:&lt;/strong&gt;&lt;br&gt;
Automatically runs at the end of the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encapsulation:&lt;/strong&gt;&lt;br&gt;
The wrapping up of data and methods is a protection mechanism for the variables and functions inside the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access Modifier:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Public:&lt;/strong&gt; Variables or functions can be accessed from everywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Private:&lt;/strong&gt; Variables or functions can ONLY be accessed inside the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Protected:&lt;/strong&gt; Variables or functions can be accessed inside the class and by child classes that extend from the parent class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constants:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can’t be changed once it is declared.&lt;/li&gt;
&lt;li&gt;Declared inside a class with the &lt;code&gt;const&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;It is recommended to name the constants in all &lt;u&gt;uppercase letters&lt;/u&gt;.&lt;/li&gt;
&lt;li&gt;Access outside the class by using the class name followed by the scope resolution operator &lt;code&gt;::&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Access a constant inside the class by using the &lt;code&gt;self&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Static Functions and Variables:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static functions or variables can be called directly - without creating an instance of the class first.&lt;/li&gt;
&lt;li&gt;Static functions or variables are declared with the &lt;code&gt;static&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;To access a static function or variable, use the &lt;u&gt;class name&lt;/u&gt;, double colon &lt;code&gt;::&lt;/code&gt;, and the &lt;u&gt;function name&lt;/u&gt;.&lt;/li&gt;
&lt;li&gt;A static function can be accessed from a function in the same class using the &lt;code&gt;self&lt;/code&gt; keyword and double colon &lt;code&gt;::&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To call a static method from a child class, use the &lt;code&gt;parent&lt;/code&gt; keyword inside the child class.&lt;/li&gt;
&lt;li&gt;The static method can be public or protected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;final&lt;/code&gt; keyword can be used to prevent class inheritance or to prevent method overriding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Polymorphism:&lt;/strong&gt;&lt;br&gt;
is one of the OOP features that allows us to perform a single action in different ways. &lt;br&gt;
Like declaring a function in the parent class and giving the function a different implementation in the children's classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can have both abstract methods (without body) and regular methods (with implementation).&lt;/li&gt;
&lt;li&gt;Can have properties (variables).&lt;/li&gt;
&lt;li&gt;A class can extended single abstract (single inheritance).&lt;/li&gt;
&lt;li&gt;Cannot be instantiated directly — only extended (inherited) by other classes.&lt;/li&gt;
&lt;li&gt;Used when classes share some common behavior, but also need to implement some parts individually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interface:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can only contain method declarations (no implementation).&lt;/li&gt;
&lt;li&gt;No properties — only method signatures.&lt;/li&gt;
&lt;li&gt;A class can implement multiple interfaces (multiple inheritance).&lt;/li&gt;
&lt;li&gt;Forces the class to implement all methods defined in the interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Traits&lt;/strong&gt;&lt;br&gt;
PHP only supports single inheritance: a child class can inherit from only one parent.&lt;br&gt;
So, what if a class needs to inherit multiple behaviors? &lt;br&gt;
OOP traits solve this problem.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traits are used to declare functions that can be used in multiple classes. Traits can have functions and abstract functions that can be used in multiple classes, and the functions can have any access modifier (public, private, or protected).&lt;/li&gt;
&lt;li&gt;Traits are declared with the trait keyword. &lt;/li&gt;
&lt;li&gt;To use a trait in a class, use the use keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Difference between Traits and Interfaces in PHP&lt;/strong&gt;&lt;br&gt;
many classes implement the same interface but have different behavior, while traits are just chunks of code injected into a class in PHP.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>oop</category>
    </item>
    <item>
      <title>SOLID Principles</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Wed, 29 Jul 2026 18:29:36 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/solid-principles-86c</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/solid-principles-86c</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;S for &lt;strong&gt;Single Responsibility&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Mistake:&lt;/strong&gt; &lt;br&gt;
Fat Controllers and Models&lt;br&gt;
Developers often cram too much logic into controllers and models, making them hard to maintain and test.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
Suggests that a class should have only one reason to change, meaning each class should only have one responsibility.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O for &lt;strong&gt;Open–Closed&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Mistake:&lt;/strong&gt; &lt;br&gt;
Modifying existing code to add new functionality&lt;br&gt;
Developers sometimes directly modify existing classes to add new features, which can introduce bugs and make the code harder to maintain.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
States that software entities should be open for extension but closed for modification.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;L for &lt;strong&gt;Liskov Substitution&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Mistake:&lt;/strong&gt; &lt;br&gt;
Subclasses that do not adhere to the contract of their base classes&lt;br&gt;
Developers may create subclasses that override base-class methods in ways that change their expected behavior, leading to bugs.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
States that subclasses should be substitutable for their base classes without altering the correctness of the program. Ensure that subclasses override methods in a manner consistent with the base class.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Violation of LS&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Fly logic&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Penguins can't fly"&lt;/span&gt;&lt;span class="p"&gt;);&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;p&gt;&lt;strong&gt;&lt;u&gt;Adhering to LS&lt;/u&gt;&lt;/strong&gt;&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Fly logic&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Swim logic&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;ol&gt;
&lt;li&gt;&lt;p&gt;I for &lt;strong&gt;Interface Segregation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Mistake:&lt;/strong&gt; &lt;br&gt;
Implementing large interfaces&lt;br&gt;
Developers sometimes create large interfaces with methods that implementing classes don't need, leading to unused code.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
Suggests that clients should not be forced to depend on interfaces they do not use. Create smaller, more specific interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;D for &lt;strong&gt;Dependency Inversion&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Mistake:&lt;/strong&gt; &lt;br&gt;
High-level modules depending on low-level modules&lt;br&gt;
Developers often make high-level classes depend directly on low-level classes, which makes the code rigid and hard to change.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
States that high-level modules should not depend on low-level modules, but both should depend on abstractions. Use dependency injection to adhere to DIP.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>development</category>
      <category>design</category>
      <category>software</category>
    </item>
    <item>
      <title>Service Container</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Tue, 28 Jul 2026 12:02:32 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/service-container-4l66</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/service-container-4l66</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Service Container&lt;/strong&gt; is one of Laravel's core features. It is Laravel's implementation of the &lt;strong&gt;Dependency Injection (DI)&lt;/strong&gt; and &lt;strong&gt;Inversion of Control (IoC)&lt;/strong&gt; patterns.&lt;br&gt;
It's also Laravel's way of managing dependencies and injecting them automatically.&lt;br&gt;
Instead of creating dependencies manually with the &lt;strong&gt;new&lt;/strong&gt; keyword, you let the container resolve and inject them for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Laravel Uses the Service Container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most Service Container bindings are registered inside Service Providers using the &lt;code&gt;register()&lt;/code&gt; method.&lt;br&gt;
Within a service provider, you have access to the container through the &lt;code&gt;$this-&amp;gt;app&lt;/code&gt; property.&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppServiceProvider&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ServiceProvider&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//&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;ul&gt;
&lt;li&gt;Use the &lt;strong&gt;bind()&lt;/strong&gt; method to tell Laravel how to create an object.
Every time the dependency is resolved, Laravel creates a new instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Exaple&lt;/strong&gt;&lt;br&gt;
Whenever someone requests LoggerInterface, Laravel should create and return a new FileLogger instance.&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="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggerInterface&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileLogger&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;ul&gt;
&lt;li&gt;Use &lt;strong&gt;singleton()&lt;/strong&gt; when you want Laravel to create only one instance and reuse it throughout the application's lifetime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Exaple&lt;/strong&gt;&lt;br&gt;
The first time CacheService is requested, Laravel creates the object.&lt;br&gt;
Every subsequent request returns the same instance instead of creating a new one.&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="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;singleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CacheService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CacheService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;$one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CacheService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CacheService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$one&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$two&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;bind() vs singleton()&lt;/strong&gt;&lt;br&gt;
bind(): Creates a new instance every time the dependency is resolved.&lt;br&gt;
singleton(): Creates one instance and reuses it for all future resolutions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You will typically be interacting with the container within service providers; however, if you would like to interact with the container outside of a service provider, you may do so via the &lt;strong&gt;App facade&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Exaple&lt;/strong&gt;&lt;br&gt;
If your class has no constructor parameters (or only concrete classes, not interfaces), Laravel will automatically create it — you don’t have to register it.&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'Hello!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HelloService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Compiled and Interpreted Languages</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Wed, 22 Jul 2026 11:23:54 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/compiled-and-interpreted-languages-4100</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/compiled-and-interpreted-languages-4100</guid>
      <description>&lt;p&gt;The key difference lies in how the code is executed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compiled Languages:&lt;/strong&gt;&lt;br&gt;
The source code is translated into machine code by a compiler.&lt;u&gt;it's a sequence of instructions the computer's CPU can directly understand&lt;/u&gt;. This process happens before the program is executed. The resulting machine code is then executed directly by the CPU.&lt;br&gt;
&lt;strong&gt;Translation:&lt;/strong&gt; Translated into machine code before execution&lt;br&gt;
&lt;strong&gt;Execution:&lt;/strong&gt; Generally faster&lt;br&gt;
&lt;strong&gt;Debugging:&lt;/strong&gt; Can be more difficult as errors may not be immediately apparent.&lt;br&gt;
&lt;strong&gt;Examples:&lt;/strong&gt; C, C++, Java, Go&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interpreted Languages:&lt;/strong&gt;&lt;br&gt;
The source code is executed line by line by an interpreter. The interpreter reads each line, translates it into machine code, and then executes it immediately.&lt;br&gt;
&lt;strong&gt;Translation:&lt;/strong&gt; Translated and executed line by line&lt;br&gt;
&lt;strong&gt;Execution:&lt;/strong&gt; Generally slower&lt;br&gt;
&lt;strong&gt;Examples:&lt;/strong&gt; Python, JavaScript, PHP, Ruby&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP:&lt;/strong&gt; PHP is primarily an interpreted language.&lt;br&gt;
However, some PHP engines (like the popular &lt;strong&gt;PHP-FPM&lt;/strong&gt;) use techniques like &lt;strong&gt;"just-in-time" (JIT) compilation&lt;/strong&gt; to improve performance. JIT compilers translate parts of the PHP code into machine code during execution, which can significantly speed up performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In essence:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compiled languages prioritize performance and efficiency, while interpreted languages prioritize ease of development and portability.&lt;/li&gt;
&lt;li&gt;The choice between a compiled and interpreted language depends on the specific needs of the project.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>software</category>
      <category>webdev</category>
      <category>programming</category>
      <category>php</category>
    </item>
    <item>
      <title>Stateful vs Stateless Authentication</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Mon, 20 Jul 2026 12:38:38 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/stateful-vs-stateless-authentication-bhe</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/stateful-vs-stateless-authentication-bhe</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stateful Authentication&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Session-based:&lt;/strong&gt; Relies on server-side sessions to maintain user state.&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; When a user logs in, a session is created on the server, and a session ID is stored in a cookie on the client. Subsequent requests include the session ID, allowing the server to identify the user.&lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; Simple to implement, good for traditional web applications.&lt;br&gt;
&lt;strong&gt;Disadvantages:&lt;/strong&gt; Scalability issues with large user bases, vulnerability to session hijacking, and state management complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stateless Authentication&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Token-based:&lt;/strong&gt; Relies on tokens (usually JWTs) instead of sessions.&lt;br&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; After successful authentication, a JWT containing user information is issued. Subsequent requests include the token in the authorization header. The server validates the token without maintaining user state.&lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; Scalability, security, and flexibility.&lt;br&gt;
&lt;strong&gt;Disadvantages:&lt;/strong&gt; Increased complexity in token management and potential for larger payloads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use Which&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Stateful Authentication:&lt;/strong&gt; Suitable for traditional web applications where user experience is prioritized and scalability is not a major concern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stateless Authentication:&lt;/strong&gt; Ideal for APIs, mobile applications, and single-page applications (SPAs) where scalability, security, and statelessness are essential.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>security</category>
    </item>
    <item>
      <title>Laravel Request Lifecycle</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Sun, 19 Jul 2026 16:59:03 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/laravel-request-lifecycle-34cf</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/laravel-request-lifecycle-34cf</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Request Entry Point&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request begins at public/index.php. This file is the entry point for all incoming requests by your web server (Apache / Nginx) configuration.
&lt;/li&gt;
&lt;li&gt;It loads the Composer autoloader and instantiates the Laravel application from bootstrap/app.php.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Kernel Initialization&lt;/strong&gt;&lt;br&gt;
This kernel is responsible for handling HTTP requests and managing middleware. The request is sent to either the HTTP kernel or the console kernel, depending on the type of request. Kernels serve as the central location where all requests flow. Just focus on the HTTP kernel, which is located in app/Http/Kernel.php.&lt;/p&gt;

&lt;p&gt;The Illuminate\Foundation\Http\Kernel class is instantiated, which defines &lt;u&gt;"an array of bootstrappers"&lt;/u&gt;, which refers to a list of classes that will be run before the request is executed. These bootstrappers configure &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;li&gt;Authentication &amp;amp; Authorization&lt;/li&gt;
&lt;li&gt;Detect the application environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also defines a list of HTTP middleware that all requests must pass through before being handled by the application. This middleware handles &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading and writing the HTTP session&lt;/li&gt;
&lt;li&gt;Verifying the cross-site request forgery (CSRF) token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Call the handle method in all middlewares, then after the response, call the terminate method, which runs after the request &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Service Providers&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One of the most important &lt;u&gt;kernel bootstrapping actions is loading&lt;/u&gt; the service providers for your application.&lt;/li&gt;
&lt;li&gt;Service providers are responsible for bootstrapping all of the framework's various components, such as &lt;u&gt;the database, queue, validation, and routing&lt;/u&gt; components.&lt;/li&gt;
&lt;li&gt;Service Providers are listed in config/app.php. This is the master list or array of all the service providers Laravel will load for your application.&lt;/li&gt;
&lt;li&gt;Laravel loops through them. When Laravel starts up (bootstraps), it goes through each provider in that list and creates (instantiates) an object of each service provider class.&lt;/li&gt;
&lt;li&gt;register() is called first on all providers. Purpose of register(): You tell Laravel’s service container how to build certain classes, bind interfaces, or configure services.&lt;/li&gt;
&lt;li&gt;After all providers are registered, Laravel then calls the boot() method on each one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-Why does Laravel call the &lt;strong&gt;boot()&lt;/strong&gt; method after &lt;strong&gt;register()&lt;/strong&gt; in a service provider?&lt;br&gt;
Because by the time boot() runs, all other service providers have been registered, you can safely use the services that those providers have registered.&lt;/p&gt;

&lt;p&gt;-Why are Service Providers so important?&lt;br&gt;
Essentially, every major feature offered by Laravel is bootstrapped and configured by a service provider. Everything in Laravel — routing, queues, events, mail, validation, etc. — is set up by these providers. When you install a package (like Cashier, Horizon, etc.), you often add its service provider to config/app.php so Laravel knows how to register and boot it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Route Matching&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One of the most important service providers in your application is the App\Providers\RouteServiceProvider. This service provider loads the route files contained within your application's routes directory.&lt;/li&gt;
&lt;li&gt;Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router, which will dispatch the request to a route or controller.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Controller Invocation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once a matching route is found, the associated controller method is executed. &lt;/li&gt;
&lt;li&gt;The controller handles the request logic, interacts with models, and prepares the response. &lt;/li&gt;
&lt;li&gt;The controller returns a response, which can be a view, JSON, or other formats.
&lt;/li&gt;
&lt;li&gt;Laravel converts the response into an HTTP response object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. Middleware Termination&lt;/strong&gt;&lt;br&gt;
The request passes through the middleware again, allowing for any final actions (e.g., logging, error handling).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Response Sending&lt;/strong&gt;&lt;br&gt;
The HTTP kernel's handle method returns the response object, and the index.php file calls the send method on the returned response. The send method sends the response content to the user's web browser.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Autoloader</title>
      <dc:creator>Mohamed Fakhr</dc:creator>
      <pubDate>Sat, 18 Jul 2026 19:00:27 +0000</pubDate>
      <link>https://dev.to/mohamed_fakhr_eldin/laravel-autoloader-27fj</link>
      <guid>https://dev.to/mohamed_fakhr_eldin/laravel-autoloader-27fj</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is an Autoloader?&lt;/strong&gt;&lt;br&gt;
An autoloader is a piece of code that automatically loads classes when they are needed, without requiring explicit include or require statements. This saves developers from manually managing file inclusion and improves code organization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Composer Installation:&lt;/strong&gt; When you create a new Laravel project, Composer installs the required packages and generates the autoloader file. &lt;u&gt;Composer also is a package management system&lt;/u&gt; as npm for nodejs.&lt;br&gt;
&lt;strong&gt;Autoloader Activation:&lt;/strong&gt; Laravel's autoloader is triggered. It examines the class name and tries to locate the corresponding file. If the file is found, it's included, making the class available for use.&lt;/p&gt;

&lt;p&gt;The Concept Behind Laravel's Autoloader&lt;br&gt;
&lt;strong&gt;(PSR-4) Autoloading Standard:&lt;/strong&gt; PHP Standard Recommendation for autoloading. &lt;u&gt;This standard defines a mapping between class names and file paths.&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;Namespace Mapping:&lt;/strong&gt; Laravel uses namespaces to organize classes. The autoloader maps namespaces to specific directories within your project structure.&lt;br&gt;
&lt;strong&gt;Classmap Generation:&lt;/strong&gt; Composer creates a classmap, which is a lookup table that maps class names to their file paths, optimizing performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is composer.lock?&lt;/strong&gt;&lt;br&gt;
The composer.lock file is auto-generated by Composer.&lt;br&gt;
Purpose:&lt;br&gt;
It locks exact versions of every package (and their dependencies).&lt;br&gt;
Ensures the project runs the same way for every developer or deployment.&lt;br&gt;
Keeps installs consistent across environments.&lt;/p&gt;

&lt;p&gt;That's why some developers don't commit and push composer.lock file so every package is installed with dependence on every developer environment. &lt;/p&gt;

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