😆Request đặc biệt, tự làm Request khác với mặc định (ok)
https://github.com/balanchi/Simple-Module-Laravel-Project/tree/master
Đã có lần sử dụng ở dự án nào đấy không rõ, được lấy một phần nhỏ trong dự án trên github
C:\xampp82\htdocs\lva4\Modules\Category\Http\Controllers\CategoryController.php
public function store(CreateCategoryRequest $request)
{
$inputs = $request->all();
Category::create($inputs);
return to_route('category.index');
}
C:\xampp82\htdocs\lva4\Modules\Category\Database\Migrations\2023_10_23_043505_create_categories_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
};
C:\xampp82\htdocs\lva4\Modules\Category\Http\Requests\CreateCategoryRequest.php
<?php
namespace Modules\Category\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateCategoryRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:120|min:2|regex:/^[ا-یa-zA-Z0-9\-۰-۹ء-ي., ]+$/u',
'description' => 'required|max:1200|min:2|regex:/^[ا-یa-zA-Z0-9\-۰-۹ء-ي., ]+$/u',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
PreviousKHÔNG DÙNG ELOQUENT THÌ DÙNG LARAVEL LÀM GÌ?NextNhững mẹo hay và ngắn chúng ta thường không nhớ sử dụng.
Last updated
Was this helpful?