2. Service injection (ok)

Cái này chắc đã quá quen thuộc rồi, Laravel cho phép chúng ta lấy một service bất kỳ có trong service container thông qua @inject.

@inject('metrics', 'App\Services\MetricsService')

<div>
    Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>

Ví dụ đã hoàn thành

C:\xampp\htdocs\blog\resources\views\child.blade.php

resources\views\child.blade.php
@inject('metrics', 'App\User')
<div>
    Monthly Revenue: {{ $metrics->find(1) }}.
</div>

C:\xampp\htdocs\blog\routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
// Route::get('admin/product/{id}', 'ProductController@getPostDetail');
Route::get('/', function () {
  return view('pages/login');
});

Kết quả: http://blog.com/

Last updated