Aid Aid
Aid Aid

Reputation: 91

how to display the annonces of each category?

i have two table categories and annonces between them hasMany relationship belongsTo, each category has several annonces, how to display for each category the annonces corresponds, in my example it displays all the annonces.

landing-page.blade.php

<div id="simplelastads">
         <h2>Les dernières annonces</h2>
         <div class="contentads">
          @foreach($categories as $category)
          <div class="simpleadsemploi">
          <h3><a href="#" title="Offre emploi">{{ $category->name }}</a></h3>
            @foreach($annonces as $annonce)
               <div class="tab_left">
                 <div class="li-child">
                   <a href="#" title="Technicien">{{ $annonce->titre }}</a>
                   <span>{{ $annonce->ville }}({{ $annonce->hay }})</span>
                 </div>
                 <div class="readmore"><a href="#" title="Voir plus...">Voir plus...</a></div>
               </div>
            @endforeach  
          </div>
          @endforeach
        </div>
      </div> 

Annonce.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Annonce extends Model
{
    public function category() {
        return $this->belongsTo('App\Category');
    }

    }

Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
     protected $guarded = [];

    protected $table = 'category';

    public function annonces(){
        return $this->hasMany('App\Annonce');
    }

}

LandingPageController.php

 public function index()
    {
        $categories = Category::all();
        $annonces = Annonce::all();
        
        return view('landing-page')->with(
            [
            'categories'=> $categories,
            'annonces'  => $annonces
        ]);
    }

Upvotes: 1

Views: 108

Answers (1)

N69S
N69S

Reputation: 17216

Correct the controller and the view like this

landing-page.blade.php

<div id="simplelastads">
    <h2>Les dernières annonces</h2>
    <div class="contentads">
        @foreach($categories as $category)
            <div class="simpleadsemploi">
                <h3><a href="#" title="Offre emploi">{{ $category->name }}</a></h3>
                @foreach($category->annonces as $annonce)
                   <div class="tab_left">
                     <div class="li-child">
                       <a href="#" title="Technicien">{{ $annonce->titre }}</a>
                       <span>{{ $annonce->ville }}({{ $annonce->hay }})</span>
                     </div>
                     <div class="readmore"><a href="#" title="Voir plus...">Voir plus...</a></div>
                   </div>
                @endforeach  
            </div>
        @endforeach
    </div>
</div> 

Use the eager loading for the annonces to improve performance with('annonces')

LandingPageController.php

public function index()
{
    $categories = Category::with('annonces')->get();
            
    return view('landing-page')->with([
        'categories'=> $categories
    ]);
}



Upvotes: 2

Related Questions