### Install Laravel Installer Globally Source: https://laravel.com/docs/4.2/docs/4.2/quick Installs the Laravel installer tool globally via Composer, enabling quick creation of new Laravel projects. Ensure Composer's global vendor bin directory is in your system's PATH. ```bash composer global require "laravel/installer=~1.1" ``` -------------------------------- ### Manage Laravel Dependencies with Composer Source: https://laravel.com/docs/4.2/docs/4 After downloading Laravel, use these Composer commands to install or update the framework's dependencies. Git must be installed on the server for this process. ```bash php composer.phar install # or composer install ``` ```bash php composer.phar update ``` -------------------------------- ### Install Laravel Installer Globally via Composer Source: https://laravel.com/docs/4.2/docs/4 This command installs the Laravel installer globally using Composer, enabling faster project creation with the `laravel new` command. Ensure the Composer vendor bin directory is in your system's PATH. ```bash composer global require "laravel/installer=~1.1" ``` -------------------------------- ### Serve Laravel Application on a Custom Port Source: https://laravel.com/docs/4.2/docs/4.2/quick Starts the Laravel development server on a specified port using the `--port` argument. Useful when port 8000 is occupied or for running multiple applications concurrently. ```bash php artisan serve --port=8080 ``` -------------------------------- ### Serve Laravel Application with PHP's Built-in Server Source: https://laravel.com/docs/4.2/docs/4.2/quick Starts a development server for your Laravel application using PHP's built-in server, listening on port 8000 by default. This is convenient for local development. ```bash php artisan serve ``` -------------------------------- ### Install and Update Laravel Dependencies via Composer Source: https://laravel.com/docs/4.2/docs/4.2/installation After downloading the Laravel framework, these commands are used to install or update its dependencies using Composer. The 'install' command sets up the project's vendor directory, while 'update' refreshes existing dependencies. Note that Git must be installed on the server for this process to complete successfully. ```bash php composer.phar install ``` ```bash composer install ``` ```bash php composer.phar update ``` -------------------------------- ### Example Controller Action Using Laravel Facade Source: https://laravel.com/docs/4.2/docs/4.2/testing This code provides a simple Laravel controller action that demonstrates the use of a static facade, `Event::fire`, to trigger an event. This serves as a foundational example for understanding how facades are typically used in Laravel applications, which is then relevant for mocking them in tests. ```PHP public function getIndex() { Event::fire('foo', array('name' => 'Dayle')); return 'All done!'; } ``` -------------------------------- ### Create New Laravel 4.2 Project via Composer Source: https://laravel.com/docs/4.2/docs/4.2/quick Downloads and installs a fresh Laravel 4.2 application into a new directory using Composer. Replace `your-project-name` with the desired project folder name. ```bash composer create-project laravel/laravel your-project-name 4.2.* ``` -------------------------------- ### Install Laravel Installer Globally via Composer Source: https://laravel.com/docs/4.2/docs/4.2/installation This command installs the Laravel Installer globally using Composer, making the 'laravel' command available system-wide for quickly creating new Laravel projects. Ensure your Composer vendor bin directory (~/.composer/vendor/bin) is added to your system's PATH environment variable for the command to be found. ```bash composer global require "laravel/installer=~1.1" ``` -------------------------------- ### Create New Laravel 4.2 Project with Composer Source: https://laravel.com/docs/4.2/docs/4 Use this Composer command to create a fresh Laravel 4.2 project in a specified directory. This method installs all necessary dependencies directly. ```bash composer create-project laravel/laravel {directory} 4.2 --prefer-dist ``` -------------------------------- ### Create New Laravel 4.2 Project via Composer Source: https://laravel.com/docs/4.2/docs/4.2/installation This Composer command creates a fresh Laravel 4.2 project in the specified directory, downloading all necessary dependencies. It's an alternative to using the Laravel Installer for setting up a new application, directly leveraging Composer's project creation capabilities. ```bash composer create-project laravel/laravel {directory} 4.2 --prefer-dist ``` -------------------------------- ### Define a Basic Laravel 4.2 Closure Route Source: https://laravel.com/docs/4.2/docs/4.2/quick Illustrates how to define a simple GET route in `app/routes.php` that maps a URL to an anonymous function, returning a direct response. This is the simplest form of routing. ```php Route::get('users', function() { return 'Users!'; }); ``` -------------------------------- ### Call Laravel Routes from Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing This example shows how to simulate HTTP requests to Laravel routes within a test using the `call` method. It demonstrates both a simple GET request and a more complex call with various parameters, returning an `Illuminate\Http\Response` object for further inspection. ```PHP $response = $this->call('GET', 'user/profile'); $response = $this->call($method, $uri, $parameters, $files, $server, $content); ``` -------------------------------- ### Install Laravel Homestead Manually via Git Source: https://laravel.com/docs/4.2/docs/4.2/homestead Clones the Laravel Homestead repository from GitHub. This method is an alternative to Composer installation, especially useful if PHP is not installed locally. It's recommended to clone it into a central directory for all Laravel projects. ```Bash git clone https://github.com/laravel/homestead.git Homestead ``` -------------------------------- ### Define a Laravel 4.2 Route to a Controller Action Source: https://laravel.com/docs/4.2/docs/4.2/quick Shows how to map a URL to a specific method within a controller class. This approach promotes better organization and separation of concerns in Laravel applications. ```php Route::get('users', 'UserController@getIndex'); ``` -------------------------------- ### Example Laravel Database Seeder Classes Source: https://laravel.com/docs/4.2/docs/4.2/migrations Provides example PHP classes for seeding a Laravel database. It illustrates how to define a `DatabaseSeeder` to call other seeder classes and a `UserTableSeeder` to insert sample user data into the `users` table. ```php class DatabaseSeeder extends Seeder { public function run() { $this->call('UserTableSeeder'); $this->command->info('User table seeded!'); } } class UserTableSeeder extends Seeder { public function run() { DB::table('users')->delete(); User::create(array('email' => 'example@example.com')); } } ``` -------------------------------- ### Define a Laravel Route to Return a Blade View Source: https://laravel.com/docs/4.2/docs/4.2/quick This PHP snippet demonstrates how to configure a Laravel route (`/users`) to render and return a Blade view instead of a simple string. It uses `View::make('users')` to instantiate and display the `users.blade.php` template. ```PHP Route::get('users', function() { return View::make('users'); }); ``` -------------------------------- ### Install Laravel Homestead CLI Tool via Composer Source: https://laravel.com/docs/4.2/docs/4.2/homestead Installs the Homestead command-line interface (CLI) tool globally using Composer. This allows for easy management of Homestead environments. Ensure Composer's global bin directory (~/.composer/vendor/bin) is in your system's PATH. ```Bash composer global require "laravel/homestead=~2.0" ``` -------------------------------- ### Declare Variables and Run PHP with Envoy @setup Source: https://laravel.com/docs/4.2/docs/4.2/ssh Explains how to use the `@setup` directive in an Envoy file to declare PHP variables and perform initial setup logic before tasks are executed. ```Envoy Blade @setup $now = new DateTime(); $environment = isset($env) ? $env : "testing"; @endsetup ``` -------------------------------- ### Install Laravel Envoy Globally Source: https://laravel.com/docs/4.2/docs/4.2/ssh Instructions for installing Laravel Envoy globally via Composer. This command makes the `envoy` executable available system-wide for defining and running remote tasks. ```Bash composer global require "laravel/envoy=~1.0" ``` -------------------------------- ### Manage Session State in Laravel Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing Provides examples of setting session data and flushing the session entirely within Laravel tests. ```PHP $this->session(['foo' => 'bar']); $this->flushSession(); ``` -------------------------------- ### Get Application Root Path (base_path) Source: https://laravel.com/docs/4.2/docs/4.2/helpers Get the fully qualified path to the root of the application install. ```APIDOC base_path() ``` -------------------------------- ### Execute Pending Database Migrations with Artisan Source: https://laravel.com/docs/4.2/docs/4.2/quick This command-line snippet runs all pending database migrations. Executing `php artisan migrate` applies the schema changes defined in the `up` methods of all new migration files to the configured database, updating its structure. ```Bash php artisan migrate ``` -------------------------------- ### Define a Basic Laravel View Source: https://laravel.com/docs/4.2/docs/4.2/responses Provides an example of a simple HTML view file (`greeting.php`) stored in `app/views`, demonstrating how to display a PHP variable. ```HTML

