Skip to content

Instantly share code, notes, and snippets.

View jasonlbeggs's full-sized avatar

Jason Beggs jasonlbeggs

View GitHub Profile

Laravel Jetstream Banners

If you're using Laravel Jetstream with Livewire or Intertia.js, the scaffolding includes a banner notification component in the app layout that you can utilize from your own views/Livewire/Inertia.js components. Notifications can be dispatched using Laravel's session.

<?php

// ...

class ExampleComponent extends Component

Testing JSON Columns

You may know that you can query against JSON columns using the column->key syntax in Laravel, but I recently learned you can use the same syntax in your tests when asserting that the database has certain data!

Query Example

FormElement::where('rules->required', null)->get();

Pass Alpine.js Bound Attributes To Blade Component

If you need to pass a bound Alpine.js attribute to a Laravel Blade component, you can prefix the attribute name with two colons instead of a single colon to tell Blade to not evaluate the attribute as a PHP expression.

<div x-data="{ isActive: true }">
    <x-some-blade-component ::is-active="isActive" />
</div>

pluck() Collection Method $key Parameter

Recently, I learned (or re-learned) that the pluck() method on a Laravel Collection accepts a second parameter to specify how you'd like the Collection keyed. Super handy in certain cases!

<?php

$posts = collect([
    ['slug' => 'post-1', 'title' => 'Taylor is cool'],
 ['slug' =&gt; 'post-2', 'title' =&gt; 'Tyler is the real MVP'],

Notification when() method

When sending notifications with Laravel, there's a when() method on each message type that can be used to optionally add lines or actions when some value is true. Very similar concept the the when method on the QueryBuilder and Collections.

Query Builder Example

Team::when(request()->has('filters.name'), function ($query) {
    $q->where('name', request('filters.name'));
})-&gt;get();

Blade Scoped Slots

Blade Components have the concept of "Scoped Slots" similar to Vue.js. This allows you to access public properties and methods on the component inside slots. Super handy in certain cases.

<x-alert>
    <x-slot name="title">
        {{ $component->formatAlert('Server Error') }}
    </x-slot>

Laravel booted() Model Method

Laravel's new(ish) booted() model method and PHP arrow functions can (subjectively) really cleaned up these little model event listeners!

<?php

// ...

class Team extends Model

Livewire Test Debugging Tips

If you're testing a Livewire component and need to debug validation errors, the last response, the last rendered view, etc., Livewire offers these public properties on the tested component. I find dumping the lastErrorBag handy pretty often!

/** @test */
public method something_works()
{
    $component = Livewire::test(SomeComponent::class)->call('someMethod');
 

Livewire Reference Page

Did you know the Livewire docs include a "Reference" page? It makes it super easy to quickly read over the entire Livewire API on a single page. Sorta like a Livewire knowledge dump.

{Maybe include a screenshot of the page?}

Livewire make, move, copy, and delete commands

In addition to the livewire:make command, Livewire includes move, copy, and delete commands to make managing components really easy! These will move, copy, or delete the component class and Blade files all at once.

# Move the Foo component to Bar/Baz
php artisan livewire:move foo bar.baz

# Copy the Foo component to a new component named Bar
php artisan livewire:move foo bar