Laravel count issue











up vote
0
down vote

favorite












I am trying to search by check-boxes and conditions but i get this error in return:



count(): Parameter must be an array or an object that implements Countable


Logic




  1. I can select 1 or more options

  2. In back-end check what kind of options has selected

  3. Depend of the kind return results


Code



controller



public function advancedsearch(Request $request) {
$options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query) {
$query->status('Active');
}])->get();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : ;
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;

//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_subspecification', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : ;
$join->on('products.id', '=', 'product_subspecification.product_id')
->where('product_subspecification.subspecification_id', '=', $suboption);
})
->paginate(12);
}

elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}

elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}


return view('front.advancesearch', compact('products', 'brands', 'options'));
}


Summary of code above



I am getting all brands from database and check if only 1 brand has selected or several



$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;


Then I check if any brand has selected in general or no by count and returning results



elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}


but I get error I share above.



Any idea?



Update



how my html look likes



one



how the result **dd** look like



array:4 [▼
"_token" => "Wqs9yzd5qwGtbv01asbCzsISeVQxHsCoWVQM1ifO"
"brands" => array:1 [▼
0 => "3"
]
"min_price" => null
"max_price" => null
]


Blade



<div class="checkbox">
@foreach($option->subspecifications as $suboption)

@if($option->title == 'Brand')
<label for="brands">
<input name="brands" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@else
<label for="suboptions">
<input name="suboptions" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@endif

@endforeach
</div>









share|improve this question




















  • 1




    What's your HTML look like? Is your input named brands so that it comes back as an array?
    – miken32
    Nov 9 at 5:05










  • @miken32 this is how it looks like ibb.co/gKDhvA
    – mafortis
    Nov 9 at 5:11










  • Edit your question to include the relevant HTML. I'm not going to click on that link.
    – miken32
    Nov 9 at 5:12










  • @miken32 updated! ......
    – mafortis
    Nov 9 at 5:15






  • 1




    laravel.com/docs/5.6/requests#retrieving-input
    – miken32
    Nov 9 at 5:32















up vote
0
down vote

favorite












I am trying to search by check-boxes and conditions but i get this error in return:



count(): Parameter must be an array or an object that implements Countable


Logic




  1. I can select 1 or more options

  2. In back-end check what kind of options has selected

  3. Depend of the kind return results


Code



controller



public function advancedsearch(Request $request) {
$options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query) {
$query->status('Active');
}])->get();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : ;
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;

//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_subspecification', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : ;
$join->on('products.id', '=', 'product_subspecification.product_id')
->where('product_subspecification.subspecification_id', '=', $suboption);
})
->paginate(12);
}

elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}

elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}


return view('front.advancesearch', compact('products', 'brands', 'options'));
}


Summary of code above



I am getting all brands from database and check if only 1 brand has selected or several



$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;


Then I check if any brand has selected in general or no by count and returning results



elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}


but I get error I share above.



Any idea?



Update



how my html look likes



one



how the result **dd** look like



array:4 [▼
"_token" => "Wqs9yzd5qwGtbv01asbCzsISeVQxHsCoWVQM1ifO"
"brands" => array:1 [▼
0 => "3"
]
"min_price" => null
"max_price" => null
]


Blade



<div class="checkbox">
@foreach($option->subspecifications as $suboption)

@if($option->title == 'Brand')
<label for="brands">
<input name="brands" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@else
<label for="suboptions">
<input name="suboptions" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@endif

@endforeach
</div>









share|improve this question




















  • 1




    What's your HTML look like? Is your input named brands so that it comes back as an array?
    – miken32
    Nov 9 at 5:05










  • @miken32 this is how it looks like ibb.co/gKDhvA
    – mafortis
    Nov 9 at 5:11










  • Edit your question to include the relevant HTML. I'm not going to click on that link.
    – miken32
    Nov 9 at 5:12










  • @miken32 updated! ......
    – mafortis
    Nov 9 at 5:15






  • 1




    laravel.com/docs/5.6/requests#retrieving-input
    – miken32
    Nov 9 at 5:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to search by check-boxes and conditions but i get this error in return:



count(): Parameter must be an array or an object that implements Countable


Logic




  1. I can select 1 or more options

  2. In back-end check what kind of options has selected

  3. Depend of the kind return results


Code



controller



public function advancedsearch(Request $request) {
$options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query) {
$query->status('Active');
}])->get();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : ;
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;

//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_subspecification', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : ;
$join->on('products.id', '=', 'product_subspecification.product_id')
->where('product_subspecification.subspecification_id', '=', $suboption);
})
->paginate(12);
}

elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}

elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}


return view('front.advancesearch', compact('products', 'brands', 'options'));
}


Summary of code above



I am getting all brands from database and check if only 1 brand has selected or several



$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;


Then I check if any brand has selected in general or no by count and returning results



elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}


but I get error I share above.



Any idea?



Update



how my html look likes



one



how the result **dd** look like



