拉拉维尔 未定义的变量,即使在设置 isset 之后



这段代码有什么问题?我还设置 isset 函数,如果未设置prop_type则不执行任何操作,但它仍然通过!然后下次使用它时,它会显示错误Undefined variable: prop_type顺便说一句,我正在使用拉拉维尔。

if(isset($prop_type))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
})

这是使用!与形式!

{
// General Webmaster Settings
$WebmasterSettings = WebmasterSetting::find(1);
$search_word = $request->search_word;
$prop_type = $request->prop_type;
$categori =$request->prop_cat;
$prop_condi =$request->prop_condi;
if (!isset($search_word,$prop_type,$categori,$prop_condi)) {

// count topics by Category
$category_and_topics_count = array();
$AllSections = Section::where('status', 1)->orderby('row_no', 'asc')->get();
if (!empty($AllSections)) {
foreach ($AllSections as $AllSection) {
$category_topics = array();
$TopicCategories = TopicCategory::where('section_id', $AllSection->id)->get();
foreach ($TopicCategories as $category) {
$category_topics[] = $category->topic_id;
}
$Topics = Topic::where([['status', 1], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orWhere([['status', 1], ['expire_date', null]])->whereIn('id', $category_topics)->orderby('row_no', 'asc')->get();
$category_and_topics_count[$AllSection->id] = count($Topics);
}
}
// Get current Category Section details
$CurrentCategory = "none";
$WebmasterSection = "none";
// Get a list of all Category ( for side bar )
$Categories = Section::where('father_id', '=',
'0')->where('status', 1)->orderby('row_no', 'asc')->get();
// Topics if NO Cat_ID
$Topics = Topic::where('title_ar', 'like', '%' . $search_word . '%')
->orwhere('title_en', 'like', '%' . $search_word . '%')
->orwhere('seo_title_ar', 'like', '%' . $search_word . '%')
->orwhere('seo_title_en', 'like', '%' . $search_word . '%')
->orwhere('details_ar', 'like', '%' . $search_word . '%')
->orwhere('details_en', 'like', '%' . $search_word . '%')
->orwhere('details_en', 'like', '%' . $search_word . '%')
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
if(isset($categori)){
$Topics= Topic::where('expire_date', '>=', Carbon::now())->join('topic_categories', function ($join) {
$join->on('topic.id', '=', 'topic_categories.topic_id')
->where('topic_categories.section_id', '=', $categori);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}


if(isset($prop_type))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}
if(isset($prop_condi))
{
$Topics= Topic::where('title_en', 'like', '%' . $search_word . '%')->join('topic_fields', function ($join) {
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_condi);
})
->orderby('id', 'desc')->paginate(env('FRONTEND_PAGINATION'));
}
// Get Most Viewed
$TopicsMostViewed = Topic::where([['status', 1], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orwhere([['status', 1], ['expire_date', null]])->orderby('visits', 'desc')->limit(3)->get();
// General for all pages
$WebsiteSettings = Setting::find(1);
$FooterMenuLinks_father = Menu::find($WebmasterSettings->footer_menu_id);
$FooterMenuLinks_name_ar = "";
$FooterMenuLinks_name_en = "";
if (!empty($FooterMenuLinks_father)) {
$FooterMenuLinks_name_ar = $FooterMenuLinks_father->title_ar;
$FooterMenuLinks_name_en = $FooterMenuLinks_father->title_en;
}
$SideBanners = Banner::where('section_id', $WebmasterSettings->side_banners_section_id)->where('status',
1)->orderby('row_no', 'asc')->get();

// Get Latest News
$LatestNews = Topic::where([['status', 1], ['webmaster_id', $WebmasterSettings->latest_news_section_id], ['expire_date', '>=', date("Y-m-d")], ['expire_date', '<>', null]])->orwhere([['status', 1], ['webmaster_id', $WebmasterSettings->latest_news_section_id], ['expire_date', null]])->orderby('row_no', 'asc')->limit(3)->get();
// Page Title, Description, Keywords
$site_desc_var = "site_desc_" . trans('backLang.boxCode');
$site_keywords_var = "site_keywords_" . trans('backLang.boxCode');
$PageTitle = $search_word;
$PageDescription = $WebsiteSettings->$site_desc_var;
$PageKeywords = $WebsiteSettings->$site_keywords_var;
// .. end of .. Page Title, Description, Keywords
// Send all to the view
return view("frontEnd.topics",
compact("WebsiteSettings",
"WebmasterSettings",
"FooterMenuLinks_name_ar",
"FooterMenuLinks_name_en",
"LatestNews",
"search_word",
"SideBanners",
"WebmasterSection",
"Categories",
"Topics",
"CurrentCategory",
"PageTitle",
"PageDescription",
"PageKeywords",
"TopicsMostViewed",
"category_and_topics_count"));
} else {
// If no section name/ID go back to home
return redirect()->action('FrontendHomeController@HomePage');
}
}```

您必须使用以下变量:

if(isset($prop_type))
{
$Topics = Topic::where('title_en', 'like', '%' . $search_word . '%')
->join('topic_fields', function ($join) use ($prop_type) { //add more this code
$join->on('topic.id', '=', 'topic_fields.topic_id')
->where('topic_fields.field_id', '=', $prop_type);
});
}

相关内容

最新更新