We help IT Professionals succeed at work.

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

Bruce Gust
Bruce Gust asked
on
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?
Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
$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.
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Hey Bruce,

Easiest way is to just pass in an array:

$data = [
    'pages' => $pages,
    'pros' => $pros,
];

return view('pros', $data)

Open in new window

Now you template will have access to $pages and $pros :)

compact will create an array, so this line:

with('pages', compact('pages')).

is effectively the same as this:

with('pages', ['pages' => $pages])

which means that your template will receive pages with a key of pages, so you'd have to access it like pages->pages which doesn't make a lot of sense
Most Valuable Expert 2018
Distinguished Expert 2019

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

return view('pros', compact('pages', 'pros'));
Bruce GustPHP Developer

Author

Commented:
Thanks, guys!

I got it!