Build Deep high-quality website with Core Pro 5.1.8 and Theme 1.0.6
Thủ thuật CRUD code php nhanh chóng Laravel
CRUD Ý nghĩa: CRUD là một từ viết tắt xuất phát từ thế giới lập trình máy tính và đề cập đến bốn chức năng được coi là cần thiết để thực hiện một ứng dụng lưu trữ liên tục: tạo, đọc, cập nhật và xóa .
Ứng dụng hoạt động CRUD của Laravel 8; Trong hướng dẫn này, bạn sẽ học từng bước cách xây dựng ứng dụng hoạt động crud đơn giản trong laravel 8. Và cách xác thực thêm và cập nhật dữ liệu biểu mẫu ở phía máy chủ trong ứng dụng crud của laravel 8.
CRUD Ý nghĩa: CRUD là một từ viết tắt xuất phát từ thế giới lập trình máy tính và đề cập đến bốn chức năng được coi là cần thiết để thực hiện một ứng dụng lưu trữ liên tục: tạo, đọc, cập nhật và xóa .
Hướng dẫn từng bước hoạt động crud của laravel 8 này sẽ triển khai một ứng dụng vận hành crud đơn giản của công ty trong ứng dụng laravel 8 với xác thực. Sử dụng ứng dụng crud này, bạn có thể tìm hiểu cách chèn, đọc, cập nhật và xóa dữ liệu khỏi cơ sở dữ liệu trong laravel 8.
Hướng dẫn Laravel 8 CRUD bằng ví dụ
- Bước 1 - Tải xuống ứng dụng Laravel 8
- Bước 2 - Thiết lập cơ sở dữ liệu với ứng dụng
- Bước 3 - Tạo mô hình công ty và di chuyển cho ứng dụng CRUD
- Bước 4 - Tạo các tuyến đường
- Bước 5 - Tạo Bộ điều khiển CRUD của Công ty theo Lệnh của Nghệ nhân
- Bước 6 - Tạo tệp Blade Views
- Đặt tên thư mục cho công ty
- index.blade.php
- create.blade.php
- edit.blade.php
- Bước 7 - Chạy ứng dụng Laravel CRUD trên Máy chủ phát triển
Bước 1 - Tải xuống ứng dụng Laravel 8
Trước hết, tải xuống hoặc cài đặt thiết lập mới laravel 8. Vì vậy, hãy mở terminal và nhập lệnh sau để cài đặt ứng dụng laravel 8 mới vào máy của bạn:
composer create-project --prefer-dist laravel/laravel LaravelCRUD
Bước 2 - Thiết lập cơ sở dữ liệu với ứng dụng
Thiết lập cơ sở dữ liệu với ứng dụng laravel 8 đã tải xuống / cài đặt của bạn. Vì vậy, bạn cần tìm tệp .env và thiết lập chi tiết cơ sở dữ liệu như sau:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password
Bước 3 - Tạo mô hình công ty và di chuyển cho ứng dụng CRUD
Mở lại dấu nhắc lệnh của bạn. Và chạy lệnh sau trên đó. Để tạo mô hình và tệp di chuyển cho biểu mẫu:
php artisan make:model Company -m
Sau đó, mở tệp create_companies_table.php bên trong thư mục LaravelCRUD / database / migrations / . Và cập nhật hàm up () với mã sau:
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('address');
$table->timestamps();
});
}
Sau đó, mở lại dấu nhắc lệnh và chạy lệnh sau để tạo bảng vào cơ sở dữ liệu:
php artisan migrate
Bước 4 - Tạo các tuyến đường
Sau đó, tạo các tuyến đường cho ứng dụng crud laravel. Vì vậy, hãy mở tệp web.php từ thư mục tuyến đường của ứng dụng laravel CRUD. Và cập nhật các tuyến sau vào tệp web.php:
use App\Http\Controllers\CompanyCRUDController;
Route::resource('companies', CompanyCRUDController::class);
Bước 5 - Tạo Bộ điều khiển CRUD của Công ty theo Lệnh của Nghệ nhân
Tạo bộ điều khiển bằng cách sử dụng lệnh sau trên dấu nhắc lệnh để tạo tệp bộ điều khiển:
php artisan make:controller CompanyCRUDController
Sau đó, truy cập vào app / Http / controllers và mở tệp CompanyCRUDController.php. Và cập nhật mã sau vào đó:
<?php
namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request;
class CompanyCRUDController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data['companies'] = Company::orderBy('id','desc')->paginate(5);
return view('companies.index', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('companies.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'address' => 'required'
]);
$company = new Company;
$company->name = $request->name;
$company->email = $request->email;
$company->address = $request->address;
$company->save();
return redirect()->route('companies.index')
->with('success','Company has been created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\company $company
* @return \Illuminate\Http\Response
*/
public function show(Company $company)
{
return view('companies.show',compact('company'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Company $company
* @return \Illuminate\Http\Response
*/
public function edit(Company $company)
{
return view('companies.edit',compact('company'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\company $company
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'address' => 'required',
]);
$company = Company::find($id);
$company->name = $request->name;
$company->email = $request->email;
$company->address = $request->address;
$company->save();
return redirect()->route('companies.index')
->with('success','Company Has Been updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Company $company
* @return \Illuminate\Http\Response
*/
public function destroy(Company $company)
{
$company->delete();
return redirect()->route('companies.index')
->with('success','Company has been deleted successfully');
}
}
Bước 6 - Tạo tệp Blade Views
Tạo thư mục và một số dạng xem phiến, xem phần sau:
- Đặt tên thư mục cho công ty
- index.blade.php
- create.blade.php
- edit.blade.php
Tạo thư mục tên công ty bên trong thư mục tài nguyên / chế độ xem .
Lưu ý rằng, hãy tạo index.blade.php, create.blade.php và edit.blade bên trong thư mục công ty. Và cập nhật mã sau vào các tệp sau:
index.blade.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 8 CRUD Tutorial From Scratch</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example Tutorial</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" href=""> Create Company</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p></p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>S.No</th>
<th>Company Name</th>
<th>Company Email</th>
<th>Company Address</th>
<th width="280px">Action</th>
</tr>
@foreach ($companies as $company)
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<form action="" method="Post">
<a class="btn btn-primary" href="">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $companies->links() !!}
</body>
</html>
create.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Company Form - Laravel 8 CRUD</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>Add Company</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href=""> Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
</div>
@endif
<form action="" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Company Name">
@error('name')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Company Email">
@error('email')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Address:</strong>
<input type="text" name="address" class="form-control" placeholder="Company Address">
@error('address')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
</body>
</html>
edit.blade.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Company Form - Laravel 8 CRUD Tutorial</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Company</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="" enctype="multipart/form-data"> Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
</div>
@endif
<form action="" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Name:</strong>
<input type="text" name="name" value="" class="form-control" placeholder="Company name">
@error('name')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Company Email" value="">
@error('email')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Address:</strong>
<input type="text" name="address" value="" class="form-control" placeholder="Company Address">
@error('address')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
</div>
</body>
</html>
Nếu bạn gửi biểu mẫu thêm hoặc chỉnh sửa trống. Vì vậy, thông báo lỗi sẽ được hiển thị với sự trợ giúp của mã dưới đây:
@error('name')
<div class="alert alert-danger mt-1 mb-1"></div>
@enderror
Bước 7 - Chạy Máy chủ Phát triển
Bước cuối cùng, mở dấu nhắc lệnh và chạy lệnh sau để khởi động máy chủ phát triển:
php artisan serve
Sau đó, mở trình duyệt của bạn và nhấn vào url sau trên đó:
http://127.0.0.1:8000/companies
Ứng dụng crud của công ty laravel 8 sẽ giống như những hình ảnh sau:
Nguồn: https://www.tutsmake.com
0 Nhận xét
Hãy trở thành người đầu tiên viết chia sẽ cảm nghĩ của mình bên dưới nhé!
Thêm Bình luận