0

I have this error, my question is. Can't pass a variable from UsersController from function userscontroller_show if the view returns function userscontroll from Dashboardcheck?

$query is undefined Make the variable optional in the blade template. Replace {{ $query }} with {{ $query ?? '' }}

Web file. Route to function who show data from database

Route::get('/userscontrollers', [UsersController::class, 'userscontroller_show']);

Route to function who returns view:

Route::get('/userscontrollers', [Dashboardcheck::class, 'userscontroll']);

UsersControllers Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UsersController extends Controller
{
    public function userscontroller_show(){
        $query = DB::table('user')
        ->select('id_users', 'nick_user', 'email_user', 'verification_user')
        ->get()
        ->toArray();
        return view('Pages.Dashboard.usercontroll', compact('query'));
    }

    protected function userscontroller_update(){

    }
}

Dashboardcheck Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class Dashboardcheck extends Controller
{
    function login(){
        if(Session::has('user')){
            return view('Pages.dashboard');
        }else{
            return redirect('/');
        }
    }
    function userscontroll(){
        if(Session::has('user')){
            $query = DB::table('user')
            ->select(DB::raw('*'))
            ->where('nick_user', Session::get('user'))
            ->first();
            if($query->user_permission >= 4){
                return view('Pages.Dashboard.usercontroll');
            }else{
                return redirect('/dashboard')->with('alert', 'Nie posiadasz uprawnien do przeglądania tych treści');
            }
        }else{
            return redirect('/');
        }
    }

}
1
  • You can't have two identical routes for /userscontrollers. Laravel has no way to know which route you want to use, so it will only use the first route every time. Commented Aug 27, 2021 at 7:59

1 Answer 1

1

First update you routes. You cannot have similar route. Update this line of code in Dashboardcheck Controller.

return view('Pages.Dashboard.usercontroll', compact('query'));

You did not pass any $query variable. So it is showing error.

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

4 Comments

Why? I wanna pass $query from UsersController. In Dashboardcheck Controller I have only check if user have privileges and return view
If you do not want to pass $query then you have to check that variable is set and not null in blade. If not set or null then return else block. Use if else.
I wanna pass $query but from UsersControllers Controller. But my view is return in Dashboardcheck Controller
Put a if else condition in blade.

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.