skip to Main Content

I am trying to make collection in laravel and trying to print it in view but this is showing me error

Trying to get property of non-object

anyone tell me where I am wrong.

$seo = Collection::make([
    "meta_title" => "Eco Elegant - Frp Dustbins & Planters Manufacturers in India",
    "meta_desc" => "Eco Elegant is the leading manufacturer and designers of stainless steel/ (Fiber Reinforced Plastic) FRP planters and dustbins for places with diverse usage needs.",
    "meta_keyword" => "Eco Elegant, Frp Dustbins, Frp Planters, Stainless Steel Dustbins, Stainless Steel Planter, Dustbins and Planter Suppliers, Exporters in India"
]);

@if($seo->count() > 0)
    @foreach($seo as $seo1)
    <title>{{ $seo1->meta_title }}</title>
    <meta name="description" content="{{ $seo1->meta_desc }}">
    <meta name="keywords" content="{{ $seo1->meta_keyword }}">
    @endforeach
@else               
    <title>Eco Elegant</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
@endif

3

Answers


  1. You need to access the array index like

    $seo1['meta_title']
    

    Instead of

    $seo1->meta_title
    
    Login or Signup to reply.
  2. Since you have a single array in the collection and not a collection of arrays or objects, you don’t need to iterate over it.

    So, remove the foreach() part and use:

    $seo->meta_title
    

    Instead of:

    $seo1->meta_title
    
    Login or Signup to reply.
  3. It seems only one item in you collection as per your question So remove the foreach and try once

     @if($seo->count()>0)
        <title>{{$seo->meta_title}}</title>
        <meta name="description" content="{{$seo->meta_desc}}">
        <meta name="keywords" content="{{$seo->meta_keyword}}">
    @else               
        <title>Eco Elegant</title>
        <meta name="description" content="">
        <meta name="keywords" content="">
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search