yanzi's avatar
Level 2

Why am I getting error "is not callable" if not called?

Hi, I am a working on an e-commerce app (please note that it is my first app ever) and in the OrderController when saving the model into the database I am getting an error "Object of type App\Models\Order is not callable". As far as I can see, it is not called anywhere. What am I missing?

$orderCreated = Order::create($orderData);
        
        if ($orderCreated) {
			// dump(is_callable($orderCreated)); // returns false
            // dump(gettype($orderCreated)); //returns "object"
            // dump(get_class_methods($orderCreated)); // update is among the available methods
            // dd($orderCreated);
            $orderCreated->update(['order_number_public' => str_pad($orderCreated->id, 6, "0", STR_PAD_LEFT)]);
           
            return view("frontend.order.show", ["order_data" => $orderCreated]);

        } else {
            return redirect()->back()->with("error", "Unable to create the order.");
        }

EDIT: I have also tried a version suggested here by the AI which looks like this:

$orderCreated = Order::create($orderData);

        
        if ($orderCreated) {
            // dump(is_callable($orderCreated)); // returns false
            // dump(gettype($orderCreated)); //returns "object"
            // dump(get_class_methods($orderCreated)); // update is among the available methods
            // dd($orderCreated);
            $orderNumberPublic = str_pad($orderCreated->id, 6, "0", STR_PAD_LEFT);
    
            // Update the order with the generated public order number
            $orderCreated->update(['order_number_public' => $orderNumberPublic]);
           
            return view("frontend.order.show", ["order_data" => $orderCreated]);

        } else {
            return redirect()->back()->with("error", "Unable to create the order.");
        }

It did not help.

Thank you for any hints.

0 likes
4 replies
yanzi's avatar
Level 2

@dr_ummond HI, thanks for the reply. Below si the migration for the model. If I just create the model, it goes without an error. So the model setup sems to be fine. Also, I have moved the update part to an observer class into "created" method and then it works.

public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            $table->foreignIdFor(User::class)->nullable();
            $table->string("order_number_public")->nullable();
            $table->string("session_id");
            $table->string("price");
            $table->string("status"); 
            $table->string("delivery_method");
            $table->string("delivery_tracking_number")->nullable();
            $table->string("delivery_status")->nullable();
            $table->string("payment_method")->nullable();
            $table->string("payment_status")->nullable();
            $table->string("notes_customer")->nullable();
            $table->string("notes_internal")->nullable();
            $table->json("delivery_address");
            $table->json("invoice_data");
        });
    }

Anyway, now the app is working so if it is not obvious what might have been wrong let's not dwell on it needlessly. Thank you.

Snapey's avatar

full error message please and which line

yanzi's avatar
Level 2

@Snapey Hi, thank you for the reply. In the meantime i have moved the update part to an observer class and its "created" method and then it works. I have tried to go back to the original code but somehow now it still works so I am not able to get the original error and in the meantime a lot of changes have been made... From what i remember, the highlighted line was the $orderCreated->update(['order_number_public' => str_pad($orderCreated->id, 6, "0", STR_PAD_LEFT)]); .

Originally I had it

$order->order_number_public = str_pad($order->id, 6, "0", STR_PAD_LEFT);
$order->save();

and in this case the error was for the line $order->save(); , not for the previous one though...

Anyway, now the app is working so if it is not obvious what might have been wrong let's not dwell on it needlessly. Thank you.

Please or to participate in this conversation.