2

In laravel 4 route, i have a database query like this

$data1 = DB::select('SELECT MAX(id) AS mxid FROM table_name', array());
return $data1["mxid"];  

But it gives the following error:

enter image description here

Help me. I can't understand why this key is not found . If i write

return $data1;  

it gives,

[{"mxid":"0"}]

3 Answers 3

4

Solution:

return $data1[0]->mxid;
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, i was stuck on the same thing. How do you count the number of rows the query fetches?
3

You could leverage the query builder to make this slightly easier and nicer looking.

return DB::table('table_name')->selectRaw('MAX(id) AS mxid')->pluck('mxid');

This should pluck the mxid column from the result and return it immediately.

Comments

1

I am getting "Trying to get property of non-object" error. Any idea?

EDIT:

This solved the problem for me:

return $result[0]['field_name'];

Comments

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.