Laravel
Routing
Resources
Create index and store routes
resource('chirps', ChirpController::class)
Route::(['index', 'store', 'update', 'destroy'])
->only(['auth', 'verified']); ->middleware
These create routes for the components: ex. 'chirps.destroy'
Update example
The dependency injection parses the chirp from the resource route /chirps/{chirp}
and injects it
You don't have to call the method. Laravel's routing does that automatically when the parameters match
class ChirpController extends Controller
{public function update(Request $request, Chirp $chirp): RedirectResponse
{
}
}
('/chirps/{chirp}', [ChirpController::class, 'update'])->name('chirp.update'); Route::patch
Middleware
- Examples: Rate-Rate Limiting, logging, caching, CSRF, email-verified users
- Define middleware groups in app/Http/Kernel.php
Wrap routes in a middleware group:
('guest')->group(function () {
Route::middleware('register', [RegisteredUserController::class, 'create'])->name('register');
Route::get
('register', [RegisteredUserController::class, 'store']);
Route::post); }
Controllers
Naming conventions
index | GET | get the view at the route |
create | GET | display the form to create a new _ |
store | POST | save new resources to the db |
show | GET | show something (but not a full page, that's index) |
POST requests automatically include CSRF protection. So any route that modifies server state should be a POST request
Database Interactions
Use TablePlus for a database GUI
# php artisan tinker
(); App\Models\Chirp::all
Relationships, Models
Establishing relationships lets you do things like:
$request->user()->chirps()->create($validated);
// app/Models/User.php
class User extends Authenticatable
{// Enable mass assignment for this attribute
protected $fillable = [
'message',
];
public function chirps()
{return $this->hasMany(Chirp::class);
}
}
class Chirp extends Model
{// Access as a property, not a method. Ex. $chirp->user
public function user()
{return $this->belongsTo(User::class);
}
}
Migrations
<?php
return new class extends Migration
{public function up(): void
{('chirps', function (Blueprint $table) {
Schema::create$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('message');
$table->timestamps();
);
}
}; }
Factories
Used to seed your DB
php artisan tinker
> App\Models\Job::factory()->create();
# or
> App\Model\Job::factrory()->unverified()->create();
Eloquent
// lazy load - can cause the N+1 problem
$jobs = Jobs::all();
$jobs[0]->salary;
(['title' => 'Director', 'salary' => '$1,000,000']);
Job::create
(7)->delete(); // find id 7 then delete it
Job::find
();
Job::first
// ex. if job belongsTo employer
// latest is essentially an ORDER BY
('employer')->latest()->simplePaginate(3); Job::with
Artisan
php artisan make:model -mrc Chirp
php artisan migrate:fresh # reset the database
Policies
No policy class => everything is allowed
Policy class => Nothing is allowed except what's specified in the policies
Lets you define rules for controller commands
class PostPolicy {public function update(User $user, Post $post): bool {return $user->id === $post->user_id; } } // in the route ('users/create', function() { Route::getReturn Inertia::render('Users/Create', [ 'can' => Auth::user()->can('create', User::class); ]); )->middleware('can:create,App\Models\User'); }
Events
- Add listeners to things that happen
php artisan make:listener SendChirpCreatedNotifications --event=ChirpCreated
ext: https://bootcamp.laravel.com/inertia/notifications-and-events
Registering Global Components
// in app.js
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.commponent("Link", Link)
.mount(el)
, }
Default Layouts
createInertiaApp({
title: title => `${title} - ${appName}`,
resolve: name =>
let page = require(`.Pages/${name}`).default;
.layout ??= Layout;
page
) }
Frontend
<script setup>import { Head } from "@inertiajs/inertia-vue3"</script>
<template>
<title>My App</title>
</template>
Useful Functions
$job = Arr::first($jobs, fn($job) => $job['id'] == $id)
Request Validation
$request->validate([
'name' => ['required'],
'email' => ['required', 'email', 'unique:users,email'],
'password' => ['required', Password::min(6)],
])