Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

I have Property model and in it I have relationships with few other tables. Here is an example for the relationship with the Status table:

public function propertyStatus()
{
    return $this->belongsTo('Status', 'status_id');  
}

If I make the followinq query:

$property = $this->property->
            with('propertyStatus', 'propertyCity', 'propertyMunicipality')->
            active()->
            where('id', '=', $id)->
            get()->toArray();

This query works fine, but it's returning all the rows from the Property, Status, City and Municipality tables.

If I go with this:

get(array('title', 'address'))

It's returning the array like

$property[0][title] and $property[0][address]

but I can't find a way to return the title, address and status only, since the status value is in another table. If I return all the rows, the array for Status is in this format:

$property[0][property_status][status]

What should I put in the get() method to get the status?

Last updated 2 years ago.
0

I forgot to mention that I've tried with propertyStatus.status, property_status.status, but with no result...

Last updated 2 years ago.
0

In this case where you are just retriving one single property model with all one-one relations, why are you lazy-loading ?

I mean, lazy loading or not, the number of queries will be anyway 4 !

If not can you give a view of your schema.

Last updated 2 years ago.
0

It eager loading, by the definition from here: http://laravel.com/docs/eloquent#eager-loading

Besides, the result that I'm returning to the user in through API and is in JSON format and that's why I need to select only certain columns and not all of them.

Last updated 2 years ago.
0

I'll redefine my question, in case I was not clear. This example is from the Query Builder documentation:

DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.id', 'contacts.phone', 'orders.price')
            ->get();

Can I select specific columns like that using Eloquent?

$this->users->
            with('contacts', 'orders')->
            get(array('users.id', 'contacts.phone', 'orders.price')->toArray();

I saw that it can be done using closure, but it can get messy if we have more joins using with().

Last updated 2 years ago.
0

I need that function as well. Would it be difficult to implement?

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

andrejmk andrejmk Joined 28 Mar 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.

© 2025 Laravel.io - All rights reserved.