Laravel RESTful API Ayyukan CRUD: APIs masu Ingantaccen Gina

Yin ayyukan CRUD(Ƙirƙiri, Read, Update, Share) a cikin wani Laravel RESTful API muhimmin al'amari na gina aikace-aikace. A ƙasa, zan jagorance ku ta kowace aiki a cikin Laravel RESTful API aikace-aikacen:

1. Create

Don ƙara sabon rikodin zuwa bayanan bayanai, kuna buƙatar ayyana hanya a cikin Controller don karɓar POST buƙatun masu amfani. Misali, ga create sabon mai amfani:

use App\Models\User;  
use Illuminate\Http\Request;  
  
public function store(Request $request)  
{  
    $user = User::create($request->all());  
    return response()->json($user, 201);  
}  

2. Read

Don dawo da bayanai daga ma'ajin bayanai, zaku iya ayyana hanya a cikin Controller buƙatun GET masu amfani. Misali, don dawo da jerin masu amfani:

use App\Models\User;  
  
public function index()  
{  
    $users = User::all();  
    return response()->json($users);  
}  

3. Update

Don update bayanin rikodin da ke akwai, kuna buƙatar ayyana hanya a cikin Controller don karɓar PUT buƙatun masu amfani. Misali, ga update bayanin mai amfani:

use App\Models\User;  
use Illuminate\Http\Request;  
  
public function update(Request $request, $id)  
{  
    $user = User::findOrFail($id);  
    $user->update($request->all());  
    return response()->json($user, 200);  
}  

4. Delete

Don cire rikodin daga ma'ajin bayanai, zaku iya ayyana hanya a cikin Controller don karɓar DELETE buƙatun daga masu amfani. Misali, ga delete mai amfani:

use App\Models\User;  
  
public function destroy($id)  
{  
    $user = User::findOrFail($id);  
    $user->delete();  
    return response()->json(null, 204);  
}  

Lura cewa kana buƙatar tabbatar da cewa kun saita hanyoyin da suka dace a cikin routes/api.php fayil don haɗi zuwa hanyoyin a cikin Controller.

Tare da waɗannan umarnin, yanzu kuna iya yin ayyukan CRUD a cikin Laravel RESTful API aikace-aikacen ku.