Search code examples
phpdatabaselaravellaravel-artisan

Laravel: DB request saves false data


First i do the DB request.

$user=DB::table('pupil')->select('accountName')->where('accountName', '6001')->get();

But it returns this data.

[{"accountName":"6001"}]

And i need this data.

6001

I want only to echo 6001 and not [{"accountName":"6001"}].


Solution

  • if you want to get only one value then used first method here..

    $user = DB::table('pupil')->select('accountName')->where('accountName', '6001')->first()->accountName;
    

    or used value method here..

    you may extract a single value from a record using the value method.

    DB::table('pupil')->where('accountName', '6001')->value('accountName')