skip to Main Content

I’m trying to pass some SEO Values to a blade view.

My Blade view is as follows :

@if(!empty($page_data))
        <?php $page_data = $page_data[0]; ?>
        <title>{{ $page_data->page_seo_title }}</title>
        <meta name="description" content="{{ $page_data->page_seo_desc }}">
        <input type="hidden" name="page_id" class="page_id" value="{{ $page_data->page_id }}">
@endif

When I pass the following collection the view, I get the error :

Trying to get property of non-object

My collection looks as follows :

Collection {#286 ▼
  #items: array:1 [▼
    0 => array:2 [▼
      "page_seo_title" => "SEO Title"
      "page_seo_desc" => "SEO Desc"
    ]
  ]
}

I am building the collection, Like so :

// Set Page Data
            $page_data  = collect(
                [
                    [
                        'page_seo_title' => 'SEO Title',
                        'page_seo_desc' => 'SEO Desc'
                    ]
                ]
            );

Do I need to define something else within the collection so that it gets’ picked up?

It’s worth pointing out that the page_data works on pages that have an Eloquent Query running on them, So that’s not an issue. Its’ just these dynamic pages, That I need to set. So ideally I don’t need to change the blade / view logic. Just the collection / controller logic.

2

Answers


  1. i have updated small example based on your code and its tested.try once

     $page_data  = collect(
                            [
                                [
                                    'page_seo_title' => 'SEO Title',
                                    'page_seo_desc' => 'SEO Desc'
                                ]
                            ]
                        );
    
    //here it will print all data 
                        print_r($page_data->toArray());
    
                        $data=$page_data->toArray();
    
    // here it will print particular data
                    echo    $data[0]['page_seo_title'];
    

    Updated

    in your controller

    $data=$page_data->toArray();
    
            return view('viewname',['data',$data];
    

    in your blade

    @if(!empty($data))
        <?php $new_data = $data[0]; ?>
        <title>{{ $new_data['page_seo_title'] }}</title>
        <meta name="description" content="{{ $new_data['page_seo_desc'] }}">
        <input type="hidden" name="page_id" class="page_id" value="{{ $new_data['page_id'] }}">
    
    Login or Signup to reply.
  2. There is no page_id property in you collection but you access it in your blade view.
    please check your that line

    <input type="hidden" name="page_id" class="page_id" value="{{ $page_data->page_id }}">
    

    there you access page_id but your collection doesn’t contain a propery with page_id.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search