Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0
//User
$table->increments('id'); // -> integer('id')->unsigned();

so change this:

//Board
// $table->bigInteger('owner_id')->unsigned(); to:
$table->integer('owner_id')->unsigned();
Last updated 2 years ago.
0

Thanks. It's now integer, but I'm still getting NULL when I run var_dump(User::find(1)->boards);. The SQL output hasn't changed, either.

Last updated 2 years ago.
0

run DB::getQueryLog(); after fetching what you wanted, this will return all the queries and we can examine what's not working then.

Last updated 2 years ago.
0

OK. I've got…

var_dump(User::find(1)->boards);
var_dump(DB::getQueryLog());

which outputs…

NULL
array(1) { [0]=> array(3) { ["query"]=> string(44) "select * from `users` where `id` = ? limit 1" ["bindings"]=> array(1) { [0]=> int(1) } ["time"]=> float(0.52) } }
Last updated 2 years ago.
0

The only thing I can think of is that you mistyped boards somewhere. There is no other way that Eloquent returns NULL from any relation. Check again User model and the relation definition.

Last updated 2 years ago.
0

It turns out I'd renamed the original User.php to User_.php, which was being loaded instead of my new User.php file. Once I deleted the original and did dump-autoload, it worked perfectly!

array(2) { [0]=> array(3) { ["query"]=> string(44) "select * from `users` where `id` = ? limit 1" ["bindings"]=> array(1) { [0]=> int(1) } ["time"]=> float(0.46) } [1]=> array(3) { ["query"]=> string(52) "select * from `boards` where `boards`.`owner_id` = ?" ["bindings"]=> array(1) { [0]=> int(1) } ["time"]=> float(0.31) } }

Thanks for your help!

Last updated 2 years ago.
0

For those that might come later, if you have camel case table names, laravel will convert it to snake case if you don't input the column name in the relationship declaration. Hope this can help someone!

0

Morialkar said:

For those that might come later, if you have camel case table names, laravel will convert it to snake case if you don't input the column name in the relationship declaration. Hope this can help someone!

THX!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2024 Laravel.io - All rights reserved.