samedi 2 décembre 2017

How to access varible inside orWhere() in laravel query builder|Laravel

I am trying to build little complex query where wvery thing wan OK untill i tried to use a advanced where condition.
In the code below I am getting an error saying

ErrorException: Undefined variable: user in file D:...**\app\Http\Controllers\ApiAuthController.php on line 285

and the line 285 is $query->where("tbl_transactions.from_user_id", "=", $user->id) inside the orWhere()

 public function loadTransactions()
    {

        $token = JWTAuth::getToken();

        $user = JWTAuth::toUser($token);


        return DB::table('tbl_transactions')
            ->join('tbl_users as fromu', 'fromu.id', '=', 'tbl_transactions.from_user_id')
            ->join('tbl_users  as tou', 'tou.id', '=', 'tbl_transactions.to_user_id')
            ->select('tbl_transactions.id', 'tbl_transactions.from_user_id', 'tbl_transactions.to_user_id', 'tbl_transactions.amount', 'tbl_transactions.created_at', 'tbl_transactions.status', 'fromu.userid', 'tou.userid', 'tou.phone as tophone', 'fromu.phone as fromphone')
            ->orWhere(function ($query) {
                $query->where("tbl_transactions.from_user_id", "=", $user->id)// here the problem is (undefined **$user**)
                    ->where('tbl_transactions.to_user_id', '=', $user->id);
            })
            ->orderBy('tbl_transactions.id', 'desc')
            ->take(50)
            ->get();


    }

I am new to laravel.Anyone here could help me would be great.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ADk59Q
via IFTTT

Aucun commentaire:

Enregistrer un commentaire