Chun na gnéithe a chruthú, a nuashonrú agus a scriosadh i Laravel, lean na céimeanna seo:
Sainmhínigh Route
Tosaigh trí shainiú route s chun na gníomhartha a chruthú, a nuashonrú agus a scriosadh a láimhseáil.
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');
Sa sampla thuas, sainímid route s chun úsáideoir a chruthú, úsáideoir a stóráil, úsáideoir a chur in eagar, úsáideoir a nuashonrú, agus úsáideoir a scriosadh.
Sainmhínigh an Controller
Ansin, sainigh na modhanna chun controller na hiarratais ón 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.');
}
}
I ngach modh, is féidir leat na gníomhartha comhfhreagracha a dhéanamh, mar shampla foirm a thaispeáint, sonraí nua a stóráil, sonraí atá ann cheana a nuashonrú, agus sonraí a scriosadh.
Cruthaigh Úsáideoir Interface
Cruthaigh an t-úsáideoir interface( views
) chun foirmeacha a thaispeáint agus sonraí a fheiceáil. Mar shampla:
Liosta( views/users/index.blade.php
):
@foreach($users as $user)
<p>{{ $user->name }}- {{ $user->email }}</p>
@endforeach
Cuir Foirm in Eagar( 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>
Cuir Foirm in Eagar( 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>
Láimhseáil Sonraí
Sa stór agus modhanna nuashonraithe sa controller, is féidir leat modhanna eloquent a úsáid chun sonraí a stóráil agus a nuashonrú sa bhunachar sonraí.
Taispeáin Teachtaireachtaí
Ar deireadh, is féidir leat teachtaireachtaí ratha nó earráide a thaispeáint don úsáideoir tar éis duit na gníomhartha a chruthú, a nuashonrú agus a scriosadh.
- Úsáid Laravel Seisiún chun teachtaireachtaí ratha nó earráide a thaispeáint i radharcanna.
Trí na céimeanna seo a leanúint, d'éirigh leat na gnéithe a chruthú, a nuashonrú agus a scriosadh i Laravel.