0

What's wrong with this code? I also set isset function to not do anything if prop_type is not set but still there it pass ! and then next time its used it shows error Undefined variable: prop_type btw I am using laravel.

  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);
        })

and this the full function m using !with form !

    {

        // 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');
        }

    }```
Aryabh
  • 77
  • 1
  • 11
  • Where you pass this variable?? test `dd($prop_type)` before if condition – A.A Noman Jun 19 '20 at 06:26
  • Try `!empty()` instead of `isset()`. This will evaluate to true only if there is something other than null, false, 0, or the empty string ''. You probably have empty strings being submitted. – Dilip Hirapara Jun 19 '20 at 06:27

1 Answers1

2

You must to use the variable:

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);
              });
   }
Ryan Nghiem
  • 2,417
  • 2
  • 18
  • 28