Aidas Patas
Aidas Patas

Reputation: 11

How do i change the format of an SQL output?

I am using Laravel 5.5, and when I am trying to export data from another DB, i use:

$fname = DB::connection('smallplanet')->table('pilots')->where('id', Auth::user()->id)->get(['fname']);
echo $fname;

and this is the output:

[{"fname":"Aidas1"}]

how do i change the output to just "Aidas1"?

Upvotes: 0

Views: 64

Answers (2)

Clément Baconnier
Clément Baconnier

Reputation: 6078

Couldn't it be easier to change the source instead of the result ?

For one pilot

$pilot = DB::connection('smallplanet')->table('pilots')->find(Auth::user()->id);

if($pilot){
    echo $pilot->fname;
}else{
    echo "pilot not found";
}

For many pilots

$pilots = DB::connection('smallplanet')->table('pilots')->where('your_field', $value)->get();

foreach($pilots as $pilot){
    echo $pilot->fname . PHP_EOL;
}

Upvotes: 0

aidinMC
aidinMC

Reputation: 1436

Its JSON ,try this:

$output = json_decode($fname,true);

echo $output[0]["fname"];
//********* OR **********
foreach($output as $row)
    echo $row["fname"];

Upvotes: 3

Related Questions