0

Here, I have setuo CRUD table with laravel, vuetify and vue . I could successfull create and read data from the database. But, for some reason my update and delete are not working. I am getting error like:

{message: "Creating default object from empty value", exception: "ErrorException",…} exception: "ErrorException" file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php" line: 53 message: "Creating default object from empty value" trace: [{file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php", line: 53,…},…]

My code are here:

StageController.php

<?php


namespace App\Sys\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

use App\Sys\Model\Stage;

class StageController extends Controller
{

    public function index(Request $request)
    {  
        $per_page = $request->per_page ? $request->per_page : 5;
        $sort_by = $request->sort_by;
        $order_by = $request->order_by;
        return response()->json(['stages' => Stage::orderBy($sort_by, $order_by)->paginate($per_page)],200);
    }

    public function store(Request $request)
    {
        $uuid = Str::uuid()->toString();
        $stage= Stage::create([
            'id' => $uuid,
            'code' =>$request->code,
            'name' =>$request->name,
            'description' =>$request->description,
        ]);
        return response()->json(['stage'=>$stage],200);
    }


    public function show($id)                                                                                                                                                           
    {
        $stages = Stage::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
        return response()->json(['stages' => $stages],200);
    }



    public function update(Request $request, $id)
    {


       $stage = Stage::find($id);


       $stage->code  = $request->code; //line 53
       $stage->name  = $request->name;
       $stage->description  = $request->description;


       $stage->save(); 
       return response()->json(['stage'=>$stage], 200);
    }

    public function destroy($id)
    {
        $stage = Stage::where('id', $id)->delete();
        return  response()->json(['stage'=> $stage],200);
    }



    public function deleteAll(Request $request){
        Stage::whereIn('id', $request->stages)->delete();
        return response()->json(['message', 'Records Deleted Successfully'], 200);
    }
}

Stage.php

<?php

namespace App\Sys\Model;

use Illuminate\Database\Eloquent\Model;

class Stage extends Model
{
    protected $guarded = [];
}

1 Answer 1

1

I just found they you are using uuid as id not increment. that why you get error like that:

to solve your problem you need to add the field to your model;

<?php

namespace App\Sys\Model;

use Illuminate\Database\Eloquent\Model;

class Stage extends Model
{
    public $incrementing = false;
    protected $keyType = 'string';
    protected $guarded = [];

}

I hope this time you can solve your problem. happy coding. Edit you can read docs for more info

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.