Support the ongoing development of Laravel.io →
posted 11 years ago
Eloquent

Find By Id Using find()

$post = Post::find(2); $post->setHidden(['content','read_more','updated_at']); echo $post; {"id":"2","title":"Post no 2","comment_count":"7","created_at":"2014-01-06 09:43:44"}

Limit No Of Records Using take() And skip()

$post = Post::skip(5)->take(2)->get(); foreach($post as $value) echo "post id:$value->id "; post id:6 post id:7

Using select() And first()

$post = Post::select('id','title')->first(); echo $post; {"id":"1","title":"Post no 1"}

Using where() With select()

$post = Post::select('id','title')->where('id','=',10)->first(); echo $post; {"id":"10","title":"Post no 10"}

Getting Related Records Using Dynamic Property

$post = Post::find(4); echo $post->comments[0]->commenter; xyz

Getting Parent Records From Child Model

$comment = Comment::find(1); echo $comment->post->title; Post no 1

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

gogs85 gogs85 Joined 15 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.