Cách bình luận trong Laravel

Bây giờ, hãy xem hướng dẫn ví dụ về hệ thống bình luận 9 trong laravel. Chúng ta sẽ sử dụng Laravel 9 Nested Comment and Reply System Tutorial. Chúng tôi sẽ sử dụng Build a Laravel 9 Live Commenting System. nếu bạn muốn xem ví dụ về Cách tạo lồng bình luận trong Laravel 9? . Vì vậy, hãy làm theo vài bước để tạo ví dụ về Tạo hệ thống nhận xét lồng nhau đa cấp trong Laravel 9

Ở đây, tôi sẽ cung cấp cho bạn một ví dụ đơn giản và dễ hiểu về cách sử dụng triển khai hệ thống nhận xét trong laravel 8 trong laravel, chỉ cần làm theo tất cả các bước của tôi

Vì vậy, trong ví dụ này, tôi sẽ tạo hai bảng cho một bảng là bài đăng và một bảng khác là nhận xét bằng cách sử dụng lệnh di chuyển

Hãy bắt đầu ví dụ sau

Đăng trang chi tiết

Đăng Trang

Bước 1. Tải xuống Laravel

Trước hết, chúng ta hãy bắt đầu hướng dẫn bằng cách cài đặt một ứng dụng laravel mới. nếu bạn đã tạo dự án rồi thì bỏ qua bước sau

composer create-project laravel/laravel example-app

Bước 2. Cập nhật cấu hình cơ sở dữ liệu

Trong bước thứ hai, chúng ta sẽ tạo cấu hình cơ sở dữ liệu ví dụ tên cơ sở dữ liệu, tên người dùng, mật khẩu, v.v. cho ứng dụng laravel 5 thô sơ của chúng ta. 7. Vì vậy, hãy mở. env và điền vào tất cả các chi tiết như dưới đây

env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Bước 3. Tạo bài đăng và bảng bình luận

chúng tôi sẽ tạo hệ thống bình luận từ đầu. vì vậy chúng tôi phải tạo di chuyển cho bảng "bài đăng" và "bình luận" bằng Laravel 5. 7 lệnh nghệ nhân php, vì vậy trước tiên hãy kích hoạt lệnh dưới đây

php artisan make:migration create_posts_comments_table

Sau lệnh này, bạn sẽ tìm thấy một tệp trong đường dẫn sau "cơ sở dữ liệu/di chuyển" và bạn phải đặt mã dưới đây vào tệp di chuyển của mình để tạo bảng

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreatePostsCommentsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('body');

$table->timestamps();

$table->softDeletes();

});

Schema::create('comments', function (Blueprint $table) {

$table->increments('id');

$table->integer('user_id')->unsigned();

$table->integer('post_id')->unsigned();

$table->integer('parent_id')->unsigned()->nullable();

$table->text('body');

$table->timestamps();

$table->softDeletes();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

Schema::dropIfExists('comments');

}

}

Bây giờ bạn phải chạy quá trình di chuyển này bằng lệnh sau

php artisan migrate

Bước 4. Tạo xác thực

trong bước này, chúng ta cần tạo giàn giáo auth cho laravel bằng lệnh auth. laravel cung cấp xác thực người dùng mặc định. vì vậy chỉ cần chạy lệnh dưới đây

php artisan make:auth

Bước 5. Tạo mô hình

Ở bước này, chúng ta cần tạo model Post và Comment cho từng bảng. chúng ta cũng cần tạo mã cho mối quan hệ laravel cho nhận xét, trả lời, người dùng. Vì vậy, tạo cả hai mô hình như dưới đây

Chạy lệnh dưới đây để tạo mô hình Post

php artisan make:model Post

ứng dụng/bài đăng. php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model

{

use SoftDeletes;

protected $dates = ['deleted_at'];

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = ['title', 'body'];

/**

* The has Many Relationship

*

* @var array

*/

public function comments()

{

return $this->hasMany(Comment::class)->whereNull('parent_id');

}

}

Chạy lệnh dưới đây để tạo mô hình Nhận xét

________số 8

ứng dụng/Nhận xét. php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model

{

use SoftDeletes;

protected $dates = ['deleted_at'];

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = ['user_id', 'post_id', 'parent_id', 'body'];

/**

* The belongs to Relationship

*

* @var array

*/

public function user()

{

return $this->belongsTo(User::class);

}

/**

* The has Many Relationship

*

* @var array

*/

public function replies()

{

return $this->hasMany(Comment::class, 'parent_id');

}

}

Bước 6. Tạo bộ điều khiển

Trong bước này, bây giờ chúng ta sẽ tạo bộ điều khiển mới là PostController và CommentController. Vì vậy, hãy chạy lệnh dưới đây và tạo bộ điều khiển mới. bộ điều khiển dưới đây để tạo bộ điều khiển tài nguyên

Tạo Post Controller bằng lệnh dưới đây

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

0

ứng dụng/Http/Bộ điều khiển

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

1

Tạo Post Controller bằng lệnh dưới đây

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

2

ứng dụng/Http/CommentController

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

3

Bước 7. Tạo tập tin Blade

Ở bước cuối cùng. Trong bước này, chúng ta phải tạo các tệp lưỡi cắt. Vì vậy, chủ yếu chúng ta phải tạo tệp bố cục và sau đó tạo thư mục mới "bài đăng" sau đó tạo tệp phiến cho hệ thống bình luận. Vì vậy, cuối cùng bạn phải tạo tập tin lưỡi dưới đây

1) chỉ mục. lưỡi. php

2) hiển thị. lưỡi. php

3) tạo. lưỡi. php

4) bình luậnHiển thị. lưỡi. php

tài nguyên/lượt xem/bài đăng/chỉ mục. lưỡi. php

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

4

tài nguyên/lượt xem/bài đăng/hiển thị. lưỡi. php

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

5

tài nguyên/lượt xem/bài đăng/tạo. lưỡi. php

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

6

tài nguyên/lượt xem/bài đăng/bình luận Hiển thị. lưỡi. php

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

7

Chạy ứng dụng Laravel

Bây giờ chúng ta đã sẵn sàng để chạy ví dụ ứng dụng hệ thống bình luận với laravel 5. 7 nên chạy lệnh dưới đây để chạy nhanh