Hello,

``` -------------------------------- ### Retrieve All Users and Pass to View in Laravel Route Source: https://laravel.com/docs/4.2/docs/4.2/quick This Laravel route defines a GET request handler for the `/users` URL. It uses the `User::all()` method to fetch all records from the `users` table via Eloquent and then passes these records to the `users` view using the `with` method, making them available for display. ```PHP Route::get('users', function() { $users = User::all(); return View::make('users')->with('users', $users); }); ``` -------------------------------- ### Initialize Envoy Configuration File Source: https://laravel.com/docs/4.2/docs/4.2/ssh Command to quickly generate a stub `Envoy.blade.php` file. This command simplifies the initial setup of Envoy for a project. ```Bash envoy init user@192.168.1.1 ``` -------------------------------- ### Querying Eloquent Models with Where and Get Source: https://laravel.com/docs/4.2/docs/4.2/eloquent This example demonstrates how to build more complex queries using Eloquent models by chaining methods like `where()` and `take()`, similar to the query builder. It also shows how to iterate over the results. ```PHP $users = User::where('votes', '>', 100)->take(10)->get(); foreach ($users as $user) { var_dump($user->name); } ``` -------------------------------- ### Registering Before and After Application Events in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/lifecycle Demonstrates how to register `before` and `after` application events in Laravel to perform pre and post request processing. These events are useful for global filtering or modifying responses and can be registered in start files or service providers. ```PHP App::before(function($request) { // }); App::after(function($request, $response) { // }); ``` -------------------------------- ### Configure Pretty URLs for Laravel on Nginx Source: https://laravel.com/docs/4.2/docs/4.2/installation This Nginx server block directive allows 'pretty URLs' for Laravel applications by attempting to serve static files or directories, then falling back to index.php for all other requests. This ensures Laravel's routing system correctly handles all incoming URLs. ```nginx location / { try_files $uri $uri/ /index.php?$query_string; } ``` -------------------------------- ### Using a Custom Facade Source: https://laravel.com/docs/4.2/docs/4.2/facades After setting up the IoC binding and the custom facade class, this example shows how to use the `Payment` facade. Calling `Payment::process()` will internally resolve the `PaymentGateway\Payment` instance from the container and execute its `process` method. ```PHP Payment::process(); ``` -------------------------------- ### Configure Pretty URLs for Laravel on Apache Source: https://laravel.com/docs/4.2/docs/4.2/installation This .htaccess configuration enables 'pretty URLs' for Laravel applications running on Apache by rewriting requests to index.php. It is crucial to ensure that the 'mod_rewrite' module is enabled on your Apache server for this configuration to function correctly. ```apache Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] ``` -------------------------------- ### Configure Apache for Laravel Pretty URLs Source: https://laravel.com/docs/4.2/docs/4 This `.htaccess` configuration enables 'pretty URLs' for Laravel applications on Apache by rewriting requests to `index.php`. Ensure the `mod_rewrite` module is enabled in your Apache setup. ```apache Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] ``` -------------------------------- ### Assert HTTP Response Status in Laravel Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing Laravel provides several convenient assertion methods to simplify testing. This example demonstrates the use of `assertResponseOk()` to verify that an HTTP response from a test call has a successful (200 OK) status code, ensuring the route or action executed without errors. ```PHP public function testMethod() { $this->call('GET', '/'); $this->assertResponseOk(); } ``` -------------------------------- ### Configure Nginx for Laravel Pretty URLs Source: https://laravel.com/docs/4.2/docs/4 This Nginx server block directive allows 'pretty URLs' for Laravel applications. It attempts to serve static files or directories first, then falls back to routing requests through `index.php`. ```nginx location / { try_files $uri $uri/ /index.php?$query_string; } ``` -------------------------------- ### Create a Blade Child View Extending a Layout Source: https://laravel.com/docs/4.2/docs/4.2/quick This snippet creates `users.blade.php`, a child view that inherits from `layout.blade.php` using `@extends('layout')`. It populates the layout's content area with 'Users!' via the `@section('content')` directive, demonstrating template inheritance. ```Blade @extends('layout') @section('content') Users! @stop ``` -------------------------------- ### Defining a Custom Class for Facade Source: https://laravel.com/docs/4.2/docs/4.2/facades This example defines a simple PHP class, `PaymentGateway\Payment`, which will be exposed later through a custom facade. This class contains a `process` method. ```PHP namespace PaymentGateway; class Payment { public function process() { // } } ``` -------------------------------- ### Artisan Commands for Database Session Setup Source: https://laravel.com/docs/4.2/docs/4.2/session Lists the Artisan and Composer commands necessary to generate the session migration, dump the autoloader, and run the migration to set up the `sessions` table for the database session driver. ```Bash php artisan session:table composer dump-autoload php artisan migrate ``` -------------------------------- ### Generate and Download Invoice PDF Source: https://laravel.com/docs/4.2/docs/4.2/billing This example shows how to generate and initiate a download for an invoice as a PDF file. The `downloadInvoice` method requires the invoice ID and accepts an array of options for customizing the vendor and product details displayed on the PDF. ```PHP return $user->downloadInvoice($invoice->id, [ 'vendor' => 'Your Company', 'product' => 'Your Product' ]); ``` -------------------------------- ### Re-seed Database in Laravel Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing Demonstrates how to re-seed the database from a test, either running all seeders or a specific seeder class, in Laravel. ```PHP $this->seed(); $this->seed('DatabaseSeeder'); ``` -------------------------------- ### Generate a New Database Migration File with Artisan Source: https://laravel.com/docs/4.2/docs/4.2/quick This command-line snippet uses Laravel's Artisan CLI to generate a new migration file. The command `php artisan migrate:make create_users_table` creates a timestamped PHP file in `app/database/migrations` with `up` and `down` methods, ready for defining database schema changes. ```Bash php artisan migrate:make create_users_table ``` -------------------------------- ### Define Basic GET Route in Laravel 4.2 Source: https://laravel.com/docs/4.2/docs/4.2/routing Demonstrates how to define a simple GET route in Laravel 4.2. The route associates the root URI ('/') with a Closure callback that returns the string 'Hello World'. Routes are typically configured in the `app/routes.php` file. ```PHP Route::get('/', function() { return 'Hello World'; }); ``` -------------------------------- ### Generating Basic Drop-Down List (Laravel Form Helper) Source: https://laravel.com/docs/4.2/docs/4.2/html Provides an example of creating a simple HTML select (drop-down) list. The `Form::select` method takes the field name and an array of options as key-value pairs. ```PHP echo Form::select('size', array('L' => 'Large', 'S' => 'Small')); echo Form::select('size', array('L' => 'Large', 'S' => 'Small')); ``` -------------------------------- ### Initialize Homestead Configuration File (Manual Git) Source: https://laravel.com/docs/4.2/docs/4.2/homestead Runs the initialization script to create the 'Homestead.yaml' configuration file after manually cloning the Homestead repository. The file will be placed in the '~/.homestead' directory, ready for customization. ```Bash bash init.sh ``` -------------------------------- ### Get Storage Directory Path (storage_path) Source: https://laravel.com/docs/4.2/docs/4.2/helpers Get the fully qualified path to the `app/storage` directory. ```APIDOC storage_path() ``` -------------------------------- ### Run Laravel Package Migrations for Installed Package Source: https://laravel.com/docs/4.2/docs/4.2/packages This Artisan command, using the `--package` directive, runs database migrations for a package installed via Composer into the `vendor` directory, ensuring its database schema is up-to-date. ```PHP php artisan migrate --package="vendor/package" ``` -------------------------------- ### Laravel Core Component API References Source: https://laravel.com/docs/4.2/docs/4.2/facades Provides a quick reference to key Laravel core components, including their fully qualified class names and links to detailed API documentation for Validator and View services. ```APIDOC Validator (Instance): Class: Illuminate\\Validation\\Validator Description: Represents an instance of the Laravel Validator. View (Factory): Class: Illuminate\\View\\Factory Description: The factory responsible for creating view instances. Facade Accessor: view View (Instance): Class: Illuminate\\View\\View Description: Represents an instance of a Laravel View. ``` -------------------------------- ### Loading a View from a Laravel Package Source: https://laravel.com/docs/4.2/docs/4.2/packages This example demonstrates the standard double-colon syntax used to load a view that belongs to a registered package. The `package::view.name` format allows Laravel to correctly resolve the view from the specified package's resource directory. ```PHP return View::make('package::view.name'); ``` -------------------------------- ### Registering a Laravel Service Provider at Run-Time Source: https://laravel.com/docs/4.2/docs/4.2/ioc This example demonstrates how to register a service provider dynamically during application execution using the `App::register` method. This allows for flexible registration of service providers based on specific application logic or conditions, rather than solely relying on the configuration file. ```PHP App::register('FooServiceProvider'); ``` -------------------------------- ### Auth (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Auth (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: Auth (Instance) Class: Illuminate\Auth\Guard IoC Binding: ``` -------------------------------- ### View Help Screen for an Artisan Command Source: https://laravel.com/docs/4.2/docs/4.2/artisan Every command includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, simply precede the name of the command with `help`. ```PHP php artisan help migrate ``` -------------------------------- ### Queue (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Queue (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: Queue (Instance) Class: Illuminate\Queue\QueueInterface IoC Binding: ``` -------------------------------- ### Get Current Laravel Controller Action Name Source: https://laravel.com/docs/4.2/docs/4.2/controllers This example shows how to retrieve the name of the currently executing controller action using `Route::currentRouteAction()`. This can be useful for logging, debugging, or conditional logic based on the active route. ```PHP $action = Route::currentRouteAction(); ``` -------------------------------- ### Demonstrate N+1 Query Problem in Eloquent Source: https://laravel.com/docs/4.2/docs/4.2/eloquent This example illustrates the N+1 query problem: one query to get all books, and then an additional query for each book to retrieve its author, leading to inefficient database access. ```PHP foreach (Book::all() as $book) { echo $book->author->name; } ``` -------------------------------- ### Display Current Laravel Version Source: https://laravel.com/docs/4.2/docs/4.2/artisan Shows how to check the installed version of your Laravel application using the `--version` option with the `php artisan` command. This helps in verifying the framework version. ```PHP php artisan --version ``` -------------------------------- ### DB (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel DB (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: DB (Instance) Class: Illuminate\Database\Connection IoC Binding: ``` -------------------------------- ### Session (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Session (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: Session (Instance) Class: Illuminate\Session\Store IoC Binding: ``` -------------------------------- ### Verify a Password Against a Hash in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/security This example shows how to verify a plain text password against a previously stored Bcrypt hash using Laravel's `Hash::check` method. It returns `true` if the passwords match, and `false` otherwise. ```PHP if (Hash::check('secret', $hashedPassword)) { // The passwords match... } ``` -------------------------------- ### Customize Password Reminder Email Subject Source: https://laravel.com/docs/4.2/docs/4.2/security Within the `postRemind` method of the `RemindersController`, you can customize the password reminder email before it's sent. This example demonstrates how to set the email's subject using the `Password::remind` method's callback. ```PHP Password::remind(Input::only('email'), function($message) { $message->subject('Password Reminder'); }); ``` -------------------------------- ### Queue an Event for Later Firing in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/events This example demonstrates how to 'queue' an event using `Event::queue`, which stages the event to be fired later rather than immediately. This is useful for deferring event processing. ```PHP Event::queue('foo', array($user)); ``` -------------------------------- ### Insert Records into a Table with Laravel Query Builder Source: https://laravel.com/docs/4.2/docs/4.2/queries Shows a simple example of inserting a single record into a database table using the `insert` method. ```PHP DB::table('users')->insert( array('email' => '[email protected]', 'votes' => 0) ); ``` -------------------------------- ### Define an Eloquent Model in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/quick This snippet defines a basic Eloquent model named `User`. By convention, Eloquent will associate this model with the `users` table in the database. This model serves as an interface for querying and representing rows in that table. ```PHP class User extends Eloquent {} ``` -------------------------------- ### Access Configured Homestead Site via Browser Source: https://laravel.com/docs/4.2/docs/4.2/homestead After configuring your Nginx site and updating your local `hosts` file, you can access your Laravel application in the browser using the mapped domain. This snippet provides an example URL. ```Text http://homestead.app ``` -------------------------------- ### Input Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Input Facade, including its underlying class and IoC binding. ```APIDOC Facade: Input Class: Illuminate\Http\Request IoC Binding: request ``` -------------------------------- ### Call Laravel Controllers from Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing You can directly invoke controller actions within tests using the `action` method. This provides a way to test controller logic in isolation. Examples include calling a simple index action and an action with specific parameters. ```PHP $response = $this->action('GET', 'HomeController@index'); $response = $this->action('GET', 'UserController@profile', array('user' => 1)); ``` -------------------------------- ### List All Available Artisan Commands Source: https://laravel.com/docs/4.2/docs/4.2/artisan To view a list of all available Artisan commands, you may use the `list` command. ```PHP php artisan list ``` -------------------------------- ### Equivalent IoC Container Call for Cache Facade Source: https://laravel.com/docs/4.2/docs/4.2/facades This snippet illustrates the underlying mechanism of a facade call. It shows how `Cache::get('key')` is internally resolved by Laravel, which fetches the 'cache' binding from the IoC container and then calls the `get` method on the resolved object. ```PHP $value = $app->make('cache')->get('key'); ``` -------------------------------- ### Authenticate a User with Credentials in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/security This snippet illustrates how to log a user into your application using `Auth::attempt` with an array of credentials like email and password. Upon successful authentication, it redirects the user to their intended destination or a fallback URI. ```PHP if (Auth::attempt(array('email' => $email, 'password' => $password))) { return Redirect::intended('dashboard'); } ``` -------------------------------- ### Starting the Laravel Queue Listener Source: https://laravel.com/docs/4.2/docs/4.2/queues Initiates the Laravel Artisan task that continuously processes new jobs from the queue. This command runs indefinitely until manually stopped and can be managed by process monitors like Supervisor. ```PHP php artisan queue:listen ``` -------------------------------- ### Registering a Laravel Package with Default Conventions Source: https://laravel.com/docs/4.2/docs/4.2/packages This snippet shows the basic usage of the `$this->package()` method within a service provider's `boot` method. It informs Laravel how to load views, configuration, and other resources for a package based on workbench conventions, typically requiring no modification. ```PHP $this->package('vendor/package'); ``` -------------------------------- ### Define Envoy Task in Envoy.blade.php Source: https://laravel.com/docs/4.2/docs/4.2/ssh An example `Envoy.blade.php` file demonstrating how to define remote servers and a basic task. Tasks contain Bash commands that will be executed on the specified servers. ```Envoy Blade @servers(['web' => '192.168.1.1']) @task('foo', ['on' => 'web']) ls -la @endtask ``` -------------------------------- ### Redirect Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Redirect Facade, including its underlying class and IoC binding. ```APIDOC Facade: Redirect Class: Illuminate\Routing\Redirector IoC Binding: redirect ``` -------------------------------- ### Define Schema for Users Table in Laravel Migration Source: https://laravel.com/docs/4.2/docs/4.2/quick This PHP snippet defines the `up` and `down` methods within a Laravel migration. The `up` method creates a `users` table with an auto-incrementing `id`, a unique `email` string, a `name` string, and `timestamps`. The `down` method reverses this by dropping the `users` table. ```PHP public function up() { Schema::create('users', function($table) { $table->increments('id'); $table->string('email')->unique(); $table->string('name'); $table->timestamps(); }); } public function down() { Schema::drop('users'); } ``` -------------------------------- ### Implement Methods for Laravel Implicit Controller Source: https://laravel.com/docs/4.2/docs/4.2/controllers Shows how to structure methods within a Laravel implicit controller. Methods are prefixed with HTTP verbs (e.g., `get`, `post`, `any`) to automatically handle corresponding HTTP requests to the controller's base URI. ```PHP class UserController extends BaseController { public function getIndex() { // } public function postProfile() { // } public function anyLogin() { // } } ``` -------------------------------- ### Add New Nginx Site Using Homestead Serve Script Source: https://laravel.com/docs/4.2/docs/4.2/homestead This command demonstrates how to dynamically add an additional Nginx site to a running Homestead environment. Executed via SSH into the Homestead box, the `serve` script maps a new domain to a specified public directory, simplifying site management without requiring a full `vagrant provision`. ```Shell serve domain.app /home/vagrant/Code/path/to/public/directory ``` -------------------------------- ### Define a Base Blade Layout View in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/quick This snippet defines `layout.blade.php`, a foundational HTML layout using Laravel's Blade templating. It includes a `@yield('content')` directive, serving as a placeholder for content injected by child views, ensuring a consistent application structure. ```HTML

Laravel Quickstart

@yield('content') ``` -------------------------------- ### Retrieve Input Value with a Default in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests This example shows how to retrieve an input value, providing a default value that will be returned if the requested input key is not present in the request. This ensures a fallback value is always available. ```PHP $name = Input::get('name', 'Sally'); ``` -------------------------------- ### Add Laravel Homestead Vagrant Box Source: https://laravel.com/docs/4.2/docs/4.2/homestead Adds the official Laravel Homestead Vagrant box to your Vagrant installation. This command downloads the pre-configured virtual machine image. An alternative command is provided for older Vagrant versions that require a direct URL. ```Bash vagrant box add laravel/homestead ``` ```Bash vagrant box add laravel/homestead https://atlas.hashicorp.com/laravel/boxes/homestead ``` -------------------------------- ### Paginator (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Paginator (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: Paginator (Instance) Class: Illuminate\Pagination\Paginator IoC Binding: ``` -------------------------------- ### Run Composer Update for Laravel Application Source: https://laravel.com/docs/4.2/docs/4.2/upgrade After completing the necessary file and configuration updates, run `composer update` to update core application files. If class load errors occur, use the `--no-scripts` option. ```Shell composer update # If class load errors occur: composer update --no-scripts ``` -------------------------------- ### Starting Daemon Queue Workers in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/releases The `queue:work` Artisan command now supports a `--daemon` option, allowing workers to continuously process jobs without re-booting the framework. This significantly reduces CPU usage but may complicate deployment. ```Bash php artisan queue:work --daemon ``` -------------------------------- ### Manually Begin a Database Transaction in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/database This snippet shows how to manually start a database transaction using `DB::beginTransaction()`. This provides explicit control over transaction boundaries, allowing for custom commit or rollback logic. ```PHP DB::beginTransaction(); ``` -------------------------------- ### Specify HTTP Method for a Laravel Form Source: https://laravel.com/docs/4.2/docs/4.2/html This example shows how to explicitly define the HTTP method for a form, such as `PUT`. Since HTML forms natively support only `POST` and `GET`, Laravel automatically spoofs `PUT` and `DELETE` methods by adding a hidden `_method` field to the form, ensuring proper routing. ```PHP echo Form::open(array('url' => 'foo/bar', 'method' => 'put')) ``` -------------------------------- ### Display User Data in Laravel Blade View Source: https://laravel.com/docs/4.2/docs/4.2/quick This Blade template snippet demonstrates how to iterate over a collection of `$users` passed from a controller or route. It extends a `layout` and displays each user's `name` within a paragraph tag, using Blade's double curly brace syntax for echoing data. ```Blade @extends('layout') @section('content') @foreach($users as $user)

{{ $user->name }}

@endforeach @stop ``` -------------------------------- ### SSH (Instance) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel SSH (Instance) Facade, including its underlying class and IoC binding. ```APIDOC Facade: SSH (Instance) Class: Illuminate\Remote\Connection IoC Binding: ``` -------------------------------- ### File Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel File Facade, including its underlying class and IoC binding. ```APIDOC Facade: File Class: Illuminate\Filesystem\Filesystem IoC Binding: files ``` -------------------------------- ### Run a Basic Envoy Task Source: https://laravel.com/docs/4.2/docs/4.2/ssh Demonstrates how to execute a simple task defined in an Envoy file using the `envoy run` command. ```Bash envoy run foo ``` -------------------------------- ### Update Global Composer Packages (including Envoy) Source: https://laravel.com/docs/4.2/docs/4.2/ssh Shows how to update globally installed Composer packages, which can include Envoy if it was installed via Composer's global installer. ```Bash composer global update ``` -------------------------------- ### Route to a Namespaced Laravel Controller Action Source: https://laravel.com/docs/4.2/docs/4.2/controllers When controllers are organized using PHP namespaces, this example shows how to specify the fully qualified class name in the route definition. This ensures Laravel correctly locates the controller and its method. ```PHP Route::get('foo', 'Namespace\FooController@method'); ``` -------------------------------- ### Perform a Basic Redirect Source: https://laravel.com/docs/4.2/docs/4.2/responses Shows how to redirect the user to a specified URL using the `Redirect::to` method. ```PHP return Redirect::to('user/login'); ``` -------------------------------- ### App Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel App Facade, including its underlying class and IoC binding. ```APIDOC Facade: App Class: Illuminate\Foundation\Application IoC Binding: app ``` -------------------------------- ### Get Public Directory Path (public_path) Source: https://laravel.com/docs/4.2/docs/4.2/helpers Get the fully qualified path to the `public` directory. ```APIDOC public_path() ``` -------------------------------- ### Accessing Laravel 4.1 Full Change List Source: https://laravel.com/docs/4.2/docs/4.2/releases The complete list of changes for Laravel 4.1 can be viewed by running the `php artisan changes` command from a 4.1 installation. This provides a detailed overview of all enhancements and modifications. ```Bash php artisan changes ``` -------------------------------- ### Update Envoy Installation via self-update Command Source: https://laravel.com/docs/4.2/docs/4.2/ssh Provides the command to update the Envoy installation directly using its built-in `self-update` feature. ```Bash envoy self-update ``` -------------------------------- ### Queue Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Queue Facade, including its underlying class and IoC binding. ```APIDOC Facade: Queue Class: Illuminate\Queue\QueueManager IoC Binding: queue ``` -------------------------------- ### Get All Request Input in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests This code demonstrates how to retrieve all user input submitted with the current request as an associative array. This includes data from GET, POST, and other request methods. ```PHP $input = Input::all(); ``` -------------------------------- ### Subscribe User to a Plan with a Coupon Source: https://laravel.com/docs/4.2/docs/4.2/billing This snippet shows how to apply a coupon code when creating a new subscription by chaining the `withCoupon` method before calling `create`. ```PHP $user->subscription('monthly') ->withCoupon('code') ->create($creditCardToken); ``` -------------------------------- ### Log Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Log Facade, including its underlying class and IoC binding. ```APIDOC Facade: Log Class: Illuminate\Log\Writer IoC Binding: log ``` -------------------------------- ### Starting Laravel Daemon Queue Worker Source: https://laravel.com/docs/4.2/docs/4.2/queues Initiates a queue worker in daemon mode, allowing it to continuously process jobs without re-booting the framework, which significantly reduces CPU usage. This mode requires careful handling during deployments to drain existing jobs. ```PHP php artisan queue:work connection --daemon php artisan queue:work connection --daemon --sleep=3 php artisan queue:work connection --daemon --sleep=3 --tries=3 ``` -------------------------------- ### Implementing the Cache Store Interface for a Custom Driver Source: https://laravel.com/docs/4.2/docs/4.2/extending Defines the `MongoStore` class, which implements the `Illuminate\Cache\StoreInterface`. This interface requires specific methods like `get`, `put`, `increment`, `decrement`, `forever`, `forget`, and `flush` to be implemented for a custom cache driver to function correctly. ```PHP class MongoStore implements Illuminate\Cache\StoreInterface { public function get($key) {} public function put($key, $value, $minutes) {} public function increment($key, $value = 1) {} public function decrement($key, $value = 1) {} public function forever($key, $value) {} public function forget($key) {} public function flush() {} } ``` -------------------------------- ### Download Files and Strings via SFTP Source: https://laravel.com/docs/4.2/docs/4.2/ssh Shows how to download files from a remote server to a local path using `get` and retrieve file contents directly as a string using `getString` via SFTP. Both methods operate on a specified SSH connection. ```PHP SSH::into('staging')->get($remotePath, $localPath); $contents = SSH::into('staging')->getString($remotePath); ``` -------------------------------- ### Instantiate Laravel Application Core Source: https://laravel.com/docs/4.2/docs/4.2/extending This snippet shows the foundational line of code that instantiates the Laravel application. This `Application` instance is responsible for bootstrapping the framework, including binding the default `Illuminate\Http\Request` instance to the IoC container. Understanding this step is crucial for customizing core components like the Request class. ```PHP $app = new \Illuminate\Foundation\Application; ``` -------------------------------- ### Queue (Base Class) Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Queue (Base Class) Facade, including its underlying class and IoC binding. ```APIDOC Facade: Queue (Base Class) Class: Illuminate\Queue\Queue IoC Binding: ``` -------------------------------- ### Retrieve Request URI in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Demonstrates how to get the URI of the current request using `Request::path()`. ```PHP $uri = Request::path(); ``` -------------------------------- ### Initialize Homestead Configuration File Source: https://laravel.com/docs/4.2/docs/4.2/homestead Generates the 'Homestead.yaml' configuration file in the '~/.homestead' directory. This file is used to customize your Homestead environment settings, such as shared folders and SSH keys. ```Bash homestead init ``` -------------------------------- ### Laravel Validation Rule: confirmed Source: https://laravel.com/docs/4.2/docs/4.2/validation Ensures a field has a matching `_confirmation` field. For example, `password` requires `password_confirmation`. ```APIDOC confirmed: Description: The field under validation must have a matching field of `foo_confirmation`. Example: If the field is `password`, `password_confirmation` must be present in the input. ``` -------------------------------- ### Running Laravel Database Migrations Source: https://laravel.com/docs/4.2/docs/4.2/migrations Explains how to execute pending database migrations using the `php artisan migrate` command. It covers running all outstanding migrations, migrations for a specific path or package, and forcing destructive operations in production environments. ```php php artisan migrate ``` ```php php artisan migrate --path=app/foo/migrations ``` ```php php artisan migrate --package=vendor/package ``` ```php php artisan migrate --force ``` -------------------------------- ### Retrieve Uploaded File Size in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Explains how to get the size of an uploaded file in bytes using `getSize()`. ```PHP $size = Input::file('photo')->getSize(); ``` -------------------------------- ### Retrieve Uploaded File MIME Type in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Shows how to get the MIME type of an uploaded file using `getMimeType()`. ```PHP $mime = Input::file('photo')->getMimeType(); ``` -------------------------------- ### Config Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Config Facade, including its underlying class and IoC binding. ```APIDOC Facade: Config Class: Illuminate\Config\Repository IoC Binding: config ``` -------------------------------- ### Retrieve Uploaded File Extension in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Demonstrates how to get the original file extension of an uploaded file using `getClientOriginalExtension()`. ```PHP $extension = Input::file('photo')->getClientOriginalExtension(); ``` -------------------------------- ### URL Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel URL Facade, including its underlying class and IoC binding. ```APIDOC Facade: URL Class: Illuminate\Routing\UrlGenerator IoC Binding: url ``` -------------------------------- ### Retrieve an Uploaded File in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Explains how to get an instance of the `UploadedFile` class for an uploaded file using `Input::file()`. ```PHP $file = Input::file('photo'); ``` -------------------------------- ### HTML Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel HTML Facade, including its underlying class and IoC binding. ```APIDOC Facade: HTML Class: Illuminate\Html\HtmlBuilder IoC Binding: html ``` -------------------------------- ### Log Out User in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/security Logs the currently authenticated user out of the application, clearing their session and authentication state. ```PHP Auth::logout(); ``` -------------------------------- ### Auth Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Auth Facade, including its underlying class and IoC binding. ```APIDOC Facade: Auth Class: Illuminate\Auth\AuthManager IoC Binding: auth ``` -------------------------------- ### Retrieve Original Uploaded File Name in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Shows how to get the original filename as it was on the client's machine using `getClientOriginalName()`. ```PHP $name = Input::file('photo')->getClientOriginalName(); ``` -------------------------------- ### Calling Laravel Cache Facade Source: https://laravel.com/docs/4.2/docs/4.2/facades This snippet demonstrates a typical call to a Laravel facade, which appears to be a static method call on the `Cache` class. ```PHP $value = Cache::get('key'); ``` -------------------------------- ### Update BaseController Use Statement in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/upgrade Change the `use` statement in `app/controllers/BaseController.php` to reflect the updated namespace for `Controller`. ```PHP // Before: use Illuminate\Routing\Controllers\Controller; // After: use Illuminate\Routing\Controller; ``` -------------------------------- ### Configure Nginx Sites in Homestead.yaml Source: https://laravel.com/docs/4.2/docs/4.2/homestead This configuration snippet demonstrates how to map a domain to a public directory within your Homestead environment using the `sites` property. It also shows how to enable HHVM for a specific site, allowing for flexible web server configurations. ```YAML sites: - map: homestead.app to: /home/vagrant/Code/Laravel/public hhvm: true ``` -------------------------------- ### Retrieve Uploaded File Path in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Explains how to get the absolute path to an uploaded file's temporary location using `getRealPath()`. ```PHP $path = Input::file('photo')->getRealPath(); ``` -------------------------------- ### Check if String Starts With (starts_with) Source: https://laravel.com/docs/4.2/docs/4.2/helpers The `starts_with` function determines if a given string (haystack) begins with a specified substring (needle). ```PHP $value = starts_with('This is my name', 'This'); ``` -------------------------------- ### Access Current Route in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/upgrade The method for accessing the current route has changed. Use `Route::current()` instead of `Route::getCurrentRoute()`. ```PHP // Before: Route::getCurrentRoute(); // After: Route::current(); ``` -------------------------------- ### Mock Laravel Facades for Testing with Mockery Source: https://laravel.com/docs/4.2/docs/4.2/testing This snippet illustrates how to mock a Laravel static facade, such as `Event`, using Mockery's `shouldReceive` method. This allows you to test interactions with facades without their actual execution, ensuring specific methods are called with expected arguments and controlling their return values. ```PHP public function testGetIndex() { Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle')); $this->call('GET', '/'); } ``` -------------------------------- ### Get All Error Messages for a Field in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/validation Illustrates how to retrieve and iterate through all validation error messages for a specific field. This is useful when a field might have multiple validation failures. ```PHP foreach ($messages->get('email') as $message) { // } ``` -------------------------------- ### DB Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel DB Facade, including its underlying class and IoC binding. ```APIDOC Facade: DB Class: Illuminate\Database\DatabaseManager IoC Binding: db ``` -------------------------------- ### Hash Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Hash Facade, including its underlying class and IoC binding. ```APIDOC Facade: Hash Class: Illuminate\Hashing\HasherInterface IoC Binding: hash ``` -------------------------------- ### Artisan Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Artisan Facade, including its underlying class and IoC binding. ```APIDOC Facade: Artisan Class: Illuminate\Console\Application IoC Binding: artisan ``` -------------------------------- ### Set Authenticated User in Laravel Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing Explains how to set a user as currently authenticated for the duration of a test using the `be` method in Laravel. ```PHP $user = new User(array('name' => 'John')); $this->be($user); ``` -------------------------------- ### Log In User by ID in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/security Authenticates a user directly using their user ID without requiring credentials, useful for programmatic login. ```PHP Auth::loginUsingId(1); ``` -------------------------------- ### Schema Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Schema Facade, including its underlying class and IoC binding. ```APIDOC Facade: Schema Class: Illuminate\Database\Schema\Blueprint IoC Binding: ``` -------------------------------- ### Get All Error Messages for All Fields in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/validation Demonstrates how to retrieve and iterate through all validation error messages across all fields. This is commonly used to display a comprehensive list of errors to the user. ```PHP foreach ($messages->all() as $message) { // } ``` -------------------------------- ### Get The Request URL in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/requests Retrieves the full URL for the current request using `Request::url()`. This includes the scheme, host, and path, but excludes the query string. ```PHP $url = Request::url(); ``` -------------------------------- ### Response Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Response Facade, including its underlying class and IoC binding. ```APIDOC Facade: Response Class: Illuminate\Support\Facades\Response IoC Binding: ``` -------------------------------- ### Get Class Basename (class_basename) Source: https://laravel.com/docs/4.2/docs/4.2/helpers The `class_basename` function extracts the class name from a fully qualified class name string, removing any namespace prefixes. ```PHP $class = class_basename('Foo\Bar\Baz'); // Baz ``` -------------------------------- ### Route Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Route Facade, including its underlying class and IoC binding. ```APIDOC Facade: Route Class: Illuminate\Routing\Router IoC Binding: router ``` -------------------------------- ### Running Laravel Database Seeders Source: https://laravel.com/docs/4.2/docs/4.2/migrations Explains how to execute database seeders using Artisan commands. It covers running the default `DatabaseSeeder` or a specific seeder class, and notes the option to run seeders after a `migrate:refresh` operation. ```php php artisan db:seed ``` ```php php artisan db:seed --class=UserTableSeeder ``` ```php php artisan migrate:refresh --seed ``` -------------------------------- ### Execute General SQL Statements with Laravel DB Facade Source: https://laravel.com/docs/4.2/docs/4.2/database This snippet demonstrates how to run any general SQL statement, such as DDL commands (e.g., `DROP TABLE`), using Laravel's `DB::statement` method. It takes a raw SQL string as input. ```PHP DB::statement('drop table users'); ``` -------------------------------- ### Session Facade Reference Source: https://laravel.com/docs/4.2/docs/4.2/facades Details for the Laravel Session Facade, including its underlying class and IoC binding. ```APIDOC Facade: Session Class: Illuminate\Session\SessionManager IoC Binding: session ``` -------------------------------- ### Add Homestead Domain to Local Hosts File Source: https://laravel.com/docs/4.2/docs/4.2/homestead This example shows the format for adding an entry to your local machine's `hosts` file. This entry redirects requests for your configured Homestead domain (e.g., `homestead.app`) to the IP address of your Homestead virtual machine, enabling local access. ```Hosts File 192.168.10.10 homestead.app ``` -------------------------------- ### Assert Old Input Data in Laravel Tests Source: https://laravel.com/docs/4.2/docs/4.2/testing Shows how to assert that old input data is present in the session after a request in Laravel tests. ```PHP public function testMethod() { $this->call('GET', '/'); $this->assertHasOldInput(); } ``` -------------------------------- ### Protect Route with 'auth' Filter in Laravel Source: https://laravel.com/docs/4.2/docs/4.2/security Applies the default 'auth' filter to a route, restricting access to only authenticated users. The filter is defined in `app/filters.php`. ```PHP Route::get('profile', array('before' => 'auth', function() { // Only authenticated users may enter... })); ``` -------------------------------- ### Execute SELECT Queries with Laravel DB Facade Source: https://laravel.com/docs/4.2/docs/4.2/database This snippet shows how to perform a SELECT query using Laravel's `DB::select` method. It demonstrates passing a raw SQL query with a placeholder and an array of bindings. The method will always return an array of results. ```PHP $results = DB::select('select * from users where id = ?', array(1)); ```