MDI
MDI

Reputation: 27

show the categories and project in view

my questions may be a bit of a beginner, but please be patient, thank you

I want to display the projects related to each category in the following code in the second foreach, but now all the projects are displayed

view

@foreach($categories as $category)
<div id="section-{{ $category->id }}" class="section">
    <h3 class="fnt_gh fnt-color">{{ $category->category_project }}</h3>
    <div class="bigBox">
        <div class="container1">
            @foreach($projects as $project)
            <article class="barghabi">
                <a href="#" data-cat="news" data-state="vic">
                    <img src="{{ $project->image_project }}" alt="">
                    <h6 class="fnt_gh" style="line-height: normal;">
                        {{ $project->title_project }}
                    </h6>
                </a>
            </article>
            @endforeach
            <div class="content"></div>
        </div>
    </div>
</div>
@endforeach

And a categories table and a project table whose models and relationships are as follows:

class Project extends Model
{
    protected $fillable = [
        'title_project' ,
        'period_project' ,
        'description_project' ,
        'manager_project' ,
        'phone_project' ,
        'email_project' ,
        'image_project'
    ];

    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

}

class Category extends Model
{
    protected $fillable = ['category_project' , 'icon_project'];

    public function projects()
    {
        return $this->belongsToMany(Project::class);
    }

}

and pivot :

Schema::create('category_project', function (Blueprint $table) {
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->unsignedBigInteger('project_id');
            $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
            $table->primary(['category_id' , 'project_id']);
            $table->timestamps();
        });

controller:

class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $categories = Category::all();

        $projects = Project::all();

        return view('home.fa.pages.project.index', compact('categories' , 'projects'  ));

    }
}

Upvotes: 0

Views: 109

Answers (1)

miken32
miken32

Reputation: 42709

You are calling @foreach($projects as $project) within your loop. You've defined $projects as all projects. Instead, use @foreach($category->projects as $project).

@foreach($categories as $category)
<div id="section-{{ $category->id }}" class="section">
    <h3 class="fnt_gh fnt-color">{{ $category->category_project }}</h3>
    <div class="bigBox">
        <div class="container1">
            @foreach($category->projects as $project)
            <article class="barghabi">
                <a href="#" data-cat="news" data-state="vic">
                    <img src="{{ $project->image_project }}" alt="">
                    <h6 class="fnt_gh" style="line-height: normal;">
                        {{ $project->title_project }}
                    </h6>
                </a>
            </article>
            @endforeach
            <div class="content"></div>
        </div>
    </div>
</div>
@endforeach

I'm not sure what's in $category->category_project but having a property with the same name as your pivot table is bound to create confusion. You should keep column names simple and readable, not append _project to each one!

Upvotes: 2

Related Questions