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 ?

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

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.