如何在laravel-php中从另一个路由调用函数



im试图从laravel-php中的另一个路由调用函数。

我有路线";欢迎";

Route::get('/', function () {
return view('welcome');
});

我称之为扩展(侧边栏(

@extends('layouts.guestnavbar')

我想看看我的表格通知是否为空,所以我做了这个控制器

class GuestNavbarController extends Controller
{
public function isempty(){
$notif = Notification::first();
if(is_null($notif)) {
return view('layouts.guestnavbar')->with("checkempty", "empty");
}else {
return view('layouts.guestnavbar')->with("checkempty", "not empty");
}
}
}

我在我的路线guestnavbar 中调用了变量{{ $checkempty }}

Route::get('/guestnavbar', [GuestNavbarController::class, 'isempty']);

当我在guestnavbar的路线上时,它就起作用了但当我在route welcome中时不起作用,因为我调用route guestnavbar中的函数,而在welcome时他不识别变量:checkempty

我需要这个函数在guestnavbar中,因为我必须在其他页面上调用它,而不仅仅是在欢迎页面中

我感谢你的帮助。

您不需要在控制器内部使用isempty,只需在通知模型内部添加方法isempty即可,您可以在通知模型中使用类似的东西:

public static function isEmpty(){
return Notification::first() ? true : false;
}

然后,在需要检查通知表是否为空的地方,只需调用notification::isEmpty((

最新更新