kendim için örnek bir kod örneği belirtiyorum. okuyarak anlam çıkartamayabilirsiniz. adım adım gidiyorum.
laravel 11 kurulumu yaptık.
php artisan make:model TaskModel
make:request StoreTaskRequest
make:request UpdateTaskRequest
make:seed TaskSeed
make:factory TaskFactory
make:task Migration
diye bir çok değer oluşturduk. ben tabi şu request, seed, factory, migration gibi şeylere karşı işi biraz daha uzatıyor diye karşıyım ama 3-5 kişi olunca projede mecbur aslında.
laravel 10dan sonra make:model yazdıktan sonra artık isim yazmana gerek yok, sen oraya kadar yaz o sana soruyor, beraberinde ne bunları da kurayım mı diyor.
oluşturduğumuz migration dosyasına geliyoruz ve up içine;
1 2 3 4 5 6 |
Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('name'); $table->boolean('is_completed')->default(false); $table->timestamps(); }); |
Daha sonra factory dosyamıza geliyoruz. çünkü bunu çağırdığımızda içine dataları getirecek yer(ekleyecek değil, getirecek)
1 2 3 4 |
return [ 'name' => fake()->sentence(), 'is_completed' => rand(0, 1) ]; |
ee nasıl ekleyeceğiz? databaseseeder.php dosyasını aç. tabi varsa user vs şimdilik onları pas geç. biz sadece task ı çalışatacağız.
1 |
Task::factory(10)->create(); |
ee artık kaydetme sırası fakat daha migrate yapmadık.
php artisan migrate –seed
tüm tablolarımız oluştu. hatalar çıkabilir googlea girerek rahatlıkla çözün ve buraya geri dönün. env dosyasında 11den sonra veritabanı ayarının başında # var onları silmeyi unutmayın.
task tablomuz içinde datalı bir şekil hazır. şimdi toplu olarak ben size her dosyaya ne geleceğini atcam. ondan sonra kodları okuya okuya yaparsınız.
controller paylaşmadan önce bir kaç değişiklik daha yapmalısınız. api kısmını kurmalıyız. eskisi gibi değil kurmak gerekiyor.
php artisan install:api
sanctum kurulacak. tabi bu kurulduktan sonra bunun tablosunun da migrate edilmesi lazım. php artisan migrate ile işlemi tamamlıyoruz. gerçi terminalde size sorar o yes/no şeklinde bunları bunları kurdum bunu da kurmamı istiyor musun diyecek yes diyeceksin.
bootstrap/app.php bölümüne api kısmı eklenecek otomatik olarak. sen yine de kontrol et eklenmiş mi diye?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // }) ->withExceptions(function (Exceptions $exceptions) { // })->create(); |
şimdi aşağıda 2 controller, 2 request, 1 resource dosyamız var.
TaskController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php namespace App\Http\Controllers\Api\v1; use App\Http\Controllers\Controller; use App\Http\Resources\TaskResource; use App\Models\Task; use Illuminate\Http\Request; use App\Http\Requests\StoreTaskRequest; use App\Http\Requests\UpdateTaskRequest; class TaskController extends Controller { /** * Display a listing of the resource. */ public function index() { return TaskResource::collection(Task::all()); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(StoreTaskRequest $request) { $task = Task::create($request->validated()); return TaskResource::make($task); } /** * Display the specified resource. */ public function show(Task $taskModel) { return TaskResource::make($taskModel); } /** * Show the form for editing the specified resource. */ public function edit(Task $taskModel) { // } /** * Update the specified resource in storage. */ public function update(UpdateTaskRequest $request, Task $taskModel) { $taskModel->update($request->validated()); return TaskResource::make($taskModel); } /** * Remove the specified resource from storage. */ public function destroy(Task $taskModel) { $taskModel->delete(); return response()->noContent(); } } |
CompleteTaskController (Gereksiz ama neyse update yapıyor)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace App\Http\Controllers\Api\v1; use App\Models\Task; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Http\Resources\TaskResource; class CompleteTaskController extends Controller { /** * Handle the incoming request. */ public function __invoke(Request $request, Task $task) { $task->is_completed = $request->is_completed; $task->save(); return TaskResource::make($task); } } |
Request/StoreTaskRequest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class StoreTaskRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ 'name' => 'required|string|max:255' ]; } } |
Request/UpdateTaskRequest (Bak BUNDA class UpdateTaskRequest extends StoreTaskRequest bölümü var Storedakini kullanıyor unutma)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UpdateTaskRequest extends StoreTaskRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return false; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ // ]; } } |