Biex tibni l-ħolqien, taġġorna u tħassar il-karatteristiċi fi Laravel, segwi dawn il-passi:
Iddefinixxi Route
Ibda billi tiddefinixxi route s biex timmaniġġja l-azzjonijiet tal-ħolqien, taġġorna u ħassar.
Route::get('/users', 'UserController@index')->name('users.index');
Route::get('/users/create', 'UserController@create')->name('users.create');
Route::post('/users', 'UserController@store')->name('users.store');
Route::get('/users/{id}/edit', 'UserController@edit')->name('users.edit');
Route::put('/users/{id}', 'UserController@update')->name('users.update');
Route::delete('/users/{id}', 'UserController@destroy')->name('users.destroy');
Fl-eżempju ta 'hawn fuq, aħna niddefinixxu route s għall-ħolqien ta' utent, il-ħażna ta 'utent, l-editjar ta' utent, l-aġġornament ta 'utent u t-tħassir ta' utent.
Iddefinixxi l- Controller
Sussegwentement, iddefinixxi l-metodi fil- controller biex jimmaniġġaw it-talbiet mill- route s.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
$users = User::all();
return view('users.index', compact('users'));
}
public function create()
{
return view('users.create');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email',
]);
$user = User::create($validatedData);
return redirect()->route('users.index')->with('success', 'User created successfully.');
}
public function edit($id)
{
$user = User::findOrFail($id);
return view('users.edit', compact('user'));
}
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email',
]);
$user = User::findOrFail($id);
$user->update($validatedData);
return redirect()->route('users.index')->with('success', 'User updated successfully.');
}
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect()->route('users.index')->with('success', 'User deleted successfully.');
}
}
F'kull metodu, tista 'twettaq l-azzjonijiet korrispondenti bħall-wiri ta' formola, il-ħażna ta 'dejta ġdida, l-aġġornament tad-dejta eżistenti u t-tħassir tad-dejta.
Oħloq Utent Interface
Oħloq l-utent interface( views
) biex turi formoli u tara d-dejta. Pereżempju:
Lista( views/users/index.blade.php
):
@foreach($users as $user)
<p>{{ $user->name }}- {{ $user->email }}</p>
@endforeach
Editja Formola( views/users/create.blade.php
):
<form method="POST" action="{{ route('users.store') }}">
@csrf
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<button type="submit">Create User</button>
</form>
Editja Formola( views/users/edit.blade.php
):
<form method="POST" action="{{ route('users.update', $user->id) }}">
@csrf
@method('PUT')
<input type="text" name="name" value="{{ $user->name }}">
<input type="email" name="email" value="{{ $user->email }}">
<button type="submit">Update User</button>
</form>
Immaniġġja Dejta
Fil-metodi ta 'maħżen u taġġorna fil- controller, tista' tuża metodi Eloquent biex taħżen u taġġorna d-dejta fid-database.
Uri Messaġġi
Fl-aħħarnett, tista 'turi messaġġi ta' suċċess jew ta 'żball lill-utent wara li twettaq l-azzjonijiet ta' toħloq, taġġorna u tħassar.
- Uża Laravel Sessjoni biex turi messaġġi ta' suċċess jew ta' żball fl-opinjonijiet.
Billi ssegwi dawn il-passi, bnejt b'suċċess il-karatteristiċi tal-ħolqien, l-aġġornament u l-ħassar fi Laravel.