The basic commands:
- See the php version
1 2 | php -v |
- See the php path
1 2 | which php |
After this command, see the path of the format: /usr/bin/php
, this type is used by default php has been installed on the machine. change the default by going to ~/.bash_profile
edit the path like this: export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
After editing is complete, call source ~/.bash_prpfile
to apply the changes.
Debug:
Debug log in each view
1 2 | tail -f storage/logs/laravel.log & php artisan serve |
Debug sql query:
1 2 3 | http://localhost/phpmyadmin/server_variables.php -> Enable genrate log -> ON |
Eloquent ORM in Laravel
- Each table in the database corresponds to a model.
- Model allows query, insert, update table in db.
The command to create a model:
1 2 3 4 5 6 7 | php artisan make:model Flight // muon tao db luon php artisan make:model Flight --migration php artisan make:model Flight -m |
Facade:
What?
- Facade provides a static interface for classes available in the service container.
- Larvel provides most complete facades for working with Laravel features.
eg:
1 2 3 4 5 6 | use IlluminateSupportFacadesCache; Route::get('/cache', function () { return Cache::get('key'); }); |
When to use?
- There are many benefits such as: short, easy to remember to use all the features of Laravel without having to remember all the classes with very long names.
- Use for easy creation in testing. (interface type)
Facade & Dependence Injection:
- facades use dynamic methods to proxy method calls to objects to resolve from the service container, we actually can test facades just as we would test an injected class instance.
ex:
1 2 3 4 5 6 | use IlluminateSupportFacadesCache; Route::get('/cache', function () { return Cache::get('key'); }); |
Write the test to verify Cache :: get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | use IlluminateSupportFacadesCache; /** * A basic functional test example. * * @return void */ public function testBasicExample() { Cache::shouldReceive('get') ->with('key') ->andReturn('value'); $this->visit('/cache') ->see('value'); } |
Facade work ntn?
- The Facade base class makes use of the
__callStatic()
magic-method to defer calls from your facade to an object resolved from the container - eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace AppHttpControllers; use AppHttpControllersController; use IlluminateSupportFacadesCache; class UserController extends Controller { /** * Show the profile for the given user. * * @param int $id * @return Response */ public function showProfile($id) { $user = Cache::get('user:'.$id); return view('profile', ['user' => $user]); } } |
Blade Templates in Laravel:
What?
Blade is the simple yet powerful templating tool provided with Laravel. Unlike other php engine templates, Blade does not restrict the use of PHP code in the view. Blade snippets are compiled into plain PHP code and cached until it is changed. Blade view uses: .blade.php
located in the resources/views
directory
Template Inheritance:
Create a layout
2 Important purposes while using blade: inheriting template & sections. See eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <span class="token operator"><</span> <span class="token operator">!</span> <span class="token operator">--</span> Stored in resources <span class="token operator">/</span> views <span class="token operator">/</span> layouts <span class="token operator">/</span> app <span class="token punctuation">.</span> blade <span class="token punctuation">.</span> php <span class="token operator">--</span> <span class="token operator">></span> <span class="token operator"><</span> html <span class="token operator">></span> <span class="token operator"><</span> head <span class="token operator">></span> <span class="token operator"><</span> title <span class="token operator">></span> App Name <span class="token operator">-</span> @ <span class="token keyword">yield</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'title'</span> <span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token operator">/</span> title <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> head <span class="token operator">></span> <span class="token operator"><</span> body <span class="token operator">></span> @ <span class="token function">section</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'sidebar'</span> <span class="token punctuation">)</span> This is the master sidebar <span class="token punctuation">.</span> @show <span class="token operator"><</span> div <span class="token keyword">class</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"container"</span> <span class="token operator">></span> @ <span class="token keyword">yield</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'content'</span> <span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token operator">/</span> div <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> body <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> html <span class="token operator">></span> |
In another template will declare a section content
, the content will be drawn immediately yield
section : define a content yield : used to display the content of the section.
Expand for 1 layout
When creating a child view, use Blade @extends
to specify the layout that the child view will inherit. eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- Stored in resources/views/child.blade.php --> @extends('layouts.app') @section('title', 'Page Title') @section('sidebar') @parent <p>This is appended to the master sidebar.</p> @endsection @section('content') <p>This is my body content.</p> @endsection |
@parent:
to replace the content in the parent view.
Components & Slots
1 2 3 4 5 6 | <!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> {{ $slot }} </div> |
Create a component to display in {{$ slot}}
1 2 3 4 | @component('alert') <strong>Whoops!</strong> Something went wrong! @endcomponent |
multiple slots for a component
1 2 3 4 5 6 7 8 | <!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> <div class="alert-title">{{ $title }}</div> {{ $slot }} </div> |
Define component
1 2 3 4 5 6 7 8 | @component('alert') @slot('title') Forbidden @endslot You are not allowed to access this resource! @endcomponent |
Note Blade:
{{ }}
: Blade sends data through PHP via a function: htmlspecialchars.
{!! $name !!}
: Sent without data escaped.
@if
@elseif
.. condition
1 2 3 4 | @unless (Auth::check()) You are not signed in. @endunless |
1 2 3 4 5 6 7 8 | @isset($records) // $records is defined and is not null... @endisset @empty($records) // $records is "empty"... @endempty |
Include sub-view:
1 2 3 4 5 6 7 8 | <div> @include('shared.errors') <form> <!-- Form Contents --> </form> </div> |
Render View for Collections:
1 2 | @each('view.name', $jobs, 'job') |