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('/');
}
}
}
/userscontrollers. Laravel has no way to know which route you want to use, so it will only use the first route every time.