0

I have a database, with 2 main tables :

etablissement
site

So i created 2 models

Site.php

class Site extends Eloquent {
    public function etablissement() {
        return $this ->hasOne('Etablissement','code_etablissement','code_site');
    }
}

Etablissement.php

class Etablissement extends Eloquent {
    public function sites() {
        return $this ->hasMany('Site', 'code_etablissement', 'code_etablissement');
    }

    public function etablissementCountSites() {
        return $this->sites->count();
    }
}

As you can see, here's how my relations are working :

"etablissement" can have multiples "site"

"site" can only have one "etablissement"

"etablissement" primary key is "code_etablissement"

"site" primary key is "code_site", having the foreign key "code_etablissement" referencing to "etablissement.code_etablissement"

I want to count how many "site" an "etablissement" have. This is why i created the function "etablissementCountSites()" above. So, in my controller, i am making this : HomeController.php

private function gen_accueil()
    {
        $etablissement = Etablissement::select('code_etablissement','nom')->from('etablissement')->orderBy('code_etablissement')->get();
        $sitesCounter = Etablissement::with('etablissementCountSites')->get();

        return View::make('accueil',  array('which_actif' => 0, 'etablissement' => $etablissement, 'compteurSites' => $sitesCounter));
    }

And in my blade page i have this : accueil_blade.php

@for ($i = 0 ; $i < count($etablissement); $i ++)
            <a class="btn popoverOption" data-content="Nombre de sites total : {{$compteurSites[$i]}}" rel="popover" data-original-title="{{$etablissement[$i]->code_etablissement}}" data-placement="bottom" href="{{ URL::to('dashboard_etablissement/' . $etablissement[$i]->code_etablissement)}}">{{$etablissement[$i]->nom}}</a><br>
@endfor

Everything should work... But i got this as error :

Table 'MYNET.etablissements' doesn't exist (SQL: select * from etablissements)

I searched all over the code, i don't have any "etablissementS" word anywhere. But i had previously. Is it possible that Laravel didn't refreshed this ?

Gui O
  • 363
  • 4
  • 8
  • 22
  • Set `$table` and `$primaryKey` properties on your models accordingly. Change wrong relation `hasOne` to `belongsTo`. – Jarek Tkaczyk Nov 19 '14 at 14:18
  • I added the protected variables primary key and table. and changed the relation to belongsTo I now have this : Call to a member function addEagerConstraints() on a non-object – Gui O Nov 19 '14 at 14:26
  • Because you're trying to load relation `with(etablissementCountSites)` while this is simple method, not a relation. Read this, there's the answeer for you http://stackoverflow.com/a/25665268/784588 – Jarek Tkaczyk Nov 19 '14 at 14:28
  • I used your 1:N way to do. But we're going for the same issue no ? i mean, everything seems alright, but how can i access the data for each "etablissement" ? You wrote :// then each post: $post->commentsCount; That would be the equivalent for : $sitesCounter->siteCount; But i can't access it in a blade page right ? – Gui O Nov 19 '14 at 14:59
  • Not at all. If you follow the instructions in that answer then you're good to go. You need `sitestCount()` relation and `getSitestCountAttribute()` accessor, that's all. – Jarek Tkaczyk Nov 19 '14 at 15:43

1 Answers1

0

Laravel when actually assume the table name if one is provided, and it does so by making plural of the model name. To your model, add:

class Establissment extends Eloquent 
{
    protected $table = 'thetablenamehere';
}

Also, naming a table as plural is considered best practise.

ollieread
  • 6,018
  • 1
  • 20
  • 36