0

My question is similar with NodeJS get only value of MongoDB result.
But in this case, i'm using laravel.

This is my controller :

public function validation(Request $request)
    {
        $email= $request->input('email');
        $password= $request->input('password');

        if (DB::table('customer')
            ->where('email','=',$email)
            ->where('password','=',$password)
            ->exists())
        {
            $data=DB::table('customer')
            ->select('first_name')
            ->where('email','=',$email)
            ->project(['_id' => 0])
            ->get();

            echo $data;
        }
        else
        {
            echo 'Wrong email or password';
        }
      }

And the output is :

[{"first_name":"John"}]

I want the result only
John
and removing field name, bracket, and the others. What should i do ? And, is there any documentation for this ?

Januar
  • 49
  • 6

1 Answers1

0

I've figured out by myself. Based on How to remove double quotes from a string.
The solution is using trim function.

Import this first on the controller :

use Illuminate\Support\Str;

And then, got a little bit change on :

           $name=DB::table('customer')
            ->select('first_name')
            ->where('email','=',$email)
            ->project(['_id' => 0])
            ->get()
            ->flatten();

           $data=Str::of($name)->trim('[""]');
           echo $data;
Januar
  • 49
  • 6