I created a table called "directors" using migrations.Then i created the model, view, controller. In "DirectorController" i wrote crud operations logic for my table. It works until i try to edit a existing record. To be clear it update all informations except the image, it remains the same. Can someone tell me what is wrong?
*Folder "images" exists in public.
//Migration
public function up()
{
Schema::create('directors', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->datetime('birth');
$table->string('town');
$table->string('country');
$table->string('image');
$table->timestamps();
});
}
//DirectorController
public function update(Request $request, Director $director)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'birth' => 'required',
'town' => 'required',
'country' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'images/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
$director->update($input);
return redirect()->route('directors.index')->with('success', 'Director updated successfully.');
}
//DirectorModel
protected $fillable = [
'first_name',
'last_name',
'birth',
'town',
'country',
'image'];
public function movies() {
return $this->hasMany(Movie::class);
}
// edit.blade.php
@extends('directors.layout')
@section('content')
<div >
<div >
<div >
<h2>Edit Director</h2>
</div>
<div >
<a href="{{ route('directors.index') }}"><i ></i> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div >
<div >
<strong>Whoops!</strong>
<button type="button" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
There were some problems with your input. <br><br>
<ul>
@foreach ( $errors->all() as $error )
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('directors.update', $director->id) }}" method="POST">
@csrf
@method('PUT')
<div >
<div >
<div >
<strong>First Name:</strong>
<input type="text" name="first_name" value="{{ $director->first_name }}" placeholder="First Name">
</div>
</div>
<div >
<div >
<strong>Last Name:</strong>
<input type="text" name="last_name" value="{{ $director->last_name }}" placeholder="Last Name">
</div>
</div>
<div >
<div >
<strong>Birth:</strong>
<input type="text" name="birth" value="{{ $director->birth }}" placeholder="Birth">
</div>
</div>
<div >
<div >
<strong>Town:</strong>
<input type="text" name="town" value="{{ $director->town }}" placeholder="Town">
</div>
</div>
<div >
<div >
<strong>Country:</strong>
<input type="text" name="country" value="{{ $director->country }}" placeholder="Country">
</div>
</div>
<div >
<div >
<strong>Image:</strong>
<input type="file" name="image" placeholder="image">
<img src="/images/{{ $director->image }}" width="300px">
</div>
</div>
<div >
<button type="submit" ><i ></i> Submit</button>
</div>
</div>
</form>
@endsection
CodePudding user response:
You need to change your form as:
<form action="{{ route('directors.update', $director->id) }}" method="POST" enctype="multipart/form-data">