Bruce Gust
asked on
Laravel 6 -> Retrieving All Records -> "undefined constant" WHY?
Ninjas!
I'm using Laravel 6 which is becoming more and more significant as I'm working through a Laravel 5 tutorial.
In any event, here's my Controller:
...and here's my View:
I get an error that says:
Undefined variable: pages (View: C:\wamp64\www\new_nomas\re sources\vi ews\admin\ listPages. blade.php)
I've played with a couple of different things, including this on my Controller
And in that scenario, I get this error (pertaining to my Controller):
Use of undefined constant pages - assumed 'pages' (this will throw an Error in a future version of PHP)
What am I missing?
I'm using Laravel 6 which is becoming more and more significant as I'm working through a Laravel 5 tutorial.
In any event, here's my Controller:
public function index()
{
//get pages
$pages = Page::all();
return view('admin/listPages', compact($pages));
}
...and here's my View:
@foreach($pages as $page)
<tr>
<td>{{ $page->name }}</td>
</tr>
@endforeach
I get an error that says:
Undefined variable: pages (View: C:\wamp64\www\new_nomas\re
I've played with a couple of different things, including this on my Controller
public function index()
{
//get pages
$pages = Page::all();
return view('admin/listPages', [b]compact(pages))[/b];
}:
And in that scenario, I get this error (pertaining to my Controller):
Use of undefined constant pages - assumed 'pages' (this will throw an Error in a future version of PHP)
What am I missing?
ASKER
Hey, Chris!
Does "compact" not do what you have referenced as far as ['pages' => $pages]?
I'm asking that because that's what the tutorial appears to advocate, and yet I get the error that I mentioned.
Here's what I'm attempting to digest...
So, there's that.
Now, when I implement your solution, I get closer to where I need to be, but I'm still coming up short.
Here's my Controller:
Here's my View:
And here's what the page looks like:
So it would seem that the page "senses" data, but I'm not able to print anything based on the syntax I'm currently using.
One more thing...
When I do a var_dump here:
code]<table class="table">
<?php var_dump($pages); ?>
@foreach($pages as $page)
<tr>
<td>{{ $page->name }}</td>
</tr>
@endforeach
</table>[/code]
I get this:
I'm looking for "title" or "body," and I don't see squat.
What do you think?
Does "compact" not do what you have referenced as far as ['pages' => $pages]?
I'm asking that because that's what the tutorial appears to advocate, and yet I get the error that I mentioned.
Here's what I'm attempting to digest...
So, there's that.
Now, when I implement your solution, I get closer to where I need to be, but I'm still coming up short.
Here's my Controller:
public function index()
{
//get pages
$pages = Page::all();
return view('admin/listPages', ['pages'=>$pages]);
}
Here's my View:
<table class="table">
<?php var_dump($pages); ?>
@foreach($pages as $page)
<tr>
<td>{{ $page->name }}</td>
</tr>
@endforeach
</table>
And here's what the page looks like:
So it would seem that the page "senses" data, but I'm not able to print anything based on the syntax I'm currently using.
One more thing...
When I do a var_dump here:
code]<table class="table">
<?php var_dump($pages); ?>
@foreach($pages as $page)
<tr>
<td>{{ $page->name }}</td>
</tr>
@endforeach
</table>[/code]
I get this:
C:\wamp64\www\new_nomas\storage\framework\views\0798441722c872e8f128792763c11a29231416be.php:26:
object(Illuminate\Database\Eloquent\Collection)[296]
protected 'items' =>
array (size=3)
0 =>
object(App\Page)[297]
protected 'fillable' =>
array (size=3)
...
protected 'connection' => string 'mysql' (length=5)
protected 'table' => string 'pages' (length=5)
protected 'primaryKey' => string 'id' (length=2)
protected 'keyType' => string 'int' (length=3)
public 'incrementing' => boolean true
protected 'with' =>
array (size=0)
...
protected 'withCount' =>
array (size=0)
...
protected 'perPage' => int 15
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
protected 'attributes' =>
array (size=6)
...
protected 'original' =>
array (size=6)
...
protected 'changes' =>
array (size=0)
...
protected 'casts' =>
array (size=0)
...
protected 'dates' =>
array (size=0)
...
protected 'dateFormat' => null
protected 'appends' =>
array (size=0)
...
protected 'dispatchesEvents' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'relations' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
public 'timestamps' => boolean true
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
1 =>
object(App\Page)[298]
protected 'fillable' =>
array (size=3)
...
protected 'connection' => string 'mysql' (length=5)
protected 'table' => string 'pages' (length=5)
protected 'primaryKey' => string 'id' (length=2)
protected 'keyType' => string 'int' (length=3)
public 'incrementing' => boolean true
protected 'with' =>
array (size=0)
...
protected 'withCount' =>
array (size=0)
...
protected 'perPage' => int 15
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
protected 'attributes' =>
array (size=6)
...
protected 'original' =>
array (size=6)
...
protected 'changes' =>
array (size=0)
...
protected 'casts' =>
array (size=0)
...
protected 'dates' =>
array (size=0)
...
protected 'dateFormat' => null
protected 'appends' =>
array (size=0)
...
protected 'dispatchesEvents' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'relations' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
public 'timestamps' => boolean true
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
2 =>
object(App\Page)[299]
protected 'fillable' =>
array (size=3)
...
protected 'connection' => string 'mysql' (length=5)
protected 'table' => string 'pages' (length=5)
protected 'primaryKey' => string 'id' (length=2)
protected 'keyType' => string 'int' (length=3)
public 'incrementing' => boolean true
protected 'with' =>
array (size=0)
...
protected 'withCount' =>
array (size=0)
...
protected 'perPage' => int 15
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
protected 'attributes' =>
array (size=6)
...
protected 'original' =>
array (size=6)
...
protected 'changes' =>
array (size=0)
...
protected 'casts' =>
array (size=0)
...
protected 'dates' =>
array (size=0)
...
protected 'dateFormat' => null
protected 'appends' =>
array (size=0)
...
protected 'dispatchesEvents' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'relations' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
public 'timestamps' => boolean true
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
I'm looking for "title" or "body," and I don't see squat.
What do you think?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Chris!
I went back to my notes and I figured it out.
Could you tell me WHY this works?
public function index()
{
//get pages
$pages = Page::all();
//return view('admin/listPages', ['pages'=>$pages]);
return view('admin/listPages', compact('pages'));
}
And then on my View, I do this:
@foreach($pages as $page)
<tr>
<td>{{ $page->title }}</td>
</tr>
@endforeach
...and I'm gold!
But what's significant about compact('pages')? Why the single quote?
I went back to my notes and I figured it out.
Could you tell me WHY this works?
public function index()
{
//get pages
$pages = Page::all();
//return view('admin/listPages', ['pages'=>$pages]);
return view('admin/listPages', compact('pages'));
}
And then on my View, I do this:
@foreach($pages as $page)
<tr>
<td>{{ $page->title }}</td>
</tr>
@endforeach
...and I'm gold!
But what's significant about compact('pages')? Why the single quote?
ASKER
I think you answered my question as I was typing it.
ASKER
Perfect!
Thank you, sir!
And listen, I've going to have more questions. It's amazing how a tutorial is one thing, but building something completely on your own is another.
Rock on!
Thank you, sir!
And listen, I've going to have more questions. It's amazing how a tutorial is one thing, but building something completely on your own is another.
Rock on!
ASKER
As always, a very valuable resource!
No worries Bruce.
It's a never ending learning process, but by building your own application, the leaning process will stick more than reading tutorials - it may just take a little while (and a lot of frustrating). Keep at it :)
FYI - in the following, $data1 and $data2 are exactly the same thing, so you can use either in your call to view :)
$pages = Page::all();
$data1 = ['pages' => $pages];
$data2 = compact('pages');
It's a never ending learning process, but by building your own application, the leaning process will stick more than reading tutorials - it may just take a little while (and a lot of frustrating). Keep at it :)
FYI - in the following, $data1 and $data2 are exactly the same thing, so you can use either in your call to view :)
$pages = Page::all();
$data1 = ['pages' => $pages];
$data2 = compact('pages');
When you pass data to a view, you need to pass an array - the key becomes the variable in the View, so if you do this:
return view('myPage', [ 'firstName' => 'Chris'] );
Then the view has access to a variable called $firstName:
<h1> {{ $firstName }} </h1>
In your case, you need to pass it something like this:
$pages = Page::all();
return view('admin/listPages', [ 'pages' => $pages ]);
Now you'll have a $pages variable in your view that you can loop through
The reason you're getting the undefined constant is because you're trying to access pages and not $pages