quyle92 liked this thread
Can you post the raw sql query and the database?
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;
What are the sql statements executed from your debugger?
$buildings = Object::with('buildings.building_type')->where('id', '=', 1)->get();
$test = Building::with('building_type')->get();
ehm, how can I figure that out?
Sorry, I'm no pro in laravel :)
Download and install the debugbar for laravel.
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')
returns what you expected?
what does this return? Is there an exception?
Yes, 2) returns the correct data and 1) returns "null" without an exception or something. I don't understand, why
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"
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 :/
Just wondering if "object" is a reserved word?
Hm, that's possible, though it works well without the "building_type". I'll test it with another name
Edit: still doesn't work :/
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)
Can we see what values you have in the database?
objects table
object_buildings
buildings
building_types
There is an error in you sql statement. I think you are referencing the pivot table incorrectly.
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?
I believe using foreign keys is necessary.
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 :(
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 ! :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community