Search code examples
laraveleloquentrelationshipslumenlumen-5.2

Laravel populate foreign key data


Hi I am new to Laravel and trying to make a RESTFul api with Laravel Lumen I have installed the laravel and Lumen and I have the version as

Laravel Framework version Lumen (5.2.6) (Laravel Components 5.2.*)

Now I have in addition I have installed the https://github.com/jarektkaczyk/eloquence for mapping the DB column names and I have done so far as

Contacts model

namespace App;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence; // base trait
use Sofa\Eloquence\Mappable; // extension trait

class Contacts extends Model {
    use Eloquence, Mappable;
    protected $table = 'contacts';
    protected $primaryKey = 'idcontacts';
    protected $visible = ['firstname', 'lastname','idorganization'];
    protected $maps = [
        'firstname'=> 'firstName',
        'lastname' => 'lastName'
    ];

    public function organization() {
        return $this->hasOne('App\Organization','idorganization');
    }
}

Organizagion Model

namespace App;
use Illuminate\Database\Eloquent\Model;

class Organization extends Model {

    protected $table = 'organization';
    protected $primaryKey = 'idorganization';
    protected $visible = ['organization_name', 'website','website','phone','num_employes','industry'];

    public function contacts() {
        return $this->belongsTo('App\Contacts', 'idorganization');
    }
}

And the controller for the Contacts looks like

namespace App\Http\Controllers;
use App\Contacts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ContactsController extends Controller
{
    public function getAll() {
        $contacts = Contacts::all();
        return response()->json($contacts);
    }
}

But the response does not populate the idorganization with the model columns from organization.

I in addition tried as $contacts = Persona::with('organization')->all();

But it returns error as

Call to undefined method Sofa\Eloquence\Query\Builder::all()

If I remove the Sofa\Eloquence in Contacts model and the traits it still does not work.

Please let me know I am missing something obvious

Without the relation I get the response as

[
 {
    "firstname":"Abhik",
    "lastname":"Chakraborty",
    "idorganization":"1"
 },
 {
    "firstname":"John",
    "lastname":"Martin"
    "idorganization":"1"
 }
]

Expected result would be

[
     {
        "firstname":"Abhik",
        "lastname":"Chakraborty",
        "organization":{
          "organization_name": "foo"
          "website": "bar"
          ..................
        }
     },
     {
        "firstname":"John",
        "lastname":"Martin"
        "organization":{
          "organization_name": "foo"
          "website": "bar"
          ...............
        }
     }
 ]

Solution

  • The reason that your response does not populate the idorganization field when you call all() is that relationships are lazy loaded by default meaning that relationship data will be loaded when you actually access them.

    To accomplish what you want, you need to use get() method instead of all() when you are using with() method which is named as eager loading.

    Replace this:

    Persona::with('organization')->all();    
    

    With:

    Persona::with('organization')->get();