Skip to content
Advertisement

How to call specific data from column database in Laravel 8

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?

Advertisement

Answer

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;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement