Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

I'm trying to pass two sets of variables into my View and failing miserably...

I need to pass two entities into my view. Both of which work fine when I retrieve them one at at time, but I'm blowing it somewhere when I try to do both.

Here's the function I'm wrestling with in my Controller and my attempt to pass both sets of data into my route simultaneously:

	public function professionals($id) {
		$pages = Page::findOrFail($id);
		$pros = Professional::sortable()->orderBy('lname', 'asc')->paginate(25);
		return view('pros', compact('pros'))->with('pages', compact('pages'));
	}

Open in new window


Here's my view:

@extends('layouts.satellite')
@section('content')
<!-- Section: intro -->
<section id="intro" class="intro">
	<div class="satellite-content">
		<div class="container">
			<div class="row">
				<div class="col-xs-12 satellite_row">
					<?php echo $pages->body; ?>
					<table class="table table-bordered">
						<tr>
						<td style=" background-color:#ea7fea; color:#fff; text-align:center; font-weight:bold;">#</td>
							<td style=" background-color:#ea7fea; color:#fff; text-align:center; font-weight:bold;">@sortablelink('lname', 'Name') </td>
							<td style=" background-color:#ea7fea; color:#fff; text-align:center; font-weight:bold;">@sortablelink('discipline', 'Discipline')</td>
							<td style=" background-color:#ea7fea; color:#fff; text-align:center; font-weight:bold;">@sortablelink('country', 'Country')</td>
							<td style=" background-color:#ea7fea; color:#fff; text-align:center; font-weight:bold;">@sortablelink('cert_year', 'Certified')</td>
						</tr>
						<?php $count=1;?>
						@foreach($pros as $pro)
							<tr>
								<td>{{ $count }}</td>
								<td>{{ $pro->lname }}, {{ $pro->fname }}</td>
								<td>{{ $pro->discipline }}</td>
								<td>{{ $pro->country }}</td>
								<td>{{ $pro->cert_year }}</td>
							</tr>
						<?php $count=$count+1;?>
						@endforeach		
						<tr>
							<td colspan="5" style="text-align:center;">{{ $pros->links() }}</td>
						</tr>
					</table>	
				</div>
			</div>
		</div>
	</div>
</section>
@endsection

Open in new window


This is the error I'm getting:

Trying to get property 'body' of non-object (View: C:\wamp64\www\new_nomas\resources\views\pros.blade.php)

What am I missing?
LaravelPHP

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
Julian Hansen

$pros = Professional::sortable()->orderBy('lname', 'asc')->paginate(25);

Open in new window

Why compact() - are you sure that does what you  think it does?

Why not  jus
return view('pro', $pros)->with('pages', compact('pages'));

Open in new window


NB
Just focusing on the $pros data for now - not sure that the compact('pages') is right either.
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Stanyon

If you really want to use compact, then this the correct way to do it:

return view('pros', compact('pages', 'pros'));
Bruce Gust

ASKER
Thanks, guys!

I got it!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23