Laravel Redirect Index to Public

Edit your htaccess file.
This alone will redirect your root o the public folder.
If you have a lot inside of your htaccess file. Be careful while adding stuff. Ensure you keep a backup before editing and test the addition of the code inside the htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Laravel Class Construct

class MyCustomClassName
{

    public function __construct()
    {
    }

    public function whatever_whatever(){
      // Code goes here. And can use anything from the construct. The construct will always load first
    }

}

Why we have classes and controllers in MVC?

A Controller or Class in MVC are both just classes.
So... Technically speaking.
It is possible to write all your functionality inside your controllers and completely ditch the classes folder.
But! The purpose of breaking out into separate classes is re-usability.

Controllers :

<?php

namespace App\Http\Controllers;

class MyCustomController extends Controller
{

}

Classes :

<?php

namespace App\Classes\MyCustomClass;

class ClassNameWhateverWhatever
{

}

Why Classes, what is the point?
-----------------------------------------------------------------
Classes serve as containers for specific purposes.
For example.

You create a class for :
Car
--
And inside your class you write all your functionality.
A car can have specific functionality.
--
moveForward()
reverse()
brake()
switchOnLights()

-----------------------------------------------------------------
Then, you reference your car class inside your contollers.
--
So something like
car->moveForward();

This design strategy makes your code extremely easy to re-use without having to re-write additional functionality. This is why we seperate everything into a class and then we create another class called a controller class and reference our classes inside the controller classes.

Laravel Save Function, update and add with exceptions

Save Function (Update and Add in 1 function based on ID)

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class RandomController extends WhateverWhateverController {

    public function saveTermsAndConditions(Request $request){
        if (isset($request->id)) {
            // Update
            try {
                DB::table('table_name')->where('id',$request->id)->update(
                    array(
                        'field'=>$request->field,
                        'content'=>$request->content,
                        'updated_at' => date("Y-m-d H:i:s", strtotime('now')),
                    )
                );
                $termsAndConditions = DB::table('table_name')->where('id', $request->id)->first();
            } catch (\Exception $e) {
                return response()->json($e);
            }
        } else {
            // Add
            try {
                $id = DB::table('table_name')->insertGetId(
                    array(
                        'field' => $request->field, 
                        'content' => $request->content, 
                        'created_at' => date("Y-m-d H:i:s", strtotime('now')),
                        'updated_at' => date("Y-m-d H:i:s", strtotime('now'))
                    )
                );
                $termsAndConditions = DB::table('table_name')->where('id', $id)->first();
            } catch (\Exception $e) {
                return response()->json($e);
            }
        }

        return $termsAndConditions;
    }
}

Laravel Raw Query The DB with Prepared Statement

Required Laravel includes

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

Select Query

class MySpecialController extends Controller
{
    public function rawQueryFunctionNameSelect(Request $request){
        $rawQuery= DB::select(
            DB::raw(
                'SELECT * FROM table_name'
            )
        );
        return $rawQuery;
    }
}

Update Query

class MySpecialController extends Controller
{
    public function rawQueryFunctionNameUpdate(Request $request){
        DB::table('table_name')->where('id',$request->id)->update(
            array(
                'field'=>$request->field,
                'content'=>$request->content,
            )
        );
    }
}

Template Snips for Laravel .Blade files

<?php 

@php
  echo "<pre style='color:#fff;'>";
    print_r($data);
  echo "</pre style='color:#fff;'>";
@endphp

@php
  echo "<pre>";
    print_r($data);
  echo "</pre>";
@endphp

@php
  echo "<pre style='color:#fff;'>";
    print_r($data);
  echo "</pre style='color:#fff;'>";
@endphp

@php
  echo "<pre>";
    print_r($data);
  echo "</pre>";
@endphp

New Dev Setup PHP & Ubuntu – For Windows

Useful for Debugging :
https://old.garthbaker.co.za/re-install-ubuntu-windows-11/

Install PHP 8 (Ubuntu Terminal cd ~)

sudo apt update
sudo apt -y upgrade
--
sudo apt update
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
--
sudo apt update
sudo apt install php8.0
--
php -v

Installing Required Extensions (Ubuntu Terminal cd ~)

The below command should be enough. But if you missing extensions, check below

sudo apt install php8.0-curl php8.0-cli php8.0-xml php8.0-mbstring php8.0-zip php8.0-pdo php8.0-mysql php8.0-gd

If you want to list all PHP 8.0 loaded modules use the command:
php -m

sudo apt install php8.0-

php8.0-amqp       php8.0-common     php8.0-gd         php8.0-ldap       php8.0-odbc       php8.0-readline   php8.0-sqlite3    php8.0-xsl
php8.0-apcu       php8.0-curl       php8.0-gmp        php8.0-mailparse  php8.0-opcache    php8.0-redis      php8.0-sybase     php8.0-yac
php8.0-ast        php8.0-dba        php8.0-igbinary   php8.0-mbstring   php8.0-pgsql      php8.0-rrd        php8.0-tidy       php8.0-yaml
php8.0-bcmath     php8.0-dev        php8.0-imagick    php8.0-memcached  php8.0-phpdbg     php8.0-smbclient  php8.0-uuid       php8.0-zip
php8.0-bz2        php8.0-ds         php8.0-imap       php8.0-msgpack    php8.0-pspell     php8.0-snmp       php8.0-xdebug     php8.0-zmq
php8.0-cgi        php8.0-enchant    php8.0-interbase  php8.0-mysql      php8.0-psr        php8.0-soap       php8.0-xhprof
php8.0-cli        php8.0-fpm        php8.0-intl       php8.0-oauth      php8.0-raphf      php8.0-solr       php8.0-xml

Install Composer (Ubuntu Terminal cd ~)

//Update your packages:
sudo apt-get update

//Install Curl to get the composer library:
sudo apt-get install curl

//Installing composer:
sudo curl -s https://getcomposer.org/installer | php

//Move the composer file to bin path :
sudo mv composer.phar /usr/local/bin/composer

//Verify composer installation :
composer

Install NPM (Ubuntu Terminal cd ~)

sudo apt install npm

Activate WSL 2 and Virtual Machine Platform
(Run as Admin in powershell ~)

NB!!! Don't forget to install the kernel for wsl2 :
https://docs.microsoft.com/en-za/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package

//Enable the WSL2 without reboot 
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart 

//Enable the Virtual Machine Platform 
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Set WSL2 Default (Run as Admin in powershell ~)

wsl --set-default-version 2

WSL2 Connection Issues, Timeouts and Unable to Open Stream and Network Unreachable

1: Try running your composer commands with sail if you using laravel and docker.
example : sail composer require laracasts/generators --dev

2: Second try running

wsl --shutdown
wsl ping google.com -c 4

3: Third try the below power shell command

# Check these threads before proceeding:
# https://github.com/microsoft/WSL/discussions/5857
# https://github.com/microsoft/WSL/issues/5821
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
  $CmdLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
  Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CmdLine
  Exit
}
# Restart the Host Network Service 
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"

Article 1

https://gist.github.com/danvy/9486bf730371436131cb888ff4c2ceb6

Article 2

https://github.com/microsoft/WSL/issues/5821