Kanggo mbangun fitur nggawe, nganyari, lan mbusak ing Laravel, tindakake langkah iki:
Netepake Route
Miwiti kanthi nemtokake route s kanggo nangani tumindak nggawe, nganyari, lan mbusak.
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');
Ing conto ing ndhuwur, kita nemtokake route s kanggo nggawe pangguna, nyimpen pangguna, nyunting pangguna, nganyari pangguna, lan mbusak pangguna.
Nemtokake Controller
Sabanjure, nemtokake cara ing kanggo controller nangani panjalukan saka 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.');
}
}
Ing saben cara, sampeyan bisa nindakake tumindak sing cocog kayata nampilake formulir, nyimpen data anyar, nganyari data sing wis ana, lan mbusak data.
Nggawe Panganggo Interface
Gawe pangguna interface( views
) kanggo nampilake formulir lan ndeleng data. Tuladhane:
Daftar( views/users/index.blade.php
):
@foreach($users as $user)
<p>{{ $user->name }}- {{ $user->email }}</p>
@endforeach
Sunting Formulir( 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>
Sunting Formulir( 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>
Nangani Data
Ing nyimpen lan nganyari cara ing controller, sampeyan bisa nggunakake cara Eloquent kanggo nyimpen lan nganyari data ing database.
Tampilan Pesen
Pungkasan, sampeyan bisa nampilake pesen sukses utawa kesalahan menyang pangguna sawise nindakake tumindak nggawe, nganyari, lan mbusak.
- Gunakake Laravel Sesi kanggo nampilake pesen sukses utawa kesalahan ing tampilan.
Kanthi ngetutake langkah-langkah iki, sampeyan wis kasil mbangun fitur nggawe, nganyari, lan mbusak ing Laravel.