Home > Net >  Laravel 9 Failing to update data in DB
Laravel 9 Failing to update data in DB

Time:01-13

I am building a blog using Laravel 9 and my update method for some unknown reason fails to update

My code Samples

Model

class Anime extends Model
{
    use HasFactory;

    protected $table = 'anime';

    protected $primaryKey = 'id';

    protected $fillable = ['anime_title','user_id','blog_title','description','slug','anime_image_profile'];

    public function blogInformation() {
        return $this->hasMany(BlogInfo::class);
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }

//    protected $hidden = 'id';
}

Controller

public function update(ValidateAnimeBlogRequest  $request, $id)
    {
        $request->validated();

        /*Update the details in the database by ID*/
        $update_data = Anime::findOrFail($id);

        $update_data = new Anime;

        $update_data->anime_title = $request->input('anime_title');
        $update_data->blog_title = $request->input('blog_title');
        $update_data->user_id = auth()->user()->id;
        $update_data->description = $request->input('description');
        $update_data->slug = Str::slug($request->input('blog_title'));


        /*Check if the user also wanted to update the image*/
        if($request->hasFile('anime_image_profile')) {

            $path_to_images = 'images/anime_image_profile/' . $update_data->anime_image_profile;

            if(File::exists($path_to_images)) {
                File::delete($path_to_images);
            }

            $new_file_name = '9anime' . '-' . time() . '-' . $request->name . '.' . $request->anime_image_profile->extension();

            $request->anime_image_profile->move(public_path('images/anime_image_profile'), $new_file_name);

            $update_data->anime_image_profile = $new_file_name;
        }

        if($update_data->update()) {
            redirect('/');
        }
        dd('Error');

    }

ValidateAnimeBlogRequest

public function rules()
    {
        return [
            'anime_title' => 'required | min:2', new nameRegex,
            'blog_title' => ['required','min:5', new nameRegex],
            'description' => ['required','min:1000'],
            'premiered' => ['required'],
            'genre' => ['required', new nameRegex],
            'licensors' => ['required', new nameRegex],
            'studio' => ['required', new nameRegex],
            'anime_image_profile' => 'required | mimes:jpeg,jpg,png | max:5408'
        ];
    }

My blade file

 <form enctype="multipart/form-data" autocomplete="off" action="/blog/{{$anime['id']}}" method="POST">

                    @method('PUT')
                    @csrf

I set up a custom check just in case

 if($update_data->update()) {
            redirect('/');
        }
        dd('Error');

The output on my webpage from this is "Error" // app\Http\Controllers\AnimeController.php:156

And when I dd($update_data) I see that the data has been updated yet it does not get sent to the database.

I tried replacing $update_data->update() with $update_data->save() but that now creates new data in the DB instead of updating the existing one

CodePudding user response:

You can keep it as the save() method. Just update the lines above where you are creating a new Anime() instance to only be created if the record cannot be found via $id from the line above.

public function update(ValidateAnimeBlogRequest  $request, $id)
{
    $request->validated();

    /*Update the details in the database by ID*/
    $update_data = Anime::findOrFail($id);

    if(!$update_data) {
        $update_data = new Anime;
    }

    $update_data->anime_title = $request->input('anime_title');
    $update_data->blog_title = $request->input('blog_title');
    $update_data->user_id = auth()->user()->id;
    $update_data->description = $request->input('description');
    $update_data->slug = Str::slug($request->input('blog_title'));


    /*Check if the user also wanted to update the image*/
    if($request->hasFile('anime_image_profile')) {

        $path_to_images = 'images/anime_image_profile/' . $update_data->anime_image_profile;

        if(File::exists($path_to_images)) {
            File::delete($path_to_images);
        }

        $new_file_name = '9anime' . '-' . time() . '-' . $request->name . '.' . $request->anime_image_profile->extension();

        $request->anime_image_profile->move(public_path('images/anime_image_profile'), $new_file_name);

        $update_data->anime_image_profile = $new_file_name;
    }

    if($update_data->save()) {
        redirect('/');
    }
    dd('Error');

}

This will create a new instance only if a record is not found and won't give a new db record

  • Related