简体   繁体   中英

Checking and adding data to url

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM