Don gina ƙirƙira, sabuntawa, da share fasali a cikin Laravel, bi waɗannan matakan:
Ƙayyade Route
Fara da ayyana route s don sarrafa ƙirƙira, ɗaukakawa, da share ayyuka.
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');
A cikin misalin da ke sama, mun ayyana route s don ƙirƙirar mai amfani, adana mai amfani, gyara mai amfani, sabunta mai amfani, da share mai amfani.
Ƙayyade da Controller
Na gaba, ayyana hanyoyin a cikin controller don sarrafa buƙatun daga 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.');
}
}
A kowace hanya, zaku iya aiwatar da ayyuka masu dacewa kamar nuna fom, adana sabbin bayanai, sabunta bayanan da ke akwai, da share bayanai.
Ƙirƙiri Mai Amfani Interface
Ƙirƙiri mai amfani interface( views
) don nuna fom da duba bayanai. Misali:
Jerin( views/users/index.blade.php
):
@foreach($users as $user)
<p>{{ $user->name }}- {{ $user->email }}</p>
@endforeach
Gyara Form( 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>
Gyara Form( 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>
Hannun Bayanai
A cikin ma'ajin da sabunta hanyoyin a cikin controller, zaku iya amfani da hanyoyin da za a iya ɗauka don adanawa da sabunta bayanai a cikin ma'ajin bayanai.
Nuna Saƙonni
A ƙarshe, zaku iya nuna nasara ko saƙonnin kuskure ga mai amfani bayan yin ƙirƙira, ɗaukakawa, da share ayyuka.
- Yi amfani da Laravel Zama don nuna nasara ko saƙonnin kuskure a cikin ra'ayoyi.
Ta bin waɗannan matakan, kun sami nasarar gina abubuwan ƙirƙira, sabuntawa, da share abubuwan a cikin Laravel.