Categories
Laravel

Database Laravel Tests failing from Sail App

Ok, this was a silly error on my part, because I got out of practice using the sail commands.

I have a Laravel app running on Sail/Docker under WSL. The app worked through the browser, but there was no DB access from the CLI or running tests.

I was running php artisan test which was using the PHP local to the WSL system, and not the virtual PHP server which should run the app. The WSL PHP happened not to have the PDO driver installed, so DB access failed.

Just needed to run the command properly as sail art test to use the correct PHP (I have aliased art to artisan).

Problem solved.

Categories
Laravel

Laravel Validation – All Errors for One Field

Laravel’s message bag as the $errors->all() method to get all errors, across all fields.

Single error for a field in Blade helper:

@error('the_field') {{ $message }} @enderror

All errors for one field

$errors->get('the_field');
Categories
Laravel

Safe Mass Assignment in Laravel

Rather than explicitly assigning each allowed property to a Model (to avoid passing user input directly through) you can use the validated data to safely mass assign.

Re this tweet:

The $request->validate() method will return the keys of valid data (and redirect on failure).

<?php
$data = $request->validate([
...$rules])

$model->fill($data);

However if you accept input in the request that needs to be validated, but should not end up on the model this approach won’t work.

Categories
JavaScript Laravel

Encoding strings for Vue components

If you’re passing server side data from a Laravel Blade template into a Vue component prop, the standard {{ encoding }} structure won’t work. The HTML encoding is parsed by Vue and will break the template parsing.

<coffee-history-list
  :name='@json($cafe->name)'
>
</coffee-history-list>

Using Blade’s @json directive will escape the quotes correctly, but the prop value must be wrapped in single, not double quotes.

Categories
Laravel

Laravel 5.5 Carbon Wrapper

Categories
Laravel

Laravel New command not finding Composer

When calling ‘laravel new myapp’ this error is given:

sh: composer: command not found

Composer is aliased in ~/.bash_profile, which apparently doesn’t help the laravel installer find it (despite being in $PATH).

The solution was to copy /Users/me/composer.phar to /usr/local/bin/composer
The Laravel installer could then use Composer to setup the application.

Categories
Laravel

Laravel Sessions Gotcha

Lost a lot of time debugging missing values in the Session. They were set in a controller method which I then dd().

Killing the process seems to stop the value being saved in the session. It showed up in that method, but the request didn’t complete so the value was missing from subsequent requests.

Upshot: Let the request complete without dd() to save sesssion values.

 

Categories
JavaScript Laravel

Laravel Elixir Notes

gulp –production minifes and removes console.log which may not be what you want! Especially when debugging the process.

 

Categories
Laravel

Laravel Windows Devs Gotchas

Homestead is slow. Native OS is fast but watch out for these issues.

  • Probably no Redis. Tagged caching not possible with file system
  • Storage facade by default writes to storage/ and uses a symlink for public files from the /public dir. Windows doesn’t have symlinks. (Could probably fix with environment config files)
Categories
Laravel

Monitor Laravel SQL Queries

DB::listen(function($sql) {
    var_dump($sql->sql);
});