0

I have successfully created a few relations in a Model. This is the user-model

<?php 
  class User extends BaseModel{ 
    protected $table = 'users';

    public function group(){
        return $this -> belongsTo('UserGroup');
    }

    ...
  } 
?>

and this is the UserGroup-model (UserGroup.php)

<?php class UserGroup extends BaseModel{ ... } ?>

Every user can be in one group (the database-column is called 'group_id' in the users-table). If I want to do eager loading on this relation, it works perfectly fine, also for other models.

The problem is that I have a few models that have a lot of foreign keys and I don't want to create all relations manually. I want a function in the BaseModel that creates all those relations automatically, like

public function group(){
    return $this->belongsTo('Group');
}

based on an array that I would provide in each model, looking like this

protected $foreignKeys = array(array('key' => 'group', 'model' => 'UserGroup'), ...);

I have read that there is an array called 'with' that you can use, but it did not work for me. Somewhere else I read I should work with query scopes but I have no idea how that could help me.

Thanks for reading and your support!

Best regards, Marcel

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
molerat
  • 946
  • 4
  • 15
  • 43
  • 1
    Ardent provides something like you'd like https://github.com/laravelbook/ardent/blob/master/src/LaravelBook/Ardent/Ardent.php#L347-L350, but I would not necessarily suggest this approach. Maybe simply create more (abstract?) models that you'd extend? And to answer your concerns - neither scopes nor `with` does what you need. – Jarek Tkaczyk Jul 05 '14 at 13:29
  • Thanks for your help. I have already tried to work with the magic __call() method but then got an error that says, first() is not a defined in the Eloquent- class or so. I will try again with Ardents version and tell you if it worked, tomorrow. How would you do it with (abstract?) Models? – molerat Jul 05 '14 at 23:11
  • Hey @deczo, I read through Ardents class and it looks pretty much like what I want to have. All the validation stuff is nice, too. Why would you NOT suggest that approach? And how would you do it with models that would be extended? Thanks! – molerat Jul 06 '14 at 11:05
  • First off, I don't use Ardent because I find it not very flexible, when you need more than just base features of the Eloqeunt. To answer your question, provide some example models that you need with common relations. – Jarek Tkaczyk Jul 06 '14 at 11:21
  • For example, I could have a `FlightData`-model with following fields: `id, origin_airport, destination_airport, date, ...` and a lot of other fields, that actually are foreign keys, just as origin_airport and destination_airport are foreign keys for the `Airport`-model. I don't wanna create 15 functions that declare the relationship between the models, but simply have on array for that. – molerat Jul 06 '14 at 12:18
  • OK, that's not what I thought of. You might find Ardent-like solution better for your situation in the end. Having all the relations in an array presents more advantages to defining them as methods - you can search through those arrays if you need etc. So probably this is the way to go for you. – Jarek Tkaczyk Jul 06 '14 at 12:57
  • Okay thank you very much. I'll see if I can get it working with my system. You may post that Artend-class as an Answer, so that I can vote it/you up and make it easier to see for other users – molerat Jul 06 '14 at 13:34

1 Answers1

0

Ardent provides something like you'd want to implement: https://github.com/laravelbook/ardent/blob/master/src/LaravelBook/Ardent/Ardent.php#L347-L350.

As per comments:

You might find Ardent-like solution better for your situation in the end. Having all the relations in an array presents more advantages to defining them as methods - you can search through those arrays if you need etc. So probably this is the way to go for you

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157