I have this query:

 static function findIdOnName($pageName){
    return Fanpages::select('id')
                ->where('url', '=', $pageName)
            ->get();
}

Response: (when done print_r)

[{"id":17}]

I just want the INT (in this case 17) I searched the interwebs for it, but I can't find anthing about it. Randomly tried adding ->toString() etc to the query, but so far, no good.

有帮助吗?

解决方案

Your code returns a Collection with a single Model (or multiple models if there are more matching the where clause), while the method you need is pluck:

return Fanpages::where('url', '=', $pageName)->pluck('id');
// returns INT 17

as it returns value for column id of the first row matching WHERE clause.

其他提示

If you do not return a view with data, then Laravel will automatically convert your data into json. In order to accomplish what you want you can simply do something like

$data = Fanpages::select('id')->where('url', '=', $pageName)->get();
die($data->id);

However, exiting the application like this isn't recommended. You should either keep the json response and work with that, or send the data to a basic blade template.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top