Giacomo
Giacomo

Reputation: 341

Laravel | Get specified data for current page

I'm trying to get meta data for each page Each page has a unique slug

In appServiceProvider I have:

$view->with('seopage', Page::all());

In blade view (section head) I have:

<title>
  @foreach($seopage as $sm)
    {{ $sm->main_title }}
  @endforeach
</title>
<meta name="description" content="
  @if(Request::is('/*'))                             
  @foreach($seopage as $sp)
  {{ $sp->site_description }}
  @endforeach
  @endif
">

But it is getting all titles and descriptions for me at the moment. How can I get data for specified page?

Page model have unique SLUG variable. Thanks

Upvotes: 0

Views: 257

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

You must be have unique uri column in seopage table

The you must be use

use Illuminate\Support\Facades\Route;

.....

$uri = \Route::current()->uri;
$seopage =  Page::where('uri', $uri)->first();

if ($seopage) {
    $seo = sprintf(
        '<title>%s</title>'  . PHP_EOL
        . '<meta name="description" content="%s>',
        $seopage->main_title ,
        $seopage->site_description
    );
} else {
    $seo = '';
}
$view->with('seo', $seo);

In Blade use

{{ $seo }}

Upvotes: 0

Malkhazi Dartsmelidze
Malkhazi Dartsmelidze

Reputation: 4992

In appServiceProvider you can share page which belongs to specified page:

$page = Page::where('slug', Request::path())->first();
View::share('seopage', $page);

And in view you can retreive shared variable:

<meta name="description" content="
  @if(Request::is('/*') && isset($seopage))
  {{ $seopage->site_description }}
  @endif
">

Upvotes: 2

Related Questions