0% found this document useful (0 votes)
39 views8 pages

Exemple - Implémenter CRUD Sur Laravel

The document shows how to implement CRUD operations in Laravel by creating a contacts management application with models, migrations, controllers and views to add, edit, update and delete contacts from the database.

Uploaded by

Elissaoui Meriem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Exemple - Implémenter CRUD Sur Laravel

The document shows how to implement CRUD operations in Laravel by creating a contacts management application with models, migrations, controllers and views to add, edit, update and delete contacts from the database.

Uploaded by

Elissaoui Meriem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Exemple - Implémenter CRUD sur Laravel

laravel new contacts


cd contacts
code .
php artisan make:model Contact -mcr
php artisan migrate

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration


{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('nom',15);
$table->string('prenom',15);
$table->string('email',50)->unique();
$table->string('telephone',14)->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
};

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model


{
use HasFactory;
protected $fillable=['nom','prenom','email','telephone',];

<?php

use App\Http\Controllers\ContactController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('welcome');
});
Route::resource('contacts',ContactController::class);

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contacts</title>
<link rel="stylesheet" href="{{asset('css/style.css')}}">
</head>
<body>
<header>
<h1>Gestion des contacts</h1>
</header>
<nav>
<ul>
<li><a href="{{route('contacts.index')}}">Mes contacts</a></li>
<li><a href="{{route('contacts.create')}}">Nouveau contact</a></li>
</ul>
</nav>
<main>
@yield('content')
</main>
<footer>
Copyright &copy;{{date('Y')}}
</footer>
</body>
</html>

@extends('master')
@section('content')
<h2>Mes contacts</h2>
@if(Session::get('info'))
<div class="info">{{Session::get('info')}}</div>
@endif
<table>
<tr>
<th>Identifiant</th>
<th>Nom</th>
<th>Prénom</th>
<th>Email</th>
<th>Téléphone</th>
<th colspan="2">Actions</th>
</tr>
@foreach($contacts as $contact)
<tr>
<td>{{$contact->id}}</td>
<td>{{$contact->nom}}</td>
<td>{{$contact->prenom}}</td>
<td>{{$contact->email}}</td>
<td>{{$contact->telephone}}</td>
<td>
<form action="{{route('contacts.destroy',$contact)}}" method="post"
onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer ?')">
@method('DELETE')
@csrf
<button type='submit'>Supprimer</button>
</form>
</td>
<td>
<a class='update' href="{{route('contacts.edit',$contact)}}">Modifier</a>
</td>
</tr>
@endforeach
</table>
@stop

@extends('master')
@section('content')
<h2>Nouveau contact</h2>
<form class='form' action="{{route('contacts.store')}}" method="post">
@method('POST')
@csrf
<p>
<div><label for="">Nom</label></div>
<input type="text" name="nom" required>
</p>
<p>
<div><label for="">Prénom</label></div>
<input type="text" name="prenom" required>
</p>
<p>
<div><label for="">Email</label></div>
<input type="email" name="email" required>
</p>
<p>
<div><label for="">Téléphone</label></div>
<input type="tel" name="telephone" required>
</p>
<p>
<button type='submit' class='save'>Enregister</button>
</p>
</form>
@stop

@extends('master')
@section('content')
<h2>Modifier un contact</h2>
<form class='form' action="{{route('contacts.update',$contact)}}" method="post">
@method('PATCH')
@csrf
<p>
<div><label for="">Nom</label></div>
<input type="text" name="nom" required value="{{$contact->nom}}">
</p>
<p>
<div><label for="">Prénom</label></div>
<input type="text" name="prenom" required value="{{$contact->prenom}}">
</p>
<p>
<div><label for="">Email</label></div>
<input type="email" name="email" required value="{{$contact->email}}">
</p>
<p>
<div><label for="">Téléphone</label></div>
<input type="tel" name="telephone" required value="{{$contact->telephone}}">
</p>
<p>
<a class='cancel' href="{{route('contacts.index')}}"><button
type='button'>Annuler</button></a>
<button type='submit' class='update'>Enregister</button>
</p>
</form>
@stop

<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use Illuminate\Http\Request;

class ContactController extends Controller


{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$contacts=Contact::all();
return view('index',compact('contacts'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validate_data=$request->validate([
'nom'=>'required|max:15',
'prenom'=>'required|max:15',
'email'=>'required|max:50',
'telephone'=>'required|max:14',
]);
Contact::create($validate_data);
return redirect()->route('contacts.index')->with('info','Contact ajouté !!');
}

/**
* Display the specified resource.
*
* @param \App\Models\Contact $contact
* @return \Illuminate\Http\Response
*/
public function show(Contact $contact)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Contact $contact
* @return \Illuminate\Http\Response
*/
public function edit(Contact $contact)
{
return view('edit',['contact'=>$contact]);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Contact $contact
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Contact $contact)
{
$validate_data=$request->validate([
'nom'=>'required|max:15',
'prenom'=>'required|max:15',
'email'=>'required|max:50',
'telephone'=>'required|max:14',
]);
$contact->update($validate_data);
return redirect()->route('contacts.index')->with('info','Contact modifié !!');

/**
* Remove the specified resource from storage.
*
* @param \App\Models\Contact $contact
* @return \Illuminate\Http\Response
*/
public function destroy(Contact $contact)
{
$contact->delete();
return redirect()->route('contacts.index')->with('info','Contact supprimé !!');

}
}

html,body{
margin:0;
padding:0;
font-family: sans-serif;
}
header{
padding: 5px;
background-color: blue;
color: #fff;
}
main{
padding: 0 15px;
min-height: 73vh;
}
footer{
padding: 8px;
font-size: 12px;
font-style: italic;
color: #fff;
background-color: #444;
}
main table{
border:1px solid;
width: 100%;
border-collapse: collapse;

}
tr{
border:1px solid;
}
th{
background-color: #000;
padding: 8px;
color: #fff;
text-align: left;
}
td{
padding: 5px;
}
tr:hover{
background-color: #090;
color: #fff;
font-weight: bold;
cursor: pointer;
}

nav ul li{
display: inline-block;
padding: 0 10px;
}
main h2{
text-align: center;
}
.form{
width: 600px;
box-shadow: 0 0 15px;
margin: 10px auto;
padding: 10px;
border-radius: 10px;

}
input{
padding: 8px;
font-size: 20px;
width: 95%;
}
.save{
padding: 8px;
font-size: 22px;
width: 100%;
border-radius: 10px;
border: none;
background-color: #08f;
color: #fff;
letter-spacing: 3px;
}
label{
font-weight: bold;
}
.info{
background-color: #090;
color: #fff;
font-size: 22px;
font-weight: bold;
text-align: center;
padding: 10px;
}
.update{
background-color: #aaa;
color: #fff;
font-size: 15px;
text-align: center;
padding: 5px 10px;
text-decoration: none;
}
.update{
background-color: #aaa;
color: #fff;
font-size: 15px;
text-align: center;
padding: 5px 10px;
text-decoration: none;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy