Support the ongoing development of Laravel.io →
Database Eloquent Views

Hi

I'm using Laravel 4 I have a section on my site that displays my portfolio, The problem I'm having is that I can't seem to back track.

This is how the portfolio section works. I have 3 categories: All, Colour and BW, Each category has its own portfolio images All: Portfolio 1 Portfolio 2 Portfolio 3 Portfolio 4 Portfolio 5

Color: Portfolio 3 Portfolio 4 Portfolio 5

Black & White: Portfolio 1 Portfolio 2

So far I've only managed to display all the categories with the respective portfolio image, but what I would like is for only the All to be displayed and if I click on Color then only the Color portfolio images get displayed and the same for Black & White.

pcategory.blade.php

<div class="row">
    <div class="portfolio_gallery">
            @foreach($content->pcategory as $pcat)
                
                <h1 style="text-align: center;">{{ $pcat->title }}</h1>
                @include('portfolio::portfolio')
            @endforeach
            

    </div>
</div>

Here the section lists all the categories and includes the portfolio page

portfolio.blade.php

@foreach($pcat->portfolio as $portfolio)
    @if($portfolio)
    <div class="portfolio_title">
        {{ $portfolio->title }}
    </div>
@endforeach

Here the portfolio page lists the portfolio titles depending on the category it falls under.

So I would like to do something like if $portfolio->title is == A specific category then display else display none.

I tried to use this plugin http://demo.phapsu.com/jquery.elastic_grid/index.php#demo

but I wasn't able to make a loop that did what I needed it to. I've tried something like this

<?php 
        echo "<PRE>";
        print_r($portfolio->pcategory()->title);
        die();
    ?>

but I just get this error:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$title

I hope I explained everything clearly.

Last updated 3 years ago.
0

I really didn't get what you're actually trying to do and if it's really a Laravel or more a JavaScript issue.

But the error means you're trying to access a Model property while accessing actually a relationship instance.

If you use

$portfolio->pcategory->title

you're accessing the pcategory Model itself (so it will be loaded from the database and you can get its values like title and so on).

If you use

$portfolio->pcategory()

you're accessing the relationship between the two models - which is also a queryBuilder object. That means you can do something like

$portfolio->pcategory()->whereTitle('Hello')->get()

So if you remove the () from your example the error should be gone and something should happen. Unfortunately I have no clue if that's what needs to happen for you ;)

0

What I'm trying to do is get all the categories to show up depending on the portfolio.

so for example. I have portfolio 1 and that portfolio belongs to the cats category.

So I made a list of my portfolios and I need the category that belongs to them.

Does that help?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiva shiva Joined 24 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.