C
C
Cavin March2017-01-24 19:55:42
Laravel
Cavin March, 2017-01-24 19:55:42

Database queries. LARAVEL?

Perhaps this will be the most stupid question on this site. But yo-mayo.. I can't query the database via DB::table. PHPStorm tells me that this method does not exist... Okay, let's ignore it. I go to the browser, and then I get:

use Illuminate\Support\Facades\DB;
        
        ...

        $user = DB::table('users')->where('username', '=', Auth::user());

        if ($user->username == NULL && $user->email == NULL) {

            //тест
           return redirect('/badlogin');

        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2017-01-24
@ForbsmC

When will people start reading documentation?
First, what is where('username', '=', Auth::user())
Auth::user() it's an entire object, you're comparing it all against username instead of Auth::user()->username.
Secondly, get () has already been written, it is necessary to do it, but then it can issue an array of strings from the base. So use ->first().
And then the result is only after checking that the result exists at all, otherwise in case of any error you will have undefined property

if($user) {

}

Thirdly, if you write authorization, then you are doing it in vain:php artisan make:auth

W
WQP, 2017-01-24
@WQP

Is it available?
use Illuminate\Http\Request;

M
Mysterion, 2017-01-24
@Mysterion

use DB;
use Auth;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question