Ad

Undefined Variable In View Page Laravel

- 1 answer

Im getting error in laravel undefined variable all is good but won't know why its coming... controller

viewerror

public function login(Request $req)
    {

        $user = users::where([

            ["email", "=", $req->email],
            ["password", "=", $req->password]

        ])->first();

        if (isset($user)) {

            $req->session()->put("log", $req->input());

            return redirect("/userlist");
        } else {
            $b="user not found";

            return view("login", compact("b"));
        }
Ad

Answer

You may fix it by defining $b variable inside your controller method

public function myLoginFunction() {
//...
$b = ...;
return view('viewName', ['b' => $b]);
}

Edit

public function login(Request $req) {

    $user = users::where([

        ["email", "=", $req->email],
        ["password", "=", $req->password]

    ])->first();

    if (isset($user)) {

        $req->session()->put("log", $req->input());

        return redirect("/userlist");
    } else {

        return view("login");
    }
}

App\Providers\AppServiceProvider

//...
public function boot()
{

    View::composer('login', function ($view) {
        $view->with('b', "user not found");
    });
}
Ad
source: stackoverflow.com
Ad