Domanda

I have a ListingsController that extends BaseController which is fine, my app works. But now I intend to create a new ContactController and create a relation between it and ListingController.

Each listing should have 1 contact and there's a contact_id field in listings table.

The problem is, to use that, I need to extend my class to Eloquent and it's already extended to BaseController and if I change that my app crashes down.

Is there a solution for this?

Thanks.

È stato utile?

Soluzione

As SUB0DH said, relationships should be declared in the Model, not in the Controller, you'd want to have something like this in your Listing model:

public function contact()
{
    return $this->hasOne('Contact');
}

and in your Contact model you can link the contact to a listing by using

public function user()
{
   return $this->belongsTo('User');
}

Be sure to read http://laravel.com/docs/eloquent#one-to-one

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top