0

I ran this code and it is returning an array. Please, how can I make it return only a user_id and not arrays of user_id?

     $userr = DB::table('level_one_models')
        ->select('user_id')
        ->where('downline', '<=', 7)
        ->orderBy('created_at', 'desc')
        ->get();
4
  • Could you give an example of your desired output? Commented Mar 5, 2017 at 13:35
  • I want to fetch the user_id as an integer. The query above is returning user_id as an array. Commented Mar 5, 2017 at 13:45
  • What version of Laravel are you using? Commented Mar 5, 2017 at 13:49
  • I am using Laravel 5.3 Commented Mar 5, 2017 at 14:16

2 Answers 2

3

If you want $userr to be the user_id you could just do:

$userr = DB::table('level_one_models')
    ->select('user_id')
    ->where('downline', '<=', 7)
    ->orderBy('created_at', 'desc')
    ->first()
    ->user_id;

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, really helpful
0

Use ->first() instead of ->get().

2 Comments

First method is throwing error: Object of class stdClass could not be converted to string
My downline column holds email addresses and I want the downline column counted. I think my approach is wrong. Please how can I count the downline column?

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.