U routes.php dodati
Route::delete('/clients/{id}', [App\Http\Controllers\ClientController::class, 'destroy']);
Route::get('/clients/{id}/edit', [App\Http\Controllers\ClientController::class, 'edit']);
Route::put('/clients/{id}', [App\Http\Controllers\ClientController::class, 'update']);
U clients/index.blade.php dodati
<th scope="col">Actions</th>
i
<td>
<a href="/clients/{{$client->id}}/edit" class="btn btn-info">EDIT</a>
<form method="POST" action="/clients/{{$client->id}}" style="display: inline">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
U ClientController dodati funkcije edit, update i destroy
public function edit($id){
$client = Client::find($id);
$data = [
'client' => $client,
];
return view('clients.edit', $data);
}
public function update(Request $request, $id){
$client = Client::find($id);
$client->first_name = $request->first_name;
$client->last_name = $request->last_name;
$client->save();
return redirect('clients');
}
public function destroy($id){
$client = Client::find($id);
$client->delete();
return redirect('clients');
}
U views/clients dodati edit.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="POST" action="/clients/{{$client->id}}">
@csrf
{{ method_field('PUT') }}
<div class="form-group">
<label for="exampleInputEmail1">First name</label>
<input class="form-control" name="first_name" value="{{$client->first_name}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Last name</label>
<input class="form-control" name="last_name" value="{{$client->last_name}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" value="{{$client->email}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phones</label>
<input class="form-control" name="phone" value="{{$client->phone}}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
@endsection
Dodavanje novih polja u postojecu tabelu u bazi
php artisan make:migration add_fields_to_clients_table
Pa onda u folderu database/migrations naci novu migraciju i dodati dva polja(dvije linije code-a)
Schema::table('clients', function (Blueprint $table) {
$table->string('email', 100);
$table->string('phone', 100);
});
Na kraju azurirati clients/index.blade.php fajl, dodati dio za email i phone(u nastavku je samo dio fajla, ne cijeli fajl index)
<th scope="col">ID</th>
<th scope="col">First name</th>
<th scope="col">Last name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach($clients as $client)
<tr>
<td>{{ $client->id }}</td>
<td>{{ $client->first_name }}</td>
<td>{{ $client->last_name }}</td>
<td>{{ $client->email }}</td>
<td>{{ $client->phone }}</td>