Hướng dẫn dynamically load content in bootstrap modal with ajax laravel - tải động nội dung trong phương thức bootstrap với ajax laravel

"; } $response['html'] = $html; return response()->json($response); } }

4. Bộ điều khiểnRoute

  • Tạo bộ điều khiển
    php artisan make:migration create_employees_table
    9.
  • Nhập mô hình
    php artisan make:migration create_employees_table
    6.
    • Tạo 2 phương pháp - Load
      public function up()
      {
          Schema::create('employees', function (Blueprint $table) {
             $table->bigIncrements('id');
             $table->string('username');
             $table->string('name');
             $table->string('email');
             $table->smallInteger('age');
             $table->timestamps();
          });
      }
      3 view.
    • Index () - Tìm nạp tất cả các bản ghi từ bảng
      php artisan make:migration create_employees_table
      2 và gán cho
      public function up()
      {
          Schema::create('employees', function (Blueprint $table) {
             $table->bigIncrements('id');
             $table->string('username');
             $table->string('name');
             $table->string('email');
             $table->smallInteger('age');
             $table->timestamps();
          });
      }
      2. Tải
      public function up()
      {
          Schema::create('employees', function (Blueprint $table) {
             $table->bigIncrements('id');
             $table->string('username');
             $table->string('name');
             $table->string('email');
             $table->smallInteger('age');
             $table->timestamps();
          });
      }
      3 Xem và vượt qua
      public function up()
      {
          Schema::create('employees', function (Blueprint $table) {
             $table->bigIncrements('id');
             $table->string('username');
             $table->string('name');
             $table->string('email');
             $table->smallInteger('age');
             $table->timestamps();
          });
      }
      4.
      This is GET type route for AJAX request.
name('getEmployeeDetails');

GetEmployedEtails () - Phương thức này lấy ID nhân viên làm A & NBSP; tham số. Lấy bản ghi bằng public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('username'); $table->string('name'); $table->string('email'); $table->smallInteger('age'); $table->timestamps(); }); }5 từ bảng php artisan make:migration create_employees_table2 và tạo bố cục. Gán public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('username'); $table->string('name'); $table->string('email'); $table->smallInteger('age'); $table->timestamps(); }); }7 cho public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('username'); $table->string('name'); $table->string('email'); $table->smallInteger('age'); $table->timestamps(); }); }8.View

Trả lại

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
9 ở định dạng JSON.

5. Tuyến đường

Mở tệp

php artisan migrate
0.





Xác định 2 tuyến đường -

/ - Tải

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
3 Xem.

/getemployedeedetails - Đây là cách loại tuyến cho yêu cầu AJAX.

  • 6. Xem
  • Tạo tệp
    php artisan migrate
    2 trong
    php artisan migrate
    3.
  • HTML
  • Bao gồm thư viện bootstrap và jQuery.
  • Tạo phương thức
    php artisan migrate
    4 trong đó hiển thị thông tin nhân viên bằng cách sử dụng jQuery ajax.

Đã hoàn thành mã

php artisan make:migration create_employees_table
0

4. Bộ điều khiểnDemo

Tạo bộ điều khiển

php artisan make:migration create_employees_table
9.


Nhập mô hình php artisan make:migration create_employees_table6.Conclusion

Tạo 2 phương pháp -

Index () - Tìm nạp tất cả các bản ghi từ bảng

php artisan make:migration create_employees_table
2 và gán cho
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
2. Tải
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
3 Xem và vượt qua
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
4.

Bootstrap Modal là một hộp bật lên sử dụng để hiển thị thông tin và hình thức nhập.

Bạn cũng có thể cập nhật nội dung của nó một cách linh hoạt mỗi lần khi mở bằng AJAX.

Trong hướng dẫn này, tôi chỉ ra cách bạn có thể tải nội dung trên phương thức Bootstrap một cách động bằng cách sử dụng JQuery Ajax trong Laravel 9.

Hướng dẫn dynamically load content in bootstrap modal with ajax laravel - tải động nội dung trong phương thức bootstrap với ajax laravel


Nội dung

  1. Cấu hình cơ sở dữ liệu
  2. Cấu trúc bảng
  3. Người mẫu
  4. Người điều khiển
  5. Tuyến đường
  6. Lượt xem
  7. Thử nghiệm
  8. Sự kết luận

1. Cấu hình cơ sở dữ liệuDatabase Configuration

Mở tệp

php artisan make:migration create_employees_table
1.

Chỉ định máy chủ, tên cơ sở dữ liệu, tên người dùng và mật khẩu.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=

2. Cấu trúc bảngTable structure

  • Tạo một bảng mới
    php artisan make:migration create_employees_table
    2 bằng cách sử dụng di chuyển.
php artisan make:migration create_employees_table
  • Bây giờ, điều hướng đến thư mục
    php artisan make:migration create_employees_table
    3 từ root dự án.
  • Tìm một tệp PHP kết thúc bằng
    php artisan make:migration create_employees_table
    4 và mở nó.
  • Xác định cấu trúc bảng trong phương pháp
    php artisan make:migration create_employees_table
    5.
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
  • Chạy di chuyển -
php artisan migrate
  • Bảng được tạo ra và tôi đã thêm một số hồ sơ vào nó.

3. Mô hìnhModel

  • Tạo mô hình
    php artisan make:migration create_employees_table
    6.
php artisan make:model Employees
  • Mở tệp
    php artisan make:migration create_employees_table
    7.
  • Chỉ định các thuộc tính mô hình có thể gán hàng loạt - tên người dùng, tên, email và tuổi bằng thuộc tính
    php artisan make:migration create_employees_table
    8.

Đã hoàn thành mã


4. Bộ điều khiểnController

  • Tạo bộ điều khiển
    php artisan make:migration create_employees_table
    9.
php artisan make:controller EmployeesController
  • Nhập mô hình
    php artisan make:migration create_employees_table
    6.

Tạo 2 phương pháp -

  • Index () - Tìm nạp tất cả các bản ghi từ bảng
    php artisan make:migration create_employees_table
    2 và gán cho
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    2. Tải
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    3 Xem và vượt qua
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    4.
    Fetch all records from
    php artisan make:migration create_employees_table
    2 table and assign to
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    2. Load
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    3 view and pass
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    4.
  • GetEmployedEtails () - Phương thức này lấy ID nhân viên làm A & NBSP; tham số. Lấy bản ghi bằng
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    5 từ bảng
    php artisan make:migration create_employees_table
    2 và tạo bố cục. Gán
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    7 cho
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    8.
    This method takes employee id as a  parameter. Fetch a record by
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    5 from the
    php artisan make:migration create_employees_table
    2 table and create a layout. Assign
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    7 to
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('username');
           $table->string('name');
           $table->string('email');
           $table->smallInteger('age');
           $table->timestamps();
        });
    }
    8.

Trả lại

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('username');
       $table->string('name');
       $table->string('email');
       $table->smallInteger('age');
       $table->timestamps();
    });
}
9 ở định dạng JSON.

Đã hoàn thành mã

get();
      return view('index',$data);
   }

   public function getEmployeeDetails($empid = 0){

      $employee = Employees::find($empid);

      $html = "";
      if(!empty($employee)){
         $html = "
ID: ".$employee->id."
Username: ".$employee->username."
Name: ".$employee->name."
Email: ".$employee->email."
Age: ".$employee->age."