Laravel - A Straight forward cheatsheet
Laravel Cheatsheet
Before anyone else says that a lot of things here are the exact same as of Laravel Official Docs, This blog/snippet/cheatsheet is just a simple straight forward extraction of a lot of things which are placed there.
Set Up
- Install xampp
- Add PHP to path
- Download composer.prar
- Add composer to path using option on website
- Create a laravel project
Create a project
composer create-project laravel/laravel laravel_api_basicsCheck out apis.php to add Routes and expirement with it.
Routing
Route::get('/profile', function(Request $request){
return "Hello";
});
## Supported Route methods
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);To respont to multiple HTTP methods.
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});Redirecting
Route::redirect('/here', '/there');By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter.
Route::redirect('/here', '/there', 301);Or, you may use the Route::permanentRedirect method to return a 301 status code:
Route::permanentRedirect('/here', '/there');To prefix to a route
// This will match all /admin routes
Route::prefix('admin')->group(function () {
Route::get('/users', function () {
// Matches The "/admin/users" URL
});
});Query Parameters
Adding a Controller
# --api adds methods for crud, not code only method
# remove --api if you want to create your own methods
php artisan make:controller <controllername> --api
# example : php artisan make:controller ProductController --apiUsing a Controller
Import a
use App\Http\Controllers\ProductController;Map a route to a function in controller
Route::get('/shorten', [ProductController::class, 'index']);Middlewares
Create a middleware
php artisan make:middleware LogIpToDatabaseA middleware is created in app/Http/Middleware
Follow this https://laravel.com/docs/8.x/middleware to register a middleaware globally or route specific.
To call a middleware from a Controller, Use a contructor : https://stackoverflow.com/questions/43804752/laravel-adding-middleware-inside-a-controller-function.
There are 3 ways to use a middleware inside a controller.
1) Protect all functions in a Controller
public function __construct()
{
$this->middleware('auth');
}2) Protect only some functions
public function __construct()
{
$this->middleware('auth')->only(['functionName1', 'functionName2']);
}3) Protect all functions except some
public function __construct()
{
$this->middleware('auth')->except(['functionName1', 'functionName2']);
}Here you can find all the documentation about this topic:Â Controller Middleware.
Models
Create a Model
php artisan make:model ProductModel --migration
// --migration to create a migration fileEdit table configuration in database>migrations>__created_migration.
Schema Docs : https://laravel.com/docs/4.2/schemahttps://laravel.com/docs/8.x/migrations#available-column-types
To Migrate
php artisan migrateAdd fillable in Model to specify what can be accept to store data in the database.
// example
protected $fillable = [
'image',
'title',
'description',
];Passing Body data to get values
$request->all();