I’m trying to pass some SEO Values to a blade view.
My Blade view is as follows :
@if(!empty($page_data))
<?php $page_data = $page_data[0]; ?>
<title>{{ $page_data->page_seo_title }}</title>
<meta name="description" content="{{ $page_data->page_seo_desc }}">
<input type="hidden" name="page_id" class="page_id" value="{{ $page_data->page_id }}">
@endif
When I pass the following collection the view, I get the error :
Trying to get property of non-object
My collection looks as follows :
Collection {#286 ▼
#items: array:1 [▼
0 => array:2 [▼
"page_seo_title" => "SEO Title"
"page_seo_desc" => "SEO Desc"
]
]
}
I am building the collection, Like so :
// Set Page Data
$page_data = collect(
[
[
'page_seo_title' => 'SEO Title',
'page_seo_desc' => 'SEO Desc'
]
]
);
Do I need to define something else within the collection so that it gets’ picked up?
It’s worth pointing out that the page_data works on pages that have an Eloquent Query running on them, So that’s not an issue. Its’ just these dynamic pages, That I need to set. So ideally I don’t need to change the blade / view logic. Just the collection / controller logic.
2
Answers
i have updated small example based on your code and its tested.try once
Updated
in your controller
in your blade
There is no page_id property in you collection but you access it in your blade view.
please check your that line
there you access page_id but your collection doesn’t contain a propery with page_id.