Support the ongoing development of Laravel.io →
posted 10 years ago
Requests

I am developing apis for andriod in laravel 4.2. Facing a very silly type of problem I have a blog model which connects to its comment by relationship.

THE CODE IS:-

$user = User::find($id); if ($user) { $blog = Blog::find($blog_id); if($blog) { $com = []; $image = $blog->postedBy->image; $blog['image'] = $image; $comments = $blog->comments;

            foreach ($comments as $comment) {
               $name = $comment->commentBy->fname." ".$comment->commentBy->mname." ".$comment->commentBy->lname;
               $image = $comment->commentBy->image;
               $comment = $comment->comment;
               $app = [
                   'name' => $name,
                   'image' => $image,
                   'comment' => $comment,
               ];
               
               array_push($com,$app);
            }
            $blog['comment'] = $com;
            
           
            unset($blog['comments']);
            
            $data [] = $blog;
            
            return Response::json(['result' => 1, 'data' => $data]);
        }
        return Response::json(['result' => 0, 'data' => 'Invalid blog details supplied']);
    }

AND THE RESPONSE IS :- { "result": 1, "data": [ { "id": "1", "title": "First blog title from alwahda college by admin", "description": "Details goes here First blog title from alwahda college updated by admin", "comment_status": "1", "attachment": "4y1S6tNhnwtwKbtJRa5i.doc", "file_name": "test.doc", "created_by": "1", "organization_id": "1", "is_delete": "0", "created_at": "2015-05-20 01:04:21", "updated_at": "2015-05-20 01:50:15", "image": null, "comment": [ { "name": "Dhrubojyoti Das", "image": null, "comment": "Comment from admin goes here" }, { "name": "Ranjeev Banerjee", "image": "MP3E5ZEAI6DTwHgINlWC.jpg", "comment": "Comment from Ranjeev Banerjee" }, { "name": "Ranjeev Pradhan", "image": null, "comment": "This is test comment on 20th may" } ], "posted_by": { "id": "1", "user_code": "ad.01", "fname": "Dhrubojyoti", "mname": "", "lname": "Das", "user_type_id": "0", "session_student_id": "0", "email": "dhrubojyoti_das@yahoo.in", "alt_email": "", "mobile_no": "8296268671", "alt_mobile_no": "", "address": "5/84 Hazra Road Kolkata 56", "city_id": "0", "state_id": "0", "pin_code": "700056", "country_id": "0", "dob": "1990-09-30", "religion": "Hindu", "gender": "Male", "blood_group": "o+", "image": null, "device_type": null, "device_token": null, "version_no": "", "is_delete": "0", "pay_status": null, "package_type": null, "organization_id": "1", "created_by": "0", "created_at": "2015-05-26 16:18:33", "updated_at": "2015-05-26 10:48:33" } } ] }

I dont want the posted_by key to appear in the array...but its's coming since i have called the $blog->postedBy relationship.

How to remove that...

Last updated 3 years ago.
0

you can select what do u want to use in commentBy relationship;

...
..->with(['commentBy' => function ($q) {
   $q->addSelect('a','b','c') //etc
}]);
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dhrubo001 dhrubo001 Joined 19 Mar 2015

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.