Isis
Isis

Reputation: 189

Getting all data through has many relationship in laravel

I'm trying to get all the data in my table from another table depending on my title. If I do this

$single_portfolio = Pcategory::with('portfolio')->where('title', $title)->first();
$portfolio_title = $single_portfolio->portfolio->first()->title;

I only get the first item in the database. I've tried

$portfolio_title = $single_portfolio->portfolio->get()->title;

but that didn't work.

I got this error

Missing argument 1 for Illuminate\Support\Collection::get(), called in C:\xampp\htdocs\bellamage-cakes\app\Modules\Open\Http\Controllers\OpenController.php on line 70 and defined

Upvotes: 1

Views: 238

Answers (1)

Isis
Isis

Reputation: 189

I've got it. This is what I did

I changed

$single_portfolio = Pcategory::with('portfolio')->where('title', $title)->first();

to

$single_portfolio = Pcategory::where('title', $title)->firstOrFail();

and removed

$portfolio_title = $single_portfolio->portfolio->first()->title;

Then in my blade template I did this

@foreach($single_portfolio->portfolio as $category)
    {!! $category->title !!}
@endforeach

and it worked.

Upvotes: 2

Related Questions