Skip to content

Instantly share code, notes, and snippets.

@marufsharia
Forked from abelcallejo/README.md
Created January 7, 2024 08:30
Show Gist options
  • Select an option

  • Save marufsharia/fd6f032aa45e0e523caddc722034fafb to your computer and use it in GitHub Desktop.

Select an option

Save marufsharia/fd6f032aa45e0e523caddc722034fafb to your computer and use it in GitHub Desktop.
Laravel Cheatsheet

Laravel Cheatsheet

Installation

# Prepare the working directory
cd /path/to/www
git clone git@github.com:goodyweb/example-app.git
cd /path/to/www/example-app

# Add the Laravel installer globally all throughout composer
composer global require laravel/installer

# Create new app
laravel new laravel-app

# Get inside the working directory and move all files to the 
cd laravel-app
mv -f ./* ../
mv -f ./.* ../
cd ../
rmdir laravel-app

# Configure the laravel installation
nano .env

# 
php artisan migrate

Breeze

composer require laravel/breeze --dev
php artisan breeze:install

php artisan migrate
npm install
npm run dev

Livewire

composer require livewire/livewire
php artisan livewire:publish --config

resources/views/layouts/app.blade.php

<html>
<head>
    ...
    @livewireStyles
</head>
<body>
    ...
    @livewireScripts
</body>
</html>

Tailwind

# Tailwind
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

tailwind.config.js

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./resources/**/*.blade.php",
    "./resources/**/*.js",
    "./resources/**/*.vue",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

resources/css/app.css

@tailwind base;
@tailwind components;
@tailwind utilities;
npm run dev

Creating a table

php artisan make:migration create_users_table --create=users

Altering a table

php artisan make:migration add_role_to_users_table --table=users

Deleting a column

$table->dropColumn('status');

Deleting columns

$table->dropColumn(['status', 'status_at']);

Data types

Reference

Creating a model

php artisan make:model User

Creating a controller

php artisan make:controller UserController

Getting the current user

use Illuminate\Support\Facades\Auth;

Auth::user()

Creating livewire component

php artisan make:livewire Users

Routing livewire components

use App\Http\Livewire\{Users};

Route::get('/users', Users::class);

Making a command

php artisan make:command SendEmails

Migrating a single migration file forcily

php artisan migrate:refresh --path=database/migrations/timestamped_migration_file.php

Converting strings to slugs

use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5 Framework', '-');

Database

use Illuminate\Support\Facades\DB;

Eloquent

Insert

Article::create(['title' => 'Traveling to Asia']);

Migration file guides

Unique column

$table->string('column_name_here')->unique();

Foreign key

$table->foreign('user_id')->references('id')->on('users');

Calling routes

route('checkout')

or

route('checkout',['uuid'=>$uuid])

Making a seeder

php artisan make:seeder UserSeeder

Hooking a seeder to the main seeder

$this->call([
    UserSeeder::class,
]);

Request parameters

GET or POST parameters that are retrievable.

$_POST['name'] is retrievable as:

request()->name

Also, $_GET['name'] is retrievable similarly as:

request()->name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment