I have a HeaderData
model, it has two fields:
$table->string('url');
$table->text('data');
Through BaseController
, I added one method for all controllers, so as not to repeat the code:
protected $header_data;
public function __construct()
{
$this->header_data = HeaderData::all();
View::share('header_data', $this->header_data);
}
And I display this on the pages, the data
field in the head
:
@foreach ($header_data as $data)
{!! $data->data !!}
@endforeach
But for me it displays all the data
that is there, but I only need it for the current page, so there is a url
field. I need to do a type check, if we are on the page that is specified in the url
field, then we display the data
field for the same page. Here's an example:
Add
"url": "/contacts"
"data": "<title>Contacts TEST</title>
<meta name="description" content="test CONTACTS" />"
Let's say we check that if we are now on the contacts page, then we display the data
field only for contacts. If the page is a let's say blog, then we display the data
for the blog.
But I can't figure out how to do this yet, can anyone tell me? Can I do it right in this controller?
CodePudding user response:
in your controller
$header_data = HeaderData::where(['url'=>Route::currentRouteName()])->first();
//and in blade
{!! $header_data->data !!}
or you can try
@foreach($header_data as $data)
@if(Route::currentRouteName() === $data->url)
{!! $header_data->data !!}
@endif
@endforeach