Laravel_REST_API/app/Http/Controllers/Api/ArticleController.php
2025-02-10 11:08:38 +01:00

67 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Article;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return Article::all();
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'titre' => 'required|string|max:255',
'contenu' => 'required|string',
]);
$article = Article::create($validated);
return redirect('/');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
return Article::findOrFail($id);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$validated = $request->validate([
'titre' => 'required|string|max:255',
'contenu' => 'required|string',
]);
$article = Article::findOrFail($id);
$article->update($validated);
return response()->json($article, 200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$article = Article::findOrFail($id);
$article->delete();
return redirect('/');
}
}