Hướng dẫn css laravel 8 - css laravel 8

Chào mọi người, do mọi người rất ủng hộ với series học laravel của mình mà bản đó thì mình viết từ version 5.3 đến giờ thì laravel cũng đã update rất nhiều rồi có những thứ đã bị outdate. Nên mình sẽ viết thêm series mới và hướng dẫn mọi người trên nền Laravel 8 luôn. Laravel 8 luôn.

Bài Viết Mới

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Những tính năng mới trong PHP 8.1

Hôm qua (26/11/2021), PHP 8.1 đã được release với một loạt các tính năng mới như: enum, read only roperty,...

Vũ Thanh Tài 7 comments 7 comments

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Những tính năng mới trong PHP 8.1

Hôm qua (26/11/2021), PHP 8.1 đã được release với một loạt các tính năng mới như: enum, read only roperty,...

Vũ Thanh Tài 7 comments 1 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Những tính năng mới trong PHP 8.1

Hôm qua (26/11/2021), PHP 8.1 đã được release với một loạt các tính năng mới như: enum, read only roperty,...

Vũ Thanh Tài 7 comments 0 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Những tính năng mới trong PHP 8.1

Hôm qua (26/11/2021), PHP 8.1 đã được release với một loạt các tính năng mới như: enum, read only roperty,...

Vũ Thanh Tài 7 comments 0 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Những tính năng mới trong PHP 8.1

Hôm qua (26/11/2021), PHP 8.1 đã được release với một loạt các tính năng mới như: enum, read only roperty,...

Vũ Thanh Tài 7 comments 0 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Bài 39: Cache trong Laravel 8

Trong dự án của các bạn chắc hẳn sẽ có các service, action nào đó hoạt động tốn rất nhiều...

Vũ Thanh Tài 7 comments 1 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Bài 39: Cache trong Laravel 8

Trong dự án của các bạn chắc hẳn sẽ có các service, action nào đó hoạt động tốn rất nhiều...

Vũ Thanh Tài 1 comment 2 comments

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Bài 38: Eloquent ORM Serialize trong Laravel 8

Khi các bạn sử dụng Laravel Eloquent để xây dựng API cho ứng dụng, thì đôi lúc các bạn sẽ...

Vũ Thanh Tài 7 comments 0 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Bài 39: Cache trong Laravel 8

Trong dự án của các bạn chắc hẳn sẽ có các service, action nào đó hoạt động tốn rất nhiều...

Vũ Thanh Tài 7 comments 0 comment

Hướng dẫn css laravel 8 - css laravel 8

# LARAVEL 8

Bài 39: Cache trong Laravel 8

Trong dự án của các bạn chắc hẳn sẽ có các service, action nào đó hoạt động tốn rất nhiều...

Vũ Thanh Tài 7 comments 0 comment

  • 1
  • 2
  • 3
  • 4
  • Bài 39: Cache trong Laravel 8

Đây là chuỗi bài viết theo phong thái dễ hiểu, đơn giản, cơ bản, phù hợp với những người bắt đầu với Laravel từ con số 0.

Laravel Views

Phần này sẽ bao gồm các nội dung sau:

  • Layout page
  • Asset File
  • Lấy dữ liệu từ database để hiển thị

Layout page

Asset FileDRY - Don't Repeat Yourself. Để có thể khắc phục được điều này ta sẽ tách phần chung đó ra một file riêng. Trong file này sẽ gọi nội dung riêng biệt của các file views còn lại và in ra. File này thường đặt tên là

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
2. Để dễ hiểu hơn hãy đi vào thực tế nhé.

Lấy dữ liệu từ database để hiển thị

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>
        ...
        
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            @yield('content')
        </div>
    </body>
</html>

Một trang web sẽ có rất nhiều view hay có nhiều màn hình giao diện khác nhau. Nhưng nếu một số hoặc toàn bộ các màn hình đó có điểm chung hay những đoạn code HTML chung thì việc mỗi file đều có nội dung này sẽ rất dư thừa, và vi phạm nguyên tắc DRY - Don't Repeat Yourself. Để có thể khắc phục được điều này ta sẽ tách phần chung đó ra một file riêng. Trong file này sẽ gọi nội dung riêng biệt của các file views còn lại và in ra. File này thường đặt tên là

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
2. Để dễ hiểu hơn hãy đi vào thực tế nhé.

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection

File

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
3 thực tế có phần
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
4 rất dài, và nội dung riêng biệt thường ở trong phần
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
5. Chúng ta sẽ copy toàn bộ trừ phần nội dung riêng biệt trong body sang file
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
6. Có thể tạo file mới bằng câu lệnh sau trên terminal
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
7. Trong phần nội dung thẻ
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
5 chúng ta thêm dòng code
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
9. Nội dung file layout view sẽ như sau.

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection

File welcome view ta cũng sẽ chỉnh tương ứng như sau:

Asset File

Lấy dữ liệu từ database để hiển thị

Một trang web sẽ có rất nhiều view hay có nhiều màn hình giao diện khác nhau. Nhưng nếu một số hoặc toàn bộ các màn hình đó có điểm chung hay những đoạn code HTML chung thì việc mỗi file đều có nội dung này sẽ rất dư thừa, và vi phạm nguyên tắc DRY - Don't Repeat Yourself. Để có thể khắc phục được điều này ta sẽ tách phần chung đó ra một file riêng. Trong file này sẽ gọi nội dung riêng biệt của các file views còn lại và in ra. File này thường đặt tên là

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
2. Để dễ hiểu hơn hãy đi vào thực tế nhé. Cho các file css, js tương ứng vào các thư mục
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
4 và
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
5. Sau đó có thể áp dụng vào view như sau.

