Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hilixo's avatar

How to select custom columns from tables.

I have this table:

Tables

Models

<?php

class Post extends Eloquent {
    protected $fillable = ['title', 'content', 'shortDescription', 'pictureUrl'];

    public function taxonomy()
    {
        return $this->belongsToMany('Taxonomy')->withPivot('post_id', 'taxonomy_id');
    }
} 
<?php

class Taxonomy extends Eloquent{
    protected $fillable = ['name', 'type', 'url'];
} 

ApiController

<?php

class ApiController extends BaseController {

    /**
     * Fetch All Posts
     *
     * @return mixed
     */
    public function gatAllPosts()
    {
        return Post::with('taxonomy')->get();
    }

}

Results

{
 "id": 3,
 "title": "Some title",
 "content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting.<\/p>",
 "shortDescription": "",
 "pictureUrl": "",
 "created_at": "2014-08-21 17:22:38",
 "updated_at": "2014-08-21 17:22:38",
 "taxonomy": [{
     "id": 1,
     "name": "News",
     "type": "category",
     "url": "news",
     "created_at": "2014-08-21 15:54:47",
     "updated_at": "2014-08-21 15:54:47",
     "pivot": {
         "post_id": 3,
         "taxonomy_id": 1
     }
 }]
}

How I can select, for example from taxonomies table only name and url.

{
 "id": 3,
 "title": "Some title",
 "content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting.<\/p>",
 "shortDescription": "",
 "pictureUrl": "",
 "created_at": "2014-08-21 17:22:38",
 "updated_at": "2014-08-21 17:22:38",
 "taxonomy": [{
     "name": "News",
     "url": "news"
 }]
}
0 likes
7 replies
mikebronner's avatar

The way you have it right now, you would access those fields like this:

echo $post->taxonomy->name;

However, if you want the database to only query the taxonomy table, and not the other tables, you would probably be best off writing raw queries, maybe something like this:

$taxonomy = DB::select(DB::raw("SELECT name, url FROM taxonomies AS t, post_taxonomy AS pt  WHERE t.id = pt.taxonomy_id AND pt.post_id = :postId"), ['postId' => $postId]);
JoshWilley's avatar

Try this:

<?php

class ApiController extends BaseController {

    /**
     * Fetch All Posts
     *
     * @return mixed
     */
    public function gatAllPosts()
    {
        return Post::with(['taxonomy' => function($query)
        {
            // From what I can remember, you have to include the id when selecting specific columns in a query constraint. I may be wrong here though, so try omitting it and see what happens.
            return $query->get(['id', 'name', 'url']);
        }])->get();
    }

}
JoshWilley's avatar

Try taking out the id field from the query constraint. So it would just be $query->get(['name', 'url']);

hilixo's avatar

I have same results with this $query->get(['name', 'url']) , $query->get(['name', 'url']) queries.

Please or to participate in this conversation.