Support the ongoing development of Laravel.io →
Configuration Database Eloquent

I'm not using the auto increment for the id instead i'm using the 32 char unique id. So when i create a relationship and query, im getting a null because my FK expecting int

my models

class User extend Eloquent {
    public $incrementing = false; 
}

class Reservation extend Eloquent {
    public $incrementing = false; 
}

so when i query this

$reservations = Reservation::with('user')->where('user_id', '=', '22beb4892ba944c8b1895855e1d4d1ad')->get();

i could not retrieve the users information but the reservations info is working fine

when i try to listen for query. eg:

Event::listen('illuminate.query', function($query, $bindings, $time, $name){
	var_dump($query);
	var_dump($bindings);
});

i get this

string(46) "select * from `reservation` where `user_id` = ?"
array(1) {
  [0]=>
  string(36) "22beb4892ba944c8b1895855e1d4d1ad"
}
string(53) "select * from `user` where `user`.`id` in (?)"
array(1) {
  [0]=>
  int(0)
}

the problem is in the second query i could not retrieve the users info because the user.id expecting int.

Last updated 2 years ago.
0

is there a reason you don't want to use integers? your app can still work with (string) user_id if you want to, just create a new column and index it, let Laravel handle the relationship stuff with the integers.

Last updated 2 years ago.
0

auto increment is fine for me when i will make new app. the problem is the database is existing and we share the database with vfp desktop app and were planning to make a web app. sometimes there is a need for primary keys to be less predictable. =)

Last updated 2 years ago.
0

did you try this

// Reservation model
return $this->belongsTo('User', 'foreign_key', 'local_key');
Last updated 2 years ago.
0

FYI

DB::getQueryLog(); // array of executed queries
Last updated 2 years ago.
0

zenry said:

did you try this

// Reservation model return $this->belongsTo('User', 'foreign_key', 'local_key');

actually my models are config like this

class User extend Eloquent {
    public $incrementing = false; 

    public function reservations()
    {
        return $this->hasMany('Reservation', 'user_id');
    }
}

class Reservation extend Eloquent {
    public $incrementing = false; 

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

am i doing it wrong?

i get this log

[ {"query":"select * from `reservation` where `user_id` = ?","bindings":["22beb4892ba944c8b1895855e1d4d1ad"],"time":0.63},
{"query":"select * from `user` where `user`.`id` in (?)","bindings":[0],"time":0.45} ]

from your

DB::getQueryLog(); 
Last updated 2 years ago.
0

wtf! im so dumb! just need to put the 2nd param on my $this->belongsTo('User')

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

thanks for helping! cheers!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jrsalunga jrsalunga Joined 25 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.