skip to Main Content

my method save is undefined, i dont know why.
this is about my laravel/php:

Environment …………………………………………………
Application Name …………………………………….. Laravel
Laravel Version ……………………………………… 10.14.1
PHP Version …………………………………………… 8.2.4
Composer Version ………………………………………. 2.5.8
Environment …………………………………………… local
Debug Mode ………………………………………….. ENABLED
URL ………………………………………………. localhost
Maintenance Mode ………………………………………… OFF

Cache ………………………………………………………
Config …………………………………………… NOT CACHED
Events …………………………………………… NOT CACHED
Routes …………………………………………… NOT CACHED
Views ……………………………………………….. CACHED

Drivers …………………………………………………….
Broadcasting ……………………………………………. log
Cache …………………………………………………. file
Database ……………………………………………… mysql
Logs …………………………………………. stack / single
Mail ………………………………………………….. smtp
Queue …………………………………………………. sync
Session ……………………………………………….. file

this is controller where i am using that method:

namespace AppHttpControllers;

use AppHttpControllersController;
use IlluminateHttpRequest;
use AppHttpResourcesProvinces as ProvincesResourceCollection;
use AppHttpResourcesCities as CityResourceCollection;
use AppModelsProvince;
use AppModelsCity;
use IlluminateSupportFacadesAuth;
use AppModelsUser;
use AppModelsBook;

and this is where the Save() method undefined

public function shipping(Request $request)
    {
        $user = Auth::user();
        $status = 'error';
        $message = "";
        $data = null;
        $code = 200;
        if ($user) {
            $this->validate($request, [
                'name' => 'required',
                'address' => 'required',
                'phone' => 'required',
                'province_id' => 'required',
                'city_id' => 'required',
            ]);
            $user->name = $request->name;
            $user->address = $request->address;
            $user->phone = $request->phone;
            $user->province_id = $request->province_id;
            $user->city_id = $request->city_id;
            if ($user->save()) {       // this is the save method
                $status = "success";
                $message = "Update Shipping Success";
                $data = $user;
            } else {
                $message = "Update Shipping failed";
            }
        } else {
            $message = "User not found";
        }

        return response()->json([
            'status' => $status,
            'message' => $message,
            'data' => $data
        ], $code);
    }

this is my user model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;
use IlluminateSupportStr;
use IlluminateSupportFacadesAuth;

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasApiTokens;

    protected $fillable = [
        'name', 'email', 'password', 'username', 'roles', 'address',
        'city_id', 'province_id', 'phone', 'avatar', 'status', 'api_token'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function generateToken()
    {
        $this->api_token = Str::random(60);
        $this->saveOrFail();
        return $this->api_token;
    }

   
}

i hope my save method is not undefined again, in this case i was making bookstore

2

Answers


  1. You need to create an Instance For User Model then you are able to use that method.

    public function shipping(Request $request)
                {
                    $user = Auth::user();
                    $status = 'error';
                    $message = "";
                    $data = null;
                    $code = 200;
                    if ($user) {
                        $this->validate($request, [
                            'name' => 'required',
                            'address' => 'required',
                            'phone' => 'required',
                            'province_id' => 'required',
                            'city_id' => 'required',
                        ]);
                 
    
                        $user = User::where('id', auth()->user()->id)->update([
                            'name' => $request->name, 
                            'address' => $request->address
                            'phone' => $request->phone
                            'province_id' => $request->province_id
                            'city_id' => $request->city_id
                        ]);
                        if ($user) {       // this is the save method
                            $status = "success";
                            $message = "Update Shipping Success";
                            $data = $user;
                        } else {
                            $message = "Update Shipping failed";
                        }
                    } else {
                        $message = "User not found";
                    }
            
                    return response()->json([
                        'status' => $status,
                        'message' => $message,
                        'data' => $data
                    ], $code);
                }
    
    Login or Signup to reply.
  2. You should try this

    public function shipping(Request $request)
        {
            $user = Auth::user();
            $status = 'error';
            $message = "";
            $data = null;
            $code = 200;
            if ($user) {
                $this->validate($request, [
                    'name' => 'required',
                    'address' => 'required',
                    'phone' => 'required',
                    'province_id' => 'required',
                    'city_id' => 'required',
                ]);
                $user = User::find($user->id);
                $user->name = $request->name;
                $user->address = $request->address;
                $user->phone = $request->phone;
                $user->province_id = $request->province_id;
                $user->city_id = $request->city_id;
                if ($user->save()) {       // this is the save method
                    $status = "success";
                    $message = "Update Shipping Success";
                    $data = $user;
                } else {
                    $message = "Update Shipping failed";
                }
            } else {
                $message = "User not found";
            }
    
            return response()->json([
                'status' => $status,
                'message' => $message,
                'data' => $data
            ], $code);
        }
    

    Adding $user = User::find($user->id); line before assign values to model

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search