简体   繁体   中英

Access a column in a database Eloquent Laravel4

I need to be able to access all the data in one of the columns of my table. My initial approach was to do something like this. The column i am trying to access in my database is called 'Tags'

//get all posts 
$post = Post::get();

//get all post tags
$post_tags = $post['tags'];

but this returns null. I know there will be an extremely simple way to do it, i am just not seeing it!

EDIT

The reason i am doing this is i have a tagging system in place when something is posted you can add 3 tags which relate to that post. What i am trying to do in the long run is when a tag is clicked it will filter out all the posts with that set tag.

The way this works is when a tag is clicked the user will be redirect to a url like so

//test-site.com/posts/tags/{tag}

so ill be using that tag to then query the database for the results. I have managed to confuse myself in a task i thought would be farily simple!

If your tags are stored in the same table, the problem may be that you are getting a full result set:

Post::get();

When you should get only one row:

Post::first();

So this one might do the trick in this case:

$post = Post::first();

$post_tags = $post->tags;

If you use Post::get() or Post::all() , you'll have to iterate trough the result set to get your info:

foreach(Post::all() as $post)
{
    echo $post->tags;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM