Réalisation du tp3, tp4, tp5 et tp6 avec documentation.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Http\Requests\StoreClientRequest;
|
||||
use App\Http\Requests\UpdateClientRequest;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ClientController extends Controller
|
||||
{
|
||||
// TP6 : recherche sur nom, email et ville, puis dix clients par page.
|
||||
public function index(Request $request)
|
||||
{
|
||||
$searchInput = $request->query('search', '');
|
||||
$search = is_string($searchInput) ? trim($searchInput) : '';
|
||||
$query = Client::query();
|
||||
|
||||
if ($search !== '') {
|
||||
$query->where(function ($query) use ($search) {
|
||||
$query->where('name', 'like', '%'.$search.'%')
|
||||
->orWhere('email', 'like', '%'.$search.'%')
|
||||
->orWhere('city', 'like', '%'.$search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
$clients = $query->orderBy('id')->paginate(10)->withQueryString();
|
||||
|
||||
return view('clients.index', compact('clients', 'search'));
|
||||
}
|
||||
|
||||
// TP4 : formulaires et actions du CRUD (créer, voir, modifier, supprimer).
|
||||
public function create()
|
||||
{
|
||||
return view('clients.create');
|
||||
}
|
||||
|
||||
// TP6 : Laravel valide le Form Request avant d'appeler cette méthode.
|
||||
public function store(StoreClientRequest $request)
|
||||
{
|
||||
Client::create($request->validated());
|
||||
|
||||
return redirect()->route('clients.index');
|
||||
}
|
||||
|
||||
public function show(Client $client)
|
||||
{
|
||||
return view('clients.show', compact('client'));
|
||||
}
|
||||
|
||||
public function edit(Client $client)
|
||||
{
|
||||
return view('clients.edit', compact('client'));
|
||||
}
|
||||
|
||||
public function update(UpdateClientRequest $request, Client $client)
|
||||
{
|
||||
$client->update($request->validated());
|
||||
|
||||
return redirect()->route('clients.index');
|
||||
}
|
||||
|
||||
public function destroy(Client $client)
|
||||
{
|
||||
$client->delete();
|
||||
|
||||
return redirect()->route('clients.index');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,40 +6,26 @@ class ClientsController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
{
|
||||
// $clients = Client::all();
|
||||
|
||||
// return view('clients.index', [
|
||||
// 'clients' => $clients
|
||||
// ]);
|
||||
// }
|
||||
|
||||
// $clients = Client::where('city', 'Paris')->get();
|
||||
// $clients = Client::where('name', 'like', '%Martin%')->get();
|
||||
$clients = Client::find(1);
|
||||
return view('clients.index', [
|
||||
'clients' => $clients
|
||||
]);
|
||||
}
|
||||
|
||||
// $clients = [
|
||||
// [
|
||||
// 'name' => 'Jean Dupont',
|
||||
// 'email' => 'jean@example.com',
|
||||
// 'city' => 'Paris'
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Marie Martin',
|
||||
// 'email' => 'marie@example.com',
|
||||
// 'city' => 'Lyon'
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Paul Durand',
|
||||
// 'email' => 'paul@example.com',
|
||||
// 'city' => 'Marseille'
|
||||
// ]
|
||||
// ];
|
||||
|
||||
// return view('clients', ['clients' => $clients]);
|
||||
return $this->renderClients(Client::all());
|
||||
}
|
||||
}
|
||||
|
||||
public function paris()
|
||||
{
|
||||
return $this->renderClients(Client::where('city', 'Paris')->get());
|
||||
}
|
||||
|
||||
public function martin()
|
||||
{
|
||||
return $this->renderClients(Client::where('name', 'like', '%Martin%')->get());
|
||||
}
|
||||
|
||||
public function second()
|
||||
{
|
||||
return $this->renderClients(collect([Client::findOrFail(2)]));
|
||||
}
|
||||
|
||||
private function renderClients($clients)
|
||||
{
|
||||
return view('clients.index', compact('clients'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,5 @@ namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// ici tu mettras ton tableau $clients
|
||||
|
||||
// puis tu retourneras la vue clients
|
||||
}
|
||||
|
||||
// TP6 : chaque contrôleur définit son propre index(), avec sa signature adaptée.
|
||||
}
|
||||
|
||||
@@ -2,48 +2,68 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Http\Requests\StoreProductRequest;
|
||||
use App\Http\Requests\UpdateProductRequest;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index(){
|
||||
// TP6 : recherche par nom ou prix (bonus), avec pagination de dix produits.
|
||||
public function index(Request $request)
|
||||
{
|
||||
$searchInput = $request->query('search', '');
|
||||
$search = is_string($searchInput) ? trim($searchInput) : '';
|
||||
$query = Product::query();
|
||||
|
||||
$products = Product::all();
|
||||
return view('product.index', [
|
||||
'product' => $products
|
||||
]);
|
||||
if ($search !== '') {
|
||||
$query->where(function ($query) use ($search) {
|
||||
$query->where('name', 'like', '%'.$search.'%')
|
||||
->orWhere('price', 'like', '%'.$search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
// $products = [
|
||||
// [
|
||||
// 'name' => 'Ordinateur portable',
|
||||
// 'price' => 899,
|
||||
// 'stock' => 12
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Écran',
|
||||
// 'price' => 249,
|
||||
// 'stock' => 7
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Clavier',
|
||||
// 'price' => 79,
|
||||
// 'stock' => 25
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Souris',
|
||||
// 'price' => 39,
|
||||
// 'stock' => 42
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'Webcam',
|
||||
// 'price' => 99,
|
||||
// 'stock' => 4
|
||||
// ]
|
||||
// ];
|
||||
$products = $query->orderBy('id')->paginate(10)->withQueryString();
|
||||
|
||||
// return view('products.index', ['products' => $products]);
|
||||
|
||||
// }
|
||||
|
||||
return view('products.index', compact('products', 'search'));
|
||||
}
|
||||
}
|
||||
|
||||
// TP4 : les six autres actions complètent le CRUD des produits.
|
||||
public function create()
|
||||
{
|
||||
return view('products.create');
|
||||
}
|
||||
|
||||
// TP6 : seules les données validées par le Form Request sont enregistrées.
|
||||
public function store(StoreProductRequest $request)
|
||||
{
|
||||
Product::create($request->validated());
|
||||
|
||||
return redirect()->route('products.index');
|
||||
}
|
||||
|
||||
public function show(Product $product)
|
||||
{
|
||||
return view('products.show', compact('product'));
|
||||
}
|
||||
|
||||
public function edit(Product $product)
|
||||
{
|
||||
return view('products.edit', compact('product'));
|
||||
}
|
||||
|
||||
public function update(UpdateProductRequest $request, Product $product)
|
||||
{
|
||||
$product->update($request->validated());
|
||||
|
||||
return redirect()->route('products.index');
|
||||
}
|
||||
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
$product->delete();
|
||||
|
||||
return redirect()->route('products.index');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user