Support the ongoing development of Laravel.io →
Security Requests Eloquent
Last updated 1 year ago.
0

Not sure if I understand you correctly or not. But you don't have to have the query fields in the select statement in order to reference them. a raw DB pull would look like:

DB::Raw("SELECT fields,to,grab FROM posts WHERE user_id=1");

You can do the same thing with the helpers

$Posts = DB::table('posts')->select(['fields','to','grab'])->where('user_id',1)->get();
0

I know thats not a real query, I can get the data fine, but for example I can view a post, I get the posts data which also grabs the comments associated, and the comments belong to a User, so to display the name of the user who posted the comment I have to grab the user info. But looking at the JSON response You can clearly see the user_id and user info which I do not want. I need to use the id's in the find, but no need to return them to the view / response. So I want to remove them after the query grabs the data, if you do not include id, user_id you do not get the associated data.

$Posts = DB::table('posts')->select(['fields','to','grab'])->where('user_id',1)->get(); will display the user id.

If a post has 50 comments the JSON will return 50 user.id's in the find, not what I want, I do not want to expose any primary id's of any kind, any record in the responses.

0

I'm not positive if this works on the DB controller, but after doing your query try something like this maybe

$Posts = DB::table('posts')->select(['fields','to','grab'])->where('user_id',1)->get();

$FilteredPosts = Posts::only(['fields','you','want']);

0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2024 Laravel.io - All rights reserved.