0

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?

shaedrich
  • 5,457
  • 3
  • 26
  • 42
alban
  • 55
  • 1
  • 6
  • Does this answer your question? [Property \[title\] does not exist on this collection instance](https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance) – ParisaN Jun 24 '21 at 10:44

2 Answers2

0

Change your code to

public function show($id)
{
    $faculty = Etablissement_filiere::where('id_etablissements','=',$id)->first();
    return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
0

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.

wunch
  • 1,092
  • 9
  • 12
  • Ok I modified my controller as you suggested and it works. But it does not display the price of the training and the support for each faculty that is in the pivot table `etablissement-filieres`. I tried to solve the problem by doing this in my controller : `$school = Etablissement_filiere::findOrFail($id);` and my view blade : `{{ $school->prix}}` and `{{ $school->prise_en_charge}}`. But it displays the same price and the same support for the first faculty on all other faculties of the school. What does this do ? – alban Jun 24 '21 at 13:32
  • First, in your School model, you specify that you want to also retrieve pivot columns whenever you retrieve faculty, i.e. `return $this->belongsToMany('App\Models\Filiere','etablissement_filieres','id_etablissements','id_filieres')->withPivot('prise_en_charge', 'prix');`. Then, to retrieve data stored in the pivot table, if you loaded the faculty as I mentioned in my answer, in your view you just do `$faculty->pivot->prix`. ( See the section "Retrieving Intermediate Table Columns" in the docs https://laravel.com/docs/7.x/eloquent-relationships#many-to-many ). – wunch Jun 24 '21 at 14:46
  • it works ! i just changed `$faculty->pivot->prix` by `$filiere->pivot->prix` while following suggestion. – alban Jun 24 '21 at 15:29