The CRUD of my Laravel 8 based application works almost fine except the update
which keeps giving me the following error:
However my code seems correct.
Here is my table information:
Schema::create('posts', function (Blueprint $table) {
$table->id('id');
$table->string('title')->unique();
$table->text('description');
$table->string('price');
$table->string('image_url');
$table->timestamps();
$table->softDeletes();
});
Then my controller's update method is as follows:
public function update($id, UpdatePostRequest $request)
{
$post = $this->postRepository->find($id);
if (empty($post)) {
Flash::error('Post not found');
return redirect(route('posts.index'));
}
$post = $this->postRepository->update($request->all(), $id);
Flash::success('Post updated successfully.');
return redirect(route('posts.index'));
}
Please I have tried everything:
php artisan migrate
php artisan migrate:rollback
php artisan migrate:refresh
php artisan migrate:fresh
php artisan optimize:clear
Absolutely everything, but the error persists. However column 1
is indeed present in the Database:
You can see it in the database screenshot
Yet it is completely impossible to change it. It becomes a puzzle for me.
And I really need your help.
CodePudding user response:
Here is the content of PostRepository:
<?php
namespace App\Repositories;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Repositories\BaseRepository;
/**
* Class PostRepository
* @package App\Repositories
* @version December 2, 2021, 3:03 am UTC
*/
class PostRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'title',
'description',
'price',
'image_url'
];
/**
* Return searchable fields
*
* @return array
*/
public function getFieldsSearchable()
{
return $this->fieldSearchable;
}
/**
* Configure the Model
**/
public function model()
{
return Post::class;
}
public function createPost(Request $request){
$file = $request->file('image_url');
$originalName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
// dd($file);
$imageName = uniqid().'.'.$extension;
$path = $file->move(public_path('images'), $imageName);
// dd($imageName);
$input = $request->all();
// $input['image_url'] = $path;
$input['image_url'] = $imageName;
return $this->create($input);
}
}
CodePudding user response:
Why such complexity just for a simple update? Wouldn't be easier to update the $post
object you already fetched?
public function update($id, UpdatePostRequest $request)
{
$post = $this->postRepository->find($id);
if (empty($post)) {
Flash::error('Post not found');
return redirect(route('posts.index'));
}
$post->update($request->all());
Flash::success('Post updated successfully.');
return redirect(route('posts.index'));
}
CodePudding user response:
where's your update function in the repository class???
it should look like
public function update($input, $id)
{
$model = $this->find($id);
$model->fill($input);
return $model->save();
}
CodePudding user response:
Thank you for your various interventions. I'll try to correct.