Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.

quyle92 liked this thread

1

Can you post the raw sql query and the database?

Last updated 2 years ago.
0
string 'select * from `objects` where `id` = ?' (length=38)
string 'select `buildings`.*, `object_buildings`.`object_id` as `pivot_object_id`, `object_buildings`.`building_id` as `pivot_building_id`, `object_buildings`.`level` as `pivot_level`, `object_buildings`.`created_at` as `pivot_created_at`, `object_buildings`.`updated_at` as `pivot_updated_at` from `buildings` inner join `object_buildings` on `buildings`.`id` = `object_buildings`.`building_id` where `object_buildings`.`object_id` in (?)' (length=431)
string 'select * from `building_types` where `building_types`.`id` in (?, ?, ?)' (length=71)

What do you mean by database? Do you mean something like this?

# Dump of table building_types
# ------------------------------------------------------------

CREATE TABLE `building_types` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



# Dump of table buildings
# ------------------------------------------------------------

CREATE TABLE `buildings` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  `affecting_resource_id` int(10) unsigned NOT NULL,
  `building_type_id` int(10) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



# Dump of table object_buildings
# ------------------------------------------------------------

CREATE TABLE `object_buildings` (
  `building_id` int(10) unsigned NOT NULL,
  `object_id` int(10) unsigned NOT NULL,
  `level` int(10) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`object_id`,`building_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



# Dump of table objects
# ------------------------------------------------------------

CREATE TABLE `objects` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `object_type_data_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `location_id` int(10) unsigned NOT NULL,
  `size` int(10) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `interface` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Last updated 2 years ago.
0

What are the sql statements executed from your debugger?

  1. $buildings = Object::with('buildings.building_type')->where('id', '=', 1)->get();

  2. $test = Building::with('building_type')->get();

Last updated 2 years ago.
0

ehm, how can I figure that out?

Sorry, I'm no pro in laravel :)

Last updated 2 years ago.
0

Download and install the debugbar for laravel.

https://github.com/barryvdh/laravel-debugbar

Last updated 2 years ago.
0

Ah okay:

1)
select * from `objects` where `id` = '1'
select `buildings`.*, `object_buildings`.`object_id` as `pivot_object_id`, `object_buildings`.`building_id` as `pivot_building_id`, `object_buildings`.`level` as `pivot_level`, `object_buildings`.`created_at` as `pivot_created_at`, `object_buildings`.`updated_at` as `pivot_updated_at` from `buildings` inner join `object_buildings` on `buildings`.`id` = `object_buildings`.`building_id` where `object_buildings`.`object_id` in ('1')
select * from `building_types` where `building_types`.`id` in ('1', '2', '3')

2)

select * from `buildings`
select * from `building_types` where `building_types`.`id` in ('1', '2', '3')
Last updated 2 years ago.
0
  1. returns what you expected?

  2. what does this return? Is there an exception?

Last updated 2 years ago.
0

Yes, 2) returns the correct data and 1) returns "null" without an exception or something. I don't understand, why

Last updated 2 years ago.
0

I would try running each of these sub queries separately and seeing if you get your expected output. Also, sometimes laravel doesn't play well with composite primary keys or if you name a primary key named anything other than "id"

Last updated 2 years ago.
0

I tried running it seperately. That worked.

But the part with the many-to-many relationship works (and that's the part with the composite primary keys). The "building_type" part is a one-to-many relationship

I don't understand it, because the queries of 2) are exactly the same as of 1), at least the "building_type" part. It should output the same data, but larval doesn't connect it to the object I think :/

Last updated 2 years ago.
0

Just wondering if "object" is a reserved word?

Last updated 2 years ago.
0

Hm, that's possible, though it works well without the "building_type". I'll test it with another name

Edit: still doesn't work :/

Last updated 2 years ago.
0

Can you try removing this part?

where id = '1' select buildings.*, object_buildings.object_id as pivot_object_id, object_buildings.building_id as pivot_building_id, object_buildings.level as pivot_level, object_buildings.created_at as pivot_created_at, object_buildings.updated_at as pivot_updated_at from buildings inner join object_buildings on buildings.id = object_buildings.building_id where object_buildings.object_id in ('1')

and try using Object::find(id)

Last updated 2 years ago.
0

Hm, it still returns "null"

Last updated 2 years ago.
0

Can we see what values you have in the database?

Last updated 2 years ago.
0

objects table objects tabls

object_buildings object_buildings

buildings buildings

building_types building_types

Last updated 2 years ago.
0

There is an error in you sql statement. I think you are referencing the pivot table incorrectly.

http://daylerees.com/codebright/eloquent-relationships

Last updated 2 years ago.
0

Ah yes, sorry forgot something. I tried out something. I deleted some entries in the database to check if it would change anything if I recreate the data. That's the new query with those entries:

select * from `objects` where `objects`.`id` = '1' limit 1

select `buildings`.*, `object_buildings`.`object_id` as `pivot_object_id`, `object_buildings`.`building_id` as `pivot_building_id`, `object_buildings`.`level` as `pivot_level`, `object_buildings`.`created_at` as `pivot_created_at`, `object_buildings`.`updated_at` as `pivot_updated_at` from `buildings` inner join `object_buildings` on `buildings`.`id` = `object_buildings`.`building_id` where `object_buildings`.`object_id` in ('1')

select * from `building_types` where `building_types`.`id` in ('1')

Edit: Hm, I've done the some as I've done with other tables. And they always worked. Only if I try nested relationships within "with()" methods it doesn't work.

I did not write something like this in the Schema: $table->foreign('artist_id')->references('id')->on('artists');

But I think this is not necessary, or is it?

Last updated 2 years ago.
0

I believe using foreign keys is necessary.

Last updated 2 years ago.
0

http://stackoverflow.com/questions/18717/are-foreign-keys-really-necessary-in-a-database-design

It should work without, though I think I should use them. But I should work anyways

Edit: I now added the foreign keys to the database. It still doesn't work :(

Last updated 2 years ago.
0

Oh my god, I found my mistake. I tried to access it like this

$buildings = Object::with('buildings.building_type')->where('id', '=', 1)->get();

But of course it must be:

$object = Object::with('buildings.building_type')->findOrFail(1);
dd($object->buildings[0]->building_type->name);

Now it works. I was just stupid :D

But thank you very much for your help ! :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

d3mueller d3mueller Joined 29 Aug 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.

© 2024 Laravel.io - All rights reserved.