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.
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.
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. =)
did you try this
// Reservation model
return $this->belongsTo('User', 'foreign_key', 'local_key');
FYI
DB::getQueryLog(); // array of executed queries
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();
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!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community