Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Respect's avatar

relationship hasMany() dosen't work

= Thanks For Help = Hello friends Why hasMany doesn't work after i uploaded project in production with user my code = Everything Works In local = Note = I created these tables in production manually without migration what i forget

// User.php

    public function ocazioncart()
    {
        return $this->hasMany('App\Models\OcazionCart', 'user_id', 'id');
    }

// OcazionCart.php

    public function user()
    {
        return $this->belongsTo('App\User');
    }

// table fields

            $table->increments('id');
            $table->unsignedInteger('user_id'); // auth user id

            $table->morphs('cartable');  // with product model

            $table->timestamps();
        
// NOTES
1- auth()->user()->ocazioncart->count(); // Work in local Only 
2- auth()->user()->ocazioncart // In production return Null so with ->count blowup page
3- OcazionCart::where('user_id',auth()->user()->id)->get(); // Works In production 
4 - I created OcazionCart table Manually in host what i missed 
// Thanks For Help
0 likes
9 replies
mvd's avatar

Hi @respect

Maybe a stupid question but you are logged in with a user on production?

1 like
edwardpalen's avatar

Hello @respect.

Why do not you structure the migration in your local environment and define the relationship in your migration file and then update your repository in production?

This way you keep your changes versioned and tested.

2 likes
Respect's avatar

@MVD - Hello mr mvd Yas i'm loged in when i use this works

OcazionCart::where('user_id',auth()->user()->id)->get(); // Works In production 

Respect's avatar

@EDWARDPALEN - Hello mr edwardpalen thanks for trying help . i local environment everything works 100% but in production code same structure same but dosent work Only This Syntax works

OcazionCart::where('user_id',auth()->user()->id)->get(); // Works In production 

edwardpalen's avatar

@RESPECT - Ok, first in which production host is your application running?

I ask you, because the structure of the application must remain integrated into production.

1 like
tokoiwesley's avatar

I created these tables in production manually without migration what i forget

@respect editing code directly in production is a terrible idea. I suggest you write the code in your local environment and test it before moving/deploying it to production.

2 likes
Respect's avatar

@EDWARDPALEN - Hell0 Mr. edwardpalen I using godaddy and already finsihed the rest of application but this realthionsip only do the error i dont know why

Respect's avatar

@TOKOIWESLEY - Hello MR.tokoiwesley there are many data in production version and finsihed model no need to run migration there may be lose the table data

tokoiwesley's avatar

@RESPECT - Then maybe you are using migrations the wrong way. They are meant exactly for this type of job in the first place - easily modifying tables in production.

Please or to participate in this conversation.