1
我沒有使用自動增量的ID,而是使用32個字符的唯一ID。所以,當我創建一個關係查詢,即時得到一個空的,因爲我的FK期待INT 我的模型Laravel:如何將主鍵和外鍵設置爲字符串
class User extend Eloquent {
public $incrementing = false;
}
class Reservation extend Eloquent {
public $incrementing = false;
}
所以,當我查詢這個
$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);
});
我得到這個
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)
}
問題是在第二個查詢我無法檢索用戶信息,因爲user.id期待int。
太棒了!有用!只需要從我的$ this-> belongsTo('User')'中輸入第二個參數'user_id'。非常感謝! – jrsalunga