mardi 30 juin 2020

Laravel - Relations - Why works this way of accessing properties?

I've got a question regarding the laravel framework (version 5.2). This is not my code, but I need to understand why and how this works for a customer. I do not understand why I can access properties of a model the following way. I have a controller, which opens a blade template (view) this way:

return view("elements.base")
       ->with("html", getTypo3HtmlTemplate(view('Modul.'.$modul->getOriginal('bezeichnung'))
       ->with('modul',$modul)
       ->with('isCustomer', ($modulLaufzeit != null || \Auth::user()->benutzerFunktion_FK == 2 ? 1 : 0))
        ->render(),$modul->bezeichnung));

As you can see there are only 2 parameters given (modul and isCustomer). modul is model coming from the database.

The model class looks like this:

<?php
class Modul extends \App\Models\Base\BaseModel
{
    public $table = "tbl_Modul";

    public function __construct()
    {
        $this->relationMethods[] = "felder";
    }

    public function getBezeichnungSpecialChars($loadGermanLanguage = false) {
        ...
    }

    public function getBezeichnungSpecialCharsNonHTML($loadGermanLanguage = false) {
        ...
    }

    private function getKostenpflichtigeModule() {
        return $this->query()->where('paket', 0)->where('modulKostenlos',0)->where('inEntwicklung',0)->get();
    }

    public function getModulPaketbezeichnung($html = true, $loadGermanLanguage = false) {
        ...
    }

    public $laufzeit = 0;

    public function felder()
    {
        return $this->hasMany(\App\Models\Feld\Feld::class,"modul_fk");
    }

    public function zulassung()
    {
        return $this->hasOne(\App\Models\Zulassung\Zulassung::class,'id','zulassung_fk');
    }

    public function vorschrift()
    {
        return $this->hasOne(\App\Models\Vorschrift\Vorschrift::class,'id','vorschrift_fk');
    }

    public function demo()
    {
        return $this->hasOne(\App\Models\Modul\ModulDemo::class,'id','modul_demo_fk');
    }

    public function demokostenlosebemessung()
    {
        return $this->hasMany(\App\Models\Modul\ModulDemoKostenloseBemessung::class, 'modul_fk');
    }

    public function modulpreise()
    {
        return $this->hasMany(\App\Models\Modul\ModulPreis::class,"modul_fk");
    }

    public function version()
    {
        return $this->hasMany(\App\Models\Modul\ModulVersion::class, 'modul_fk');
    }

    public function formeln(){
        return $this->hasMany(\App\Models\Formel\Formel::class, 'modul_fk');
    }
}

Now back to my initial question. In the blade template (view) the following code is being used:

@foreach ($feld_holzart->inhaltsTypen as $inhaltsTyp)
   ...
@endforeach

How can a variable $feld_holzart being used, when it is not given explicitly by the controller to the view? I thought, it only can be used this way:

@foreach ($modul->feld->holzart->inhaltsTypen as $inhaltsTyp)

I know there is a relation to felder, but nott to feld. I can't get it, how this works!? Can anybody explain to me how this is working? If you need more info, please let me know. I will try to provide these as soon as possible. Many, many thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire