0

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.

1
  • 1
    If the response is json, then start by json_decoding it Commented May 13, 2014 at 12:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

Beat me to it. :) This is the correct answer, have an upvote.
Chris, its not to show in a view, I now display it in a view just to show what it returns. I eventually could do: return $data->id;
@Chilion could you edit your question to post more of your code? I'm not really sure what the problem is. I'd be happy to update my answer.
Hi Chris, thanks for your answer. I was looking for really getting an INT in return, so the answer of Deczo is exactly what I was needing. Guess my questionskills could use a little update! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.