Search code examples
laravelforeacheloquentlaravel-bladelaravel-7

Laravel - Property [filieres] does not exist on this collection instance


I have a little one with my school app. I want to display all the faculties of a school. Indeed in my DB a school can have one or more faculties and a faculty can belong to one or more schools.

Model School:

public function filieres ()
{
    return $this->belongsToMany('App\Models\Filiere','etablissement_filieres','id_etablissements','id_filieres');
}
public function etablissement_filieres(){
    return $this->hasMany('App\Models\Etablissement_filiere');
  }
protected $fillable = [
    'nom', 'image', 'ville', 'adresse', 'contact_1', 'contact_2',
    'email', 'logo', 'presentation', 'brochure', 'localisation',
];

Model table pivot Etablissement_filiere:

public function filiere(){
    return $this->belongsTo('App\Models\Filiere');
  }
protected $fillable = [
    'id', 'id_etablissements', 'id_filieres', 'prise_en_charge', 'prix',
];

Model Filiere:

public function etablissements ()
{
    return $this->belongsToMany(Etablissement::class,'etablissement_filiere');
}
protected $fillable = [
    'nom', 'diplome_requis', 'diplome_obtenu', 'prix', 'duree', 'type',
];

Controller:

public function show($id)
{
    $faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get();
    return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}

Blade view:

@foreach($faculty->filieres as $filiere)
          <div class="container bg-fil py-4 my-5">       
            <div class="row pl-5">
              <div class="col-md-9">
                <h6 class="font-weight-bold">{{ $filiere ->nom}} <br>
                <span class="text-primary"> {{ $filiere ->diplome_obtenu}}</span></h6>
              </div>
              <div class="col-md-3 pt-n5">
                <img src="{{asset($etablissement->image)}}" alt="">
              </div>
            </div>
            <div class="row pl-5 mt-md-n5">
              <div class="col-md-6">
                <h6> <strong> Diplôme réquis</strong>: {{ $filiere ->diplome_requis}} <br>
                    <strong>Durée</strong>: {{ $filiere ->duree}} <br>
                    <strong>Montant de la formation</strong>: {{ $etablissement_filieres ->prix}}</h6>
              </div>
              <div class="col-md-6">
                 <h6> <strong> Mode d'etude</strong>: {{ $filiere ->type}} <br>
                 <strong>Prise en charge</strong>: {{ $etablissement_filieres ->prise_en_charge}}</h6>
              </div>
            </div>
            <div class="row pl-5 mt-4">
              <div class="col-md-6">
                <a href="{{ route('inscription') }}" class="btn btn-success font-weight-bold w-75 now">INSCRIVEZ VOUS MAINTENANT</a>
              </div> 
            </div>
          </div>
          @endforeach

I am trying to display all the faculties in the school but I have this error:

Property [filieres] does not exist on this collection instance.

Can you tell me where the error is preventing?


Solution

  • Your immediate view error is when it tries to evaluate $faculty->filieres.

    Calls to something like Model::where()->get() return a Collection instance (which is like an iterable group) of Models, not a single Model. Even if that collection only contains one model, it's still a group and not the model itself.

    So, $faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get(); returns a Collection, not a single model. A single model may have a filieres property, but the Collection instance (which itself is a class in Laravel) does not. So, if what you want is the first result only, and not a collection, end that line with ->first() instead of ->get().

    However even if you fix that, you also have a second problem. You're getting an instance of the pivot model Etablissement_filiere, and then trying to access the filieres property on it (which it doesn't have). Instead, you should be getting an instance of the School model. If you've defined your relations correctly, Laravel will take care of finding the pivot for you, so you normally wouldn't access a pivot model directly unless you had a particular reason to. So, instead try this in your controller:

    public function show($id)
    {
        $establissement = Etablissement::find($id);
        $faculty = $establissement->filieres;
    
        return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissement'));
    }
    

    and then in your view:

    @foreach($faculty as $filiere)
    

    There are cleaner ways to do this (e.g. look into some tutorials on route-model binding), but that should at least solve the error for your current code.