0

I want to call specific value from field column in my database..

Let say i have table called posts, my table have field called post_meta_title and have value home How do i call only that value to show in my meta tags..

I have tried it, and it show error Property [post_meta_title] does not exist on this collection instance

Does anyone know how to solved it?

4
  • 1
    What have you tried? Show your attempts. Commented May 28, 2021 at 2:01
  • Here's in my view meta <title>@yield('post_meta_title', 'default title')</title> <meta name="keywords" content="@yield('post_meta_keyword','some default keywords')"> <meta name="description" content="@yield('post_meta_description','default description')"> Commented May 28, 2021 at 2:09
  • Here's in my content @section('post_meta_title', $posts->post_meta_title) @section('meta_keywords', $posts->post_meta_keyword) @section('meta_description', $posts->post_meta_description) Commented May 28, 2021 at 2:11
  • I call it at Controller $posts = Post::all(); I don't know how to call specific value from field database Commented May 28, 2021 at 2:13

2 Answers 2

1

The post_meta_title does not exist on $posts because $posts is a collection of multiple posts (Post::all()).

You can only get the post_meta_title from a single post. For example, you could do

$post = Post::first();
$post->post_meta_title;

or

$post = Post::find(3); // in this example, 3 is the post's ID
$post->post_meta_title;

or

foreach(Post::all() as $post) {
    $post->post_meta_title;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have found the solution, i use query builder $keywords = Meta::where('id', '1')->value('meta_keyword'); it allows me to directly get specific value from database.. Thankyou somuch for your help..
0

because your $posts is a collection of object not model instance, to access post_meta_title you need to get it in a model instance: foreach ($posts as $post){echo $post->post_meta_title} or in blade

@foreach($posts and $post)
    @section('post_meta_title', $post->post_meta_title)
    @section('meta_keywords', $post->post_meta_keyword)
    @section('meta_description', $post->post_meta_description)
@endforeach

1 Comment

I have found the solution, i use query builder $keywords = Meta::where('id', '1')->value('meta_keyword'); it allows me to directly get specific value from database.. Thankyou somuch for your help..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.