array:4 [▼
"_token" => "Wqs9yzd5qwGtbv01asbCzsISeVQxHsCoWVQM1ifO"
"brands" => array:1 [▼
0 => "3"
]
"min_price" => null
"max_price" => null
]


Blade



<div class="checkbox">
@foreach($option->subspecifications as $suboption)

@if($option->title == 'Brand')
<label for="brands">
<input name="brands" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@else
<label for="suboptions">
<input name="suboptions" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@endif

@endforeach
</div>









share|improve this question















I am trying to search by check-boxes and conditions but i get this error in return:



count(): Parameter must be an array or an object that implements Countable


Logic




  1. I can select 1 or more options

  2. In back-end check what kind of options has selected

  3. Depend of the kind return results


Code



controller



public function advancedsearch(Request $request) {
$options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query) {
$query->status('Active');
}])->get();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : ;
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;

//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_subspecification', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : ;
$join->on('products.id', '=', 'product_subspecification.product_id')
->where('product_subspecification.subspecification_id', '=', $suboption);
})
->paginate(12);
}

elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}

elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}


return view('front.advancesearch', compact('products', 'brands', 'options'));
}


Summary of code above



I am getting all brands from database and check if only 1 brand has selected or several



$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : ;


Then I check if any brand has selected in general or no by count and returning results



elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}


but I get error I share above.



Any idea?



Update



how my html look likes



one



how the result **dd** look like



array:4 [▼
"_token" => "Wqs9yzd5qwGtbv01asbCzsISeVQxHsCoWVQM1ifO"
"brands" => array:1 [▼
0 => "3"
]
"min_price" => null
"max_price" => null
]


Blade



<div class="checkbox">
@foreach($option->subspecifications as $suboption)

@if($option->title == 'Brand')
<label for="brands">
<input name="brands" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@else
<label for="suboptions">
<input name="suboptions" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
@endif

@endforeach
</div>






php laravel count






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 5:12

























asked Nov 9 at 4:53









mafortis

930634




930634








  • 1




    What's your HTML look like? Is your input named brands so that it comes back as an array?
    – miken32
    Nov 9 at 5:05










  • @miken32 this is how it looks like ibb.co/gKDhvA
    – mafortis
    Nov 9 at 5:11










  • Edit your question to include the relevant HTML. I'm not going to click on that link.
    – miken32
    Nov 9 at 5:12










  • @miken32 updated! ......
    – mafortis
    Nov 9 at 5:15






  • 1




    laravel.com/docs/5.6/requests#retrieving-input
    – miken32
    Nov 9 at 5:32














  • 1




    What's your HTML look like? Is your input named brands so that it comes back as an array?
    – miken32
    Nov 9 at 5:05










  • @miken32 this is how it looks like ibb.co/gKDhvA
    – mafortis
    Nov 9 at 5:11










  • Edit your question to include the relevant HTML. I'm not going to click on that link.
    – miken32
    Nov 9 at 5:12










  • @miken32 updated! ......
    – mafortis
    Nov 9 at 5:15






  • 1




    laravel.com/docs/5.6/requests#retrieving-input
    – miken32
    Nov 9 at 5:32








1




1




What's your HTML look like? Is your input named brands so that it comes back as an array?
– miken32
Nov 9 at 5:05




What's your HTML look like? Is your input named brands so that it comes back as an array?
– miken32
Nov 9 at 5:05












@miken32 this is how it looks like ibb.co/gKDhvA
– mafortis
Nov 9 at 5:11




@miken32 this is how it looks like ibb.co/gKDhvA
– mafortis
Nov 9 at 5:11












Edit your question to include the relevant HTML. I'm not going to click on that link.
– miken32
Nov 9 at 5:12




Edit your question to include the relevant HTML. I'm not going to click on that link.
– miken32
Nov 9 at 5:12












@miken32 updated! ......
– mafortis
Nov 9 at 5:15




@miken32 updated! ......
– mafortis
Nov 9 at 5:15




1




1




laravel.com/docs/5.6/requests#retrieving-input
– miken32
Nov 9 at 5:32




laravel.com/docs/5.6/requests#retrieving-input
– miken32
Nov 9 at 5:32












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Solved



Changed count with has solved the issue



elseif($request->has['brands']){



PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
question. count error solved so I'll mark it as solved.




Thanks to miken32






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220062%2flaravel-count-issue%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Solved



    Changed count with has solved the issue



    elseif($request->has['brands']){



    PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
    question. count error solved so I'll mark it as solved.




    Thanks to miken32






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Solved



      Changed count with has solved the issue



      elseif($request->has['brands']){



      PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
      question. count error solved so I'll mark it as solved.




      Thanks to miken32






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Solved



        Changed count with has solved the issue



        elseif($request->has['brands']){



        PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
        question. count error solved so I'll mark it as solved.




        Thanks to miken32






        share|improve this answer












        Solved



        Changed count with has solved the issue



        elseif($request->has['brands']){



        PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
        question. count error solved so I'll mark it as solved.




        Thanks to miken32







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 5:46









        mafortis

        930634




        930634






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220062%2flaravel-count-issue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini