LaravelMetaTags icon indicating copy to clipboard operation
LaravelMetaTags copied to clipboard

pass variable in $og->addImage( )

Open manishlok opened this issue 5 years ago • 3 comments

Hi, this package is vary helpful to me. How to pass variable in $og->addImage( ); Eg. : $image='https://site.com'; $og->addImage($image); // Not working but ->setTitle($site->title); // is working

manishlok avatar Oct 30 '20 09:10 manishlok

Hi!

After OgPackage initialization you should register it.

$og = new Butschster\Head\Packages\Entities\OpenGraphPackage('some_name');

$og->setType('website')
   ->setSiteName('My awesome site')
   ->setTitle('Post title');
   
// You can render itself

$og->toHtml();
// <meta name="og:type" content="website">
// <meta name="og:site_name" content="My awesome site">
// <meta name="og:title" content="Post title">

// Or just register this package in Meta class and it will be rendered automatically
Meta::registerPackage($og);

butschster avatar Oct 30 '20 10:10 butschster

Thanks for reply. I want to pass a variable in addImage( ); When I pass variable in set attribute , It works $og->setType($site->type) // working ->setSiteName($site->name) // working ->setTitle($site->title); // working But when I pass variable in add attribute , It doesn't work Eg.: $og->addImage($site->image) // not working

manishlok avatar Oct 30 '20 12:10 manishlok

Thanks for reply. I want to pass a variable in addImage( ); When I pass variable in set attribute , It works $og->setType($site->type) // working ->setSiteName($site->name) // working ->setTitle($site->title); // working But when I pass variable in add attribute , It doesn't work Eg.: $og->addImage($site->image) // not working

Please show me full code, it will be easier to understand your problem.

Try to render $og object locally.

$og->setType($site->type) // working
->setSiteName($site->name) // working
->setTitle($site->title)
->addImage($site->image) // not working

dd((string) $og);

This package has unit tests coverage and you can see examples right in this tests https://github.com/butschster/LaravelMetaTags/blob/master/tests/Packages/Entities/OpenGraphPackageTest.php#L50

butschster avatar Oct 30 '20 12:10 butschster