Nono.MA

OCTOBER 10, 2024

The issue is that, by default, Laravel came with DB_HOST as 127.0.0.1 but MySQL 9 will reject that host in favor of localhost.

If everything else is configured correctly, simply set your DB_HOST to localhost, and you should be good to go.

NOVEMBER 18, 2022

Here's a way to encode a Laravel site request as JSON to log it via Laravel's logging mechanism, using the Log class from the illuminate/support package1.

// Log parameters in a get request
Route::get('a-view', function(Request $request) {
  \Log::info(json_encode(request()->server()));
  return view('your.view');
});

// Log parameters in a get request and redirect
Route::get('redirect', function(Request $request) {
  \Log::info(json_encode(request()->server()));
  return redirect('/some/page');
});

  1. The service provider of Laravel's Log class is Illuminate\Support\Facades\Log

AUGUST 26, 2022

I had installed an old version of Laravel Valet (2.18.10) on my system, and even when running composer global update, Valet wasn't being updated to the latest available version (3.1.9 at the time of this writing).

In theory, here's how you install and update laravel/valet.

# Install
composer global require laravel/valet

# Update
composer global update

But, as I mentioned above, my Valet installation was stuck at 2.18.10.

I forced composer to upgrade laravel/valet to the latest available version by specifying the ^3.0.0 constrain. (Note that this may lock your Valet version and not update in the future when ^4.0.0 is released.)

composer global require laravel/valet:^3.0.0
# Changed current directory to /Users/nono/.composer
# ./composer.json has been updated
# Running composer update laravel/valet
# Loading composer repositories with package information
# Updating dependencies
# Lock file operations: 0 installs, 1 update, 0 removals
#  - Upgrading laravel/valet (v2.18.10 => v3.1.9)
# Writing lock file
# Installing dependencies from lock file (including require-dev)
# Package operations: 0 installs, 1 update, 0 removals
#  - Upgrading laravel/valet (v2.18.10 => v3.1.9): Extracting archive
# Generating autoload files
# 15 packages you are using are looking for funding.
# Use the `composer fund` command to find out more!

Verify the installed version.

valet --version
# Laravel Valet 3.1.9

Inspecting Composer's global composer.json

You can get the path where your global composer.json is located, which defines your installed dependencies.

composer config data-dir
# /Users/nono/.composer

In that directory, you'll find composer.json.

cat $(composer config data-dir)/composer.json
# {
#     "require": {
#         "laravel/valet": "^3.0.0",
#         "laravel/installer": "^4.1"
#     }
# }

AUGUST 25, 2022

Here's how to locate the path of Composer's global composer.json, which defines the system packages installed and managed by Composer.

# Retrieve composer's data directory
composer config data-dir
# /Users/nono/.composer

# Print composer.json's contents
cat $(composer config data-dir)/composer.json
# {
#     "require": {
#         "laravel/valet": "^3.0.0",
#         "laravel/installer": "^4.1"
#     }
# }

AUGUST 24, 2022

Since Laravel Valet version 3, you can choose the specific PHP version a site should run on without the need to change Valet's global PHP version as in previous releases.

You can, for instance, run all your sites in PHP 8.1 but set specific legacy sites to run in PHP 7.4.

You'd set your global Valet PHP version.

valet use php@8.1

Then set the PHP version for a specific linked site.

# When the current folder matches the name of a linked site
valet isolate php@7.4

# Explicitly specifying a site name
valet isolate php@7.4 --site="my-app"

How to find a site's name

valet links
# +------+------------------+-------------------------+-------------+
# | Site | URL              | Path                    | PHP Version |
# +------+------------------+-------------------------+-------------+
# | gs   | http://gs.test   | /Users/.../gs-folio     | php@8.1     |
# | nono | http://nono.test | /Users/.../nonoma-folio | php@8.1     |
# +------+------------------+-------------------------+-------------+

The name of your site is listed in the Site column.

JULY 31, 2022

Here's an easy way to retrieve the PHP version your Laravel app is running.

>>> PHP_VERSION
=> "8.1.8"

JULY 30, 2022

Here's an easy way to retrieve the Laravel version your app is running.

>>> Illuminate\Foundation\Application::VERSION
=> "9.22.1"

JULY 22, 2022

First, you must install the sass NPM package as a development dependency.

npm install --save-dev sass

Then add your scss file to the vite.config.js file.

// vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/sass/app.scss',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});

You can then load the built css file in your Blade views.

// my-view.blade.php
@vite(['resources/sass/app.scss'])
// <link rel="stylesheet" href="http://nono.test/build/assets/app.0b1a4b87.css" />

JULY 21, 2022

Laravel recently switched from building assets with Laravel Mix to Vite.js. According to its website, "Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects."

It provides a dev server and a build command that bundles code using Rollup.

"Vite is opinionated and comes with sensible defaults out of the box, but is also highly extensible via its Plugin API and JavaScript API with full typing support."

Here are its main features.

  • 💡 Instant Server Start. On demand file serving over native ESM, no bundling required!
  • ⚡️ Lightning Fast HMR. Hot Module Replacement (HMR) that stays fast regardless of app size.
  • 🛠️ Rich Features. Out-of-the-box support for TypeScript, JSX, CSS and more.
  • 📦 Optimized Build. Pre-configured Rollup build with multi-page and library mode support.
  • 🔩 Universal Plugins. Rollup-superset plugin interface shared between dev and build.
  • 🔑 Fully Typed APIs. Flexible programmatic APIs with full TypeScript typing.

You can create a clean Laravel install that comes with Vite pre-configured.

# Create a Laravel app with the Laravel CLI tool
laravel new app

# Create a Laravel app with Composer
composer create-project laravel/laravel app

# Enter the directory
cd app

# Install NPM dependencies
npm install

# Run Vite's development server
npm run dev
#  VITE v3.0.2  ready in 249 ms
#
#  ➜  Local:   http://localhost:5173/
#  ➜  Network: use --host to expose
#
#  LARAVEL v9.21.3  plugin v0.5.0
#
#  ➜  APP_URL: http://app-laravel.test

# Build for production
npm run build
# > build
# > vite build
# 
# vite v3.0.2 building for production...
# ✓ 58 modules transformed.
# public/build/manifest.json            0.25 KiB
# public/build/assets/app.7c3c19f8.js   0.00 KiB / gzip: 0.02 KiB
# public/build/assets/app.334e7359.js   90.63 KiB / gzip: 33.07 KiB

JUNE 15, 2022

When trying to send an email using Amazon SES (Simple Email Service) using Laravel's Mail class.

\Mail::send('email', [], function ($message) {
    $message
    ->from('from@gmail.com', 'John Appleseed')
    ->to('to@gmail.com', 'Nono Martínez Alonso')
    ->subject('A sample email')
    ->html('<p>Hello, this is a test email.</p>');
});

I got the following error.

Expected response code "250" but got code "530",
with message "530 5.7.1 Authentication required".

Set up Amazon SES

You need to configure the following environment variables in .env.

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXX
AWS_DEFAULT_REGION=us-east-1

And then in config/services.php.

<?php

return [
    // ...
    'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    ],
];

Set up Laravel Mail

Lastly, set Amazon SES as Laravel's default mailer in .env.

MAIL_MAILER=ses

Note that you could also set the default in your config/mail.php in case there's no MAIL_MAILER key in your .env file.

<?php

return [
    // ...
    'default' => env('MAIL_MAILER', 'ses'),

Request to AWS SES API failed

This is the last error I got before everything worked.

Request to AWS SES API failed.
Email address is not verified.
The following identities failed the check in region US-EAST-1.

I was sending from an unverified email address. Changing it to one of my verified accounts fixed the issue.

DECEMBER 20, 2021

I came across this error when trying to pass a JSON object from Laravel to a Vue component.

[Vue warn]: Invalid prop: type check failed for prop "myProperty". Expected Array, got String with value...

You may have passed a stringified JSON object which Vue is not parsing as an Array.

In my case, the error was I wasn't binding a property but passing a raw String that was meant to be evaluated by Vue as a JavaScript expression.

I was defining properties in Laravel PHP.

$collection = Post::all();
$numbers = [1,2,3];

Then passing them with the Blade templating syntax to my Vue component.

<issue-cover
  v-bind:collection="{!! $collection->toJson() !!}"
  my-numbers="{!! $numbers !!}"
><\/issue-cover>

As you can see, my-numbers is not using the v-bind:property-name or :property-name (a short-hand to the former) syntax. That was all I had to do to solve this issue on my end.

<issue-cover
    v-bind:collection="{!! $collection->toJson() !!}"
    v-bind:my-numbers="{!! $numbers !!}"
><\/issue-cover>

JULY 8, 2021

This page is incomplete

An option is to set CACHE=array on your .env file.

I need to learn why this happens and what other alternatives there are to avoid this error.

This happened to me when installing laravel-geoip https://github.com/Torann/laravel-geoip/issues/123

It makes sense that you don't want to change your cache type if you're using file or database to array simply to use a function in this package.

Other users recommended to publish tarenn/geoip's config file and disable caching and tagging.

# Publish the configuration file
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
# Copied File [/vendor/torann/geoip/config/geoip.php] To [/config/geoip.php]
# Publishing complete.
    // ...
    'cache' => 'none', // defaults to 'all'
    // ...
    'cache_tags' => [], // defaults to // ['torann-geoip-location']
    // ...

Another option is to conditionally set the tag.

    // ...
    'cache_tags' => env('CACHE_DRIVER') == "array" ? ['torann-geoip-location'] : null,
    // ...

LAST UPDATED FEBRUARY 16, 2022

If you're receiving this error when trying to composer install.

Your GitHub OAuth token for github.com contains invalid characters

Updating Composer

The solution is to update Composer to the latest version, which supports the new token format, as suggested by Jordi Boggiano on this tweet. "Composer 1.10.21 and 2.0.12 (both released April 1st) added support for the new GitHub token format."

The following command will install the latest version of Composer on your machine—v2.2.6 as of this writing. Note that future Composer updates will break the script as shown here, as the sha384 hash check won't pass.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Installing composer with brew

You can use Homebrew to install (or reinstall) composer on macOS.

brew install composer
brew reinstall composer

The brute-force fix

2021.04.20

As I mentioned above, both Lukas Kahwe Smith and Jordi Boggiano discouraged tinkering with Composer's auth.json file manually and recommended upgrading Composer to its latest version instead.

Still, here's the brute-force fix that worked for me. Apparently, editing the auth.json is the only way to update to the latest Composer programmatically, and you can revert it to its original state if you opt for this option. The alternative, of course, is to upgrade as shown above.

Edit the composer authentication configuration file ~/.composer/auth.json.

nano ~/.composer/auth.json

Then replace the following.

  "github-oauth": {
    "github.com": "ghp_[YOUR-PERSONAL-TOKEN]"
  }

With this (basic auth):

  "http-basic": {
    "github.com": {
      "username": "[YOUR-GITHUB-USERNAME]",
      "password": "ghp_[YOUR-PERSONAL-TOKEN]"
    }
  }

Source

Thanks

To Lukas Kahwe Smith and Jordi Boggiano for pointing this out on Twitter.

APRIL 20, 2021

I found this error while trying to update and install composer packages with composer install.

could not find driver (SQL: select * from information_schema.tables where table_schema = folio_burns and table_name = folio_items and table_type = 'BASE TABLE')

At first, I thought the solution was to edit /etc/php/7.4/cli/php.ini (for PHP-FPM 7.4 in my case) and uncomment the line ;extension=pdo_mysql to be like extension=pdo_mysql . But I was still getting this error as the mysql extension was missing.

PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_mysql' (tried: /usr/lib/php/20190902/pdo_mysql (/usr/lib/php/20190902/pdo_mysql: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/pdo_mysql.so (/usr/lib/php/20190902/pdo_mysql.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

The solution ended up being to install the extension, which would also add its own .ini file and activate itself on installation.

sudo apt-get install -y php7.4-mysql

Note that you can run this command with multiple extensions to be installed at once.

sudo apt-get install -y php7.4-{xml,bcmath,gd,mbstring,xsl,zip,curl,mysql}

APRIL 14, 2021

Here's how to get the raw String value of a Stringable object. Laravel's Illuminate\Support\Stringable has lots of helper functions, but sometimes you want to get the raw string value. For that, you just need to use the strval PHP built-in function on an object of the Stringable class.

// Define Stringable object
$stringable = Str::of('laravel-stringable-to-string');
get_class($stringable); // returns Illuminate\Support\Stringable
gettype($stringable); // returns object

// Get its raw String value
$string = strval($stringable);
get_class($string); // returns PHP Warning: get_class() expects parameter 1 to be object, string given in […]
gettype($string); // returns string

MARCH 8, 2021

Change this.

window.Vue = require('vue');

For this.

window.Vue = require('vue').default;

Then this should work.

window.Vue = require('vue').default;
window.VueResource = require('vue-resource');
window.Vue.use(window.VueResource);

MARCH 8, 2021

When trying to use the newest features of Laravel 8.x to automatically install Babel plugins with the .vue() call after using mix.js(), I kept getting the following error.

AssertionError [ERR_ASSERTION]: mix.js() is missing required parameter 1: entry

Even though I was using Laravel 8.x, my application had been migrated from older versions and my JavaScript dependencies in package.json were old. Specifically, I was still using Laravel Mix 5 while the newest version 6 had been released.

The solution was to change to version ^6.0.6 in package.json and run npm install, as the .vue() call is a new feature in Laravel Mix 6.

    "devDependencies": {
        // ...
        "laravel-mix": "^6.0.6",
        // ...
    },

Then on webpack.mix.js.

mix.js('resources/js/app.js', 'public/js')
   .vue();

I could then run the following commands successfully.

npm run dev
npm run watch
npm run hot
npm run prod

I believe this works with Vue 2 and Vue 3, and that the Vue version wasn't the problem but the version of Mix. (Note that I had also run npm update previously to update other dependencies and had to remove the --inline, --hide-modules and --no-progress options from my development and production commands in the scripts property of package.json.)

JUNE 2, 2020

In trying to use Artisan::call($command, $arguments) to execute a command exposed by my Laravel package—Folio—I was running into this issue.

The command "folio:clone" does not exist.

My commands were working on the terminal, by calling php artisan folio:clone, for instance, but they were not working programmatically, calling something like this.

Artisan::call('folio:clone 123 "New Title"');

Artisan::command was not a solution as it serves to register commands and not to execute them.

By looking into the FolioServiceProvider.php (the service provider of my own package) I noticed the $this->app->runningInConsole() check. My commands were being registered in the console but were not exposed elsewhere (that is, in the application itself).

I'd guess this is a security and performance measure. Commands that don't need to be available to the Laravel app are not registered for it.

Solution

The solution was simply registering the commands I want to be callable from my Laravel sites outside of the if statement that checks for $this->app->runningInConsole().

While eight commands are only available to run on the console, there's one available to both the console and the application's runtime.

if ($this->app->runningInConsole()) {
    $this->commands([
        \Nonoesp\Folio\Commands\GenerateSitemap::class,
        \Nonoesp\Folio\Commands\MigrateTemplate::class,
        \Nonoesp\Folio\Commands\TextAndTitleToJSON::class,
        \Nonoesp\Folio\Commands\ItemPropertiesExport::class,
        \Nonoesp\Folio\Commands\ItemPropertiesImport::class,
        \Nonoesp\Folio\Commands\ItemRetag::class,
        \Nonoesp\Folio\Commands\InstallCommand::class,
        \Nonoesp\Folio\Commands\CreateUserCommand::class,
    ]);      
}

$this->commands([
    \Nonoesp\Folio\Commands\ItemClone::class,
]);

In my case, I'm the maintainer of the package and could easily work around this limitation by taking the command I want to use in Laravel out of the if statement.

But you can register commands yourself in your app's $commands array in app/Console/Kernel.php. See the following example.

// app/Console/Kernel.php
protected $commands = [
    \Nonoesp\Folio\Commands\CreateUserCommand::class,
];

While the CreateUserCommand is only registered to the console by the package, I can explicitly make it available for my entire application calling it with Artisan::call('folio:user {email} {password}') (which is this command's signature).

Thanks!

I hope you found this useful. Feel free to ping me at @nonoesp, join the mailing list, or check out other Laravel posts and code-related publications.

FEBRUARY 26, 2020

I was getting this error in Laravel as I created a new app using the latest version (that is 6.2). Not sure why, the class would work locally but not remotely (on a deployment running in Ubuntu 18.04.3 on DigitalOcean to be precise).

I was using \ResourceBundle::getLocales('') to get a list of valid locales present on a website and then using in_array($needle, $array) to check whether a given locale is valid in PHP.

Here's how I fixed it.

  • composer require symfony/intl to install Symfony's Intl component.
  • Replaced my in_array calls with \Symfony\Component\Intl\Locales::exists($translation).

AUGUST 19, 2018

There is a nifty way to specify the way in which you want each of the pages (or Laravel routes) of your site to be indexed by search engines. In my case, I looked Robots meta tag and X-Robots-Tag HTTP header specifications to learn more about what was possible.

In short, you might tell Google a specific route or page has "no restrictions for indexing or serving" by setting the X-Robots-Tag HTTP header to all or, on the contrary, tell it to stop indexing (or saving cached versions of a page) with the noindex value.

In Laravel, the guys at Spatie made it really easy. Just install their spatie/laravel-robots-middleware composer package on your Laravel app with:

composer require spatie/laravel-robots-middleware

Let's see a few examples on how to use this.

Allow every single page to be indexed and served

Create a new middleware in your application.

// app/Http/Middleware/MyRobotsMiddleware.php

<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Spatie\RobotsMiddleware\RobotsMiddleware;

class MyRobotsMiddleware extends RobotsMiddleware
{
    /**
     * @return string|bool
     */
    protected function shouldIndex(Request $request)
    {
        return 'all';
    }
}

And then register your new in the middleware stack.

// app/Http/Kernel.php

class Kernel extends HttpKernel
{
    protected $middleware = [
        // ...
        \App\Http\Middleware\MyRobotsMiddleware::class,
    ];
    
    // ...
}

Forbid every single from being indexed, cached, and served

// app/Http/Middleware/BlockAllRobotsMiddleware.php

<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Spatie\RobotsMiddleware\RobotsMiddleware;

class BlockAllRobotsMiddleware extends RobotsMiddleware
{
    /**
     * @return string|bool
     */
    protected function shouldIndex(Request $request)
    {
        return 'noindex';
    }
}

Conditional robots middleware

Probably, the most interesting application of this middleware is to embed more intelligent logic to avoid indexing specific pages, but letting Google (and other search engines) crawl the pages you want to expose in search engines.

We could send a noindex header for our admin pages only, for instance.

// app/Http/Middleware/SelectiveRobotsMiddleware.php

<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Spatie\RobotsMiddleware\RobotsMiddleware;

class SelectiveRobotsMiddleware extends RobotsMiddleware
{
    protected function shouldIndex(Request $request) : string
    {
        if ($request->segment(1) === 'admin') {
            return 'noindex';
        }
        return 'all';
    }
}

Remember that you need to add all of your new middlewares to the app/Http/Kernel.php file in order for them to be called before each request. This method can be handing to block search indexing with noindex or to customize the way search engines are allow to process your pages. Here are other directives you can use in the x-robots-tag HTTP header and what they mean.

  • all - There are no restrictions for indexing or serving. Note: this directive is the default value and has no effect if explicitly listed.
  • noindex - Do not show this page in search results and do not show a "Cached" link in search results.
  • nofollow - Do not follow the links on this page
  • none - Equivalent to noindex, nofollow
  • noarchive - Do not show a "Cached" link in search results.
  • nosnippet - Do not show a text snippet or video preview in the search results for this page. A static thumbnail (if available) will still be visible.
  • notranslate - Do not offer translation of this page in search results.
  • noimageindex - Do not index images on this page.
  • unavailable_after: [RFC-850 date/time] - Do not show this page in search results after the specified date/time. The date/time must be specified in the RFC 850 format.

Thanks!

I hope you found this useful. Feel free to ping me at @nonoesp or join the mailing list. Here are some other Laravel posts and code-related posts.

APRIL 5, 2018

Laravel ships with the laravel-mix npm module installed. Laravel Mix is a wrapper around Webpack that simplifies the process of compiling your web assets (which seems to be useful for "80% of the use cases").

Still, if all you want is to use it for compiling your assets (say, compile your SCSS or SASS or Less styles to minified CSS, or compile and minify your JavaScript assets) it is not a trivial process to follow. I hope this notes help remove any problems you might find to get going.

Setting Up Your Project

Let's assume we are starting from scratch; a new empty folder. We'll want to initialize our npm directory as follows.

npm init

You can accept all the defaults of the npm init command. This will create a package.json in your directory.

Install Laravel Mix

Next, we need to install the following node packages: laravel-mix, webpack, and cross-env.

npm install laravel-mix cross-env

You can expect this command to take several minutes to install and compile all of the required dependencies. (It will also install webpack as it is a dependency of laravel-mix.)

Create Your webpack.mix.js File

Now, create the webpack.mix.js file, which serves as a configuration to specify via laravel-mix what you want webpack to do. Places this contents inside it:

let mix = require('laravel-mix');

mix.sass('assets/style.scss', 'dist')
   .js('assets/app.js', 'dist');

This will compile your SCSS assets (style.scss) to CSS (style.css) and place it inside the dist folder, and compile your JavaScript assets (app.js) also into the dist folder (app.js). (Make sure that you do place those files style.scs and app.js inside the assets folder (or specify what files you'd like to compile.

Add npm Commands to Your package.json

The only piece we are missing is a command to tell Webpack to process our operations. Place this command inside the "scripts" dictionary of your package.json file (that should have been created when you ran the npm init command.

"scripts": {
	"dev": "npm run development",
	"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
	"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
	"watch-poll": "npm run watch -- --watch-poll",
	"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
	"prod": "npm run production",
	"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},

Compile Your Assets

Now you can compile your assets for development (no minifying but faster).

npm run development

Or for production (slower but minified and less file size).

npm run production

Note that you can use npm run dev and npm run prod as shorthands instead of npm run development and npm run production.


You can also run the watch command while you are editing your assets during development — Laravel Mix will re-compile everything you make changes to one of your asset files and save. This will make your development experience even better. Just remember to compile in production before deploying your application so your assets get minified.

npm run watch

You are all set!

SEPTEMBER 16, 2017

To make sure your Laravel application doesn't break when you are applying changes to your database, it's a good practice to check whether a table exists or not before doing any calls.

\Schema::hasTable('users');

MARCH 2, 2017

Sort A Laravel Collection

When using Laravel, it is common to sort Eloquent models obtained with the query builder by calling ->orderBy('created_at', 'DESC'), for instance. But this is not always possible when arranging an Eloquent Collection (Illuminate\Database\Eloquent\Collection). To do this, we need to pass a sorting closure to the ->sortBy() method. (An example would be that our collection has the property order.) In that case, we could just call the following:

$items = $items->sortBy(function($item) {
  return -$item->order;
});

If your data is simple you can also use native PHP ways of sorting arrays like asort, arsort, ksort, or krsort.

JUNE 15, 2015

Useful Laravel Dependencies

Laravel is a powerful PHP framework. Like many other PHP systems, it benefits from the use of Composer to manage its dependencies, making the use of open-source libraries extremely simple.

What follows is a list of the packages (or dependencies) I am using in most of my current projects—let me explain you why.

dimsav/laravel-translatable

A package I discovered a few weeks ago. It gives your Laravel app the possibility of adding translations for your SQL tables to various different languages, with a really flexible structure. You can, for instance, have the articles in your blog written in English by default, and only translate to certain languages the ones you want.

Then, you can show those translated languages for users that have selected that locale on their browser, but fallback to the default language if an article is not available in their language.

Check it on Github.

jenssegers/laravel-date

A package based in Carbon. It makes ridiculously simple working with dates, with support for all languages.

Some of the features I use the most are: parsing database dates to human-readable ones (2015-06-14 could be translated to Sunday 14, June 2015); expressing how long ago a content was created (posted 2 minutes ago, for instance); calculating dates in the past or in the future, by adding or substracting days, weeks, months (or whatever unit) to a date object.

Possibilities are unlimited, and this library makes it even easier that before.

Check it on Github.

rtconner/laravel-tagging

With this package, you can use the Taggable trait to any of your models, and start tagging them. Then, you can use the query builder with its own methods to filter your content depending on tags.

I have been using it for articles and projects, to organize content and allow users to navigate by article categories.

Check it on Github.

panique/laravel-sass

If you are designing with SCSS, you need a parser o automate the generation of your CSS files. This package does the job for me pretty well.

It allows you to run inline PHP functions specifying what SCSS folder to parse, and where to save the CSS. Also, it has an in-built function to minify your CSS files, compressing them a lot, so you don’t have to worry about it.

For development purposes, I tend to set a GET variable on the App::before() filter function (located on app/filters.php) to force generate new CSS files. Running the URL /home/?scss=1, for instance, would regenerate all my CSS files.

Check it on Github

vtalbot/markdown

Based on Michelf’s PHP parser, this package implements methods to parse Markdown text from strings or files directly.

One example would be calling Markdown::string($string) in your code to parse the $string from markdown to HTML.

Check it on Github

If you know other PHP packages that I should know of, please drop me a tweet! Thanks for reading.

NOVEMBER 18, 2014

Localizing A Laravel Web App

Following the series of Laravel posts, here is how to make a Laravel app adapt to whatever language the visitor has set as a preference on its browser, or how to show a default language in case your website does not support your visitor's language.

When localizing a website, I usually use the following process:

  1. Set the locale to the user's preferred language.
  2. Fallback to English when the user's preferred language is not supported by your site.

To achieve this, the first thing we need to do is add the following lines inside the App::before method, on the file app/filters.php:

App::before(function($request)
{
	// Set the locale of the app to the user's browser language
	$browser_lang = substr(Request::server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
	App::setLocale($browser_lang);	
});

This will set the language to whatever first language the user browser has selected.

Then, set the fallback_locale variable inside the app/config/app.php to 'en', which will do for the falling back.

And that's it. Now just use the lang/ folder to add different language files in subfolders with their language code (i.e. lang/en/help.php for English, and lang/es/help.php for Spanish).

The strings inside the file should be wrapped in a PHP array, as follows:

<?php
return array(
  'terms' => 'términos',
  'help' => 'ayuda',
  'items' => 'artículos',
);

Lastly, you can use anywhere in your Laravel app the strings inside the help.php file, just by writing trans('help.terms'), for instance—which is a shorthand for Lang::get('help.terms'). That code, will get the terms value corresponding to the user’s language, or to the fallback language if the user’s language does not exist.

OCTOBER 8, 2014

Incoming Laravel Posts

After using Laravel in my website projects for the last year, I have been exploring many of its basic features. Along the way, I have documented the important tasks — useful at the beginning and development of each app — in article-like format.

From now on, I will be sharing some of those posts, on how to create a Laravel project, do the initial setup, and interact with various aspects, as localizing a site for different languages, for instance.

Tune in on Twitter at @nonoesp to hear about the posts, or add our feed to you RSS reader.

Want to see older publications? Visit the archive.

Listen to Getting Simple .