<link href="css/*.css" rel="stylesheet" type="text/css" media="all">

File

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
3 thực tế có phần
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
4 rất dài, và nội dung riêng biệt thường ở trong phần
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
5. Chúng ta sẽ copy toàn bộ trừ phần nội dung riêng biệt trong body sang file
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
6. Có thể tạo file mới bằng câu lệnh sau trên terminal
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
7. Trong phần nội dung thẻ
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
5 chúng ta thêm dòng code
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
9. Nội dung file layout view sẽ như sau.

File welcome view ta cũng sẽ chỉnh tương ứng như sau:

Với file info view ta cũng sẽ làm tương tự. Cho các file sass, js tương ứng vào các thư mục

<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
6 và
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
7. Sau đó require file đó vào các file:

@import './default.css';
@import './fonts.css';
require('./bootstrap');

Lúc này dòng code trong file

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
6 là
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
9 sẽ tìm tất cả section có tên là content trong các file mà
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
2. Chúng ta có thể định nghĩa nhiều section trong file và gọi ra ở file
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
3 một cách tùy thích.

Trong các project web sẽ luôn có các file css, js dành cho frontend. Có 2 cách để implement các file này trong laravel.

{
   ...
   "devDependencies": {
       "axios": "^0.19",
       "cross-env": "^7.0",
       "laravel-mix": "^5.0.1",
       "lodash": "^4.17.19",
       "resolve-url-loader": "^3.1.0",
       "sass": "^1.15.2",
       "sass-loader": "^8.0.0",
       "vue-template-compiler": "^2.6.11"
   }
}

Cách 1 Cho các file css, js tương ứng vào các thư mục

<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
4 và
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
5. Sau đó có thể áp dụng vào view như sau.

<link href="{{ mix('css/app.css') }}" rel="stylesheet" media="all">

Thư mục public đúng như tên gọi sẽ public những file bên trong, người dùng có thể xem thông qua F12.

File welcome view ta cũng sẽ chỉnh tương ứng như sau:

Với file info view ta cũng sẽ làm tương tự.

Lúc này dòng code trong file

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
6 là
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
9 sẽ tìm tất cả section có tên là content trong các file mà
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
2. Chúng ta có thể định nghĩa nhiều section trong file và gọi ra ở file
<link href="css/*.css" rel="stylesheet" type="text/css" media="all">
3 một cách tùy thích.

# Lệnh này sẽ đồng thời tạo ra file migration
php artisan make:model Article -m
    ...
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            # Tên bài báo
            $table->string('title');
            # Tóm tắt sơ qua bài báo
            $table->text('excerpt');
            # Nội dung bài báo
            $table->text('body');
            $table->timestamps();
        });
    }
    ...

Sau đó bạn có thể tạo 4 bản ghi tùy ý bằng cách sử dụng

@import './default.css';
@import './fonts.css';
7 như sau.

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
0

Hoặc là sử dụng laravel seed. Tham khảo tại đây.

Hãy thử test việc lấy dữ liệu ra có thành công hay không bằng cách viết trong

@import './default.css';
@import './fonts.css';
8 như như sau.

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
1

Ngoài ra còn có nhiều cách truy vấn dữ liệu trong laravel, tham khảo tại đây. Sau khi làm quen với một số cách, ta lựa chọn cách phù hợp với trường hợp này.

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
2

Sau đó, ở file about view, ta viết lại như sau để in ra dữ liệu.

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
3

Cú pháp laravel

@import './default.css';
@import './fonts.css';
9 sẽ tương đương với
require('./bootstrap');
0 mà larvavel sẽ giúp chúng ta thông dịch điều này, điều này sẽ giúp chúng ta code đơn giản hơn.
require('./bootstrap');
1 cũng là cú pháp của laravel để chúng ta nhúng đoạn code laravel trong file php.

Bây giờ chúng ta sẽ tạo ra view cho từng bài article, lần này sẽ để xử lý ở controller, cho đúng với flow của laravel, hay mô hình MVC hơn nhé.

Khai báo và sửa các file code sau:

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
4
#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
5
#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
6

Sửa lại file about view:

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
7

Cuối cùng, hãy chuyển đến làm trang hiển thị toàn bộ bài báo. Ta thêm method index trong

require('./bootstrap');
2

#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
8
#Cho ta biết file này là nội dung thêm của file `layout.blade.php`.
@extends('layout')

#Nội dung riêng biệt được đặt trong cặp @section('tên section')~@endsection
@section('content')
    <div class="flex-center position-ref full-height">
       ...

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="links">
                <a href="https://laravel.com/docs">Docs</a>
                <a href="https://laracasts.com">Laracasts</a>
                <a href="https://laravel-news.com">News</a>
                <a href="https://blog.laravel.com">Blog</a>
                <a href="https://nova.laravel.com">Nova</a>
                <a href="https://forge.laravel.com">Forge</a>
                <a href="https://vapor.laravel.com">Vapor</a>
                <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
        </div>
    </div>
@endsection
9
@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
0

Trên đây là những bài thực hành rất linh động để hiểu về cách lấy dữ liệu từ trong DB để hiển thị thực tế trên view.

Ngoài ra, để hiển thị đẹp hơn ở thanh điều hướng: "Khi bấm ở các thanh điều hướng thì box oval xanh sẽ chuyển sự hiển thị theo". Ta có thể sửa file code dưới như sau:

Hướng dẫn css laravel 8 - css laravel 8
Ta có thể sửa file code dưới như sau:

@extends('layout')

@section('content')
    {{ $detail->body }}
    Hello
@endsection
1

Phần này xin được tạm kết tại đây! Nguồn tham khảo: https://laracasts.com/series/laravel-6-from-scratch/