【发布时间】:2020-04-25 14:05:02
【问题描述】:
您好开发人员,我的控制器有问题,当我尝试访问斜杠路由时,我收到了这个错误 get_the_recent_post (View: C:\xampp\htdocs\staging_project\resources\views\index.blade.php) 那个错误当我的帐户注销时发生。但是,如果我的帐户已登录,则没有发现错误。
为了更好地理解,如果我访问此链接 http://localhost:8000/ 并且我的帐户已关闭,我会收到此错误 Undefined variable: get_the_recent_post, 但是如果我访问我的帐户我在数据库中获取的数据正在显示。
目标:如果我的路线是http://localhost:8000/,我的输出必须与http://localhost:8000/index上的相同
我的路线:
Route::get('/', function () {
return view('/index');
});
Route::get('/index', 'HomeController@index')->name('index');
HomeController:
/**
* Create a new controller instance.
*
* @return void
*/
// public function __construct()
// {
// $this->middleware('auth');
// }
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$get_the_recent_post = DB::select('SELECT content_id,content_title,content_desc,when_created,content_author FROM icweb_content WHERE content_status = ? AND MONTH(when_created) = MONTH(CURDATE()) ORDER BY when_created DESC LIMIT 3',[
'Approved'
]);
$get_the_upcoming_event = DB::select('SELECT ic_content.content_id,content_title,content_desc,content_author,start_event,end_event,when_created FROM icweb_content as ic_content
WHERE content_status = ? AND start_event = CURDATE() AND end_event >= CURDATE() ORDER BY when_created DESC LIMIT 3
',[
'Approved'
]);
return view('/index')
->with('get_the_recent_post',$get_the_recent_post)
->with('get_the_upcoming_event',$get_the_upcoming_event);
}
index.blade.php:
@foreach($get_the_recent_post as $data_recent_post)
<a href="" style="text-decoration: none;">
<div style="line-height: 2px;">
<h5 class="card-title" style="font-weight: 500; font-size:13px; color:black;">{{$data_recent_post->content_title}}</h5>
<p class="card-text" style="color:#757a91; font-size:12px; font-weight: 500;">{{strip_tags($data_recent_post->content_desc)}} </p>
</div>
</a>
@endforeach
【问题讨论】:
-
我认为您还需要在“/”路由中调用“HomeController@index”。与您在“/index”路由中调用的相同。
-
@SohilChamadia 我试过了,但还是不行
-
在您的“/”操作中,您只是在加载没有数据的视图页面,您也需要加载数据
-
否则你需要这样做 Route::get('/', 'HomeController@index')->name('index2');
-
当您在“/”路由中调用“HomeController@index”时,您面临什么问题?