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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community