PHP

Laravel Essentials

What is Laravel?

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller architectural pattern and based on Symfony.

Laravel


Installation

Composer

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

$ composer create-project laravel/laravel your-project-name --prefer-dist

Alternatively, you can install Laravel Installer by running the following command in your terminal:

$ composer global require laravel/installer
$ laravel new <your-project-name>

For macOS, the Laravel installer may have issues with the laravel command not found. To fix this, add the following to your .zshrc file:

$ nano ~/.zshrc

Add the following line to the file:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Herd

Herd is a Laravel client software to allow you to manage your Laravel application environment. You can install all you need including Laravel installer, composer, Nginx, Node.js and multiple PHP versions with just simple clicks.

Download it here


Sanctum

$ php artisan install:api

Configuration


Localization

By default, the Laravel application skeleton does not include the lang directory. If you would like to customize Laravel's language files or create your own, you should scaffold the lang directory via the lang:publish Artisan command. The lang:publish command will create the lang directory in your application and publish the default set of language files used by Laravel:

$ php artisan lang:publish

Logging

Slack Webhook

Laravel provides a convenient way to send log messages to Slack using the Slack webhook.

  1. Create a new channel in your Slack workspace.
  2. Open channel settings and add an app.
  3. Search for "Incoming WebHooks" and add it to your channel.
  4. Configure the webhook settings and select the channel you created.
  5. Copy the webhook URL provided by Slack and add that URL in your .env file:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url

Request

What $request->query() does?

  • The method $request->query('key', $default = null) retrieves a value only from the query string (i.e., the ?key=value part of the URL). 
  • If the parameter was sent via POST body, JSON payload, etc., query() will not pick it up. 
  • Use it when you explicitly want to read “GET parameters” (query parameters) rather than input from the request body.

What $request->get() does?

  • $request->get('key', $default = null) is inherited from the underlying Symfony HTTP Foundation Request class (which Laravel’s Illuminate\Http\Request wraps). 
  • It tries to get the parameter from multiple sources (query string, request body, etc). Because of this, it’s less specific and in Laravel’s own docs and community it is often discouraged in favour of more specific methods. 
  • The Laravel community article notes that input() is preferred, and that get() may not support nested “dot” notation, etc. 

What $request->input() does?

While you didn’t ask about this specifically, it’s relevant for comparison:

  • $request->input('key', $default = null) grabs the value from any part of the request payload: query string, post body, JSON body, etc. 
  • It supports nested access (dot notation) and works consistently regardless of HTTP method. 
  • So if you just want “get me the input for this key” (regardless of where it came from), input() is often the right choice.

Troubleshooting

failed to listen on 127.0.0.1:8000 (reason ?)

This is happened when you run php artisan serve on Windows. To fix this, open the php.ini file and change variables_order = "EGPCS" to variables_order = "GPCS".

Previous
Drupal