0

I am trying to get only integer value (serial id) without a column name, which makes it a string. I am working in Laravel so I can use Laravel functions for SQL queries if necessary.

$idcko = DB::table('repairs')
         ->select('id')
         ->latest();

So when I print idcko I get something like {"column name": 2} Where I want only number 2.

UPDATE:

With help of answers, I came up with this idea:

$idcko = DB::table('repairs')
                                ->select('id')
                                ->orderBy('id','desc')
                                ->first()
                                ->id;

Because I was getting error that I don't have a timestamp column and I wanted to get the newest / highest id, I could do a little trick and order it by id and select only first row.

0

2 Answers 2

1

latest() just adds an order by clause and returns the builder instance. (See the API reference.) You still need to use some fetch method to actually get any records to pull values from. Since you're trying to get a single value, you can use first() and then get the property you want from that record.

$id = DB::table('repairs')->select('id')->latest()->first()->id;
Sign up to request clarification or add additional context in comments.

5 Comments

For both answers (Samsquanch), I am getting this error: Undefined column: 7 ERROR: column "created_at" does not exist LINE 1: select "id" from "repairs" order by "created_at" desc limit ... ^ (SQL: select "id" from "repairs" order by "created_at" desc limit 1)
Yeah, latest() depends on you having a timestamp column. It uses created_at by default, but you can pass a different one if your timestamp column is differently named.
But I did not change name of it. Is there another way how to get the newest id if its serial type?
I think I may have misunderstood what you're trying to do. Can you edit the question to add more context/details regarding your overall goal?
I have updated my question where I posted my solution with your help guys.
1

You're just wanting to retrieve the value of the column, so use value():

$idcko = DB::table('repairs')
             ->select('id')
             ->latest()
             // ->first()      // get the first result -- may not be needed?
             ->value('id'); // or any other column name here

7 Comments

Oh yeah, forgot about value(). That's better.
@Don'tPanic Thanks for the tip on first(), I thought latest() returned a single result.
I think you were right at first. I believe ->value('id') is a shortcut equivalent to ->first()->id
Guys, for both of your's answers, I get same error as I mentioned in comment in @Don'tPanic answer.
@Ady96 are you sure the column you're trying to select is in the repairs table?
|

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.