Class Closure的对象无法在Laravel中转换为整数。

huangapple go评论69阅读模式
英文:

Object of class Closure could not be converted to int in laravel

问题

I see that you're facing an issue with pagination and getting an error related to a Closure in your Laravel application. To resolve this issue, you need to ensure that you're correctly paginating the results.

It appears that the error is related to the following line in your controller:

$housescategory->whereIn('id', function($q) use ($hsetag_id) {
    $q->select('rental_id')
        ->from('rentalhouse_tags')
        ->where('tag_id', $hsetag_id);
});

Make sure that $housescategory is a valid query builder instance and that $hsetag_id is defined and has the expected value.

Additionally, you should ensure that your pagination is set up correctly in your controller method. It seems like you're already using paginate(4) to paginate the results, which is correct. However, you should double-check that the pagination is functioning as expected.

If the issue persists, consider providing more specific details about the error message and the data being used so that I can assist you further.

英文:

am trying to paginate the results of the filter function but am getting this error Object of class Closure could not be converted to int. The function works this way upon selecting an option all the houses which hs those tags are shown.am achieving this using ajax,laravel and jquery.i have been able to do this but the issue am getting is when i try to paginate the results usingthis function i get the above error.here is my function in the view where am calling the paginate function

$(document).on('click', '.pagination a', function(event){
 event.preventDefault();
 var page = $(this).attr('href').split('page=')[1];
 getMoreHouses(page);
});

//function for getting the results from the response and adding them to the pages in the pagination 

function getMoreHouses(page)
        {
            var hseurl=$("#url").val();
            var hsetag = $('input[class^="tagtitle"]').filter(":checked").val();

            $.ajax({
                type:"GET",
                url:'{{ route("rentalcategory.get-more-houses") }}' + "?page=" + page,
                data:{
                    url:hseurl,
                    rentaltag:hsetag,

                },
                success:function(data)
                {
                    $('.showrentalhouses').html(data);
                }
            })
        }

here is my function in the controller

 public function get_more_houses(Request $request)
{
    if($request->ajax()){

        $hsedata=$request->all();

        $rentalcaturl=$hsedata['url'];
        
        $rentalcatcount=Rental_category::where(['rentalcat_url'=>$rentalcaturl,'status'=>1])->count();

        if($rentalcatcount>0){

            $rentalcategorydetails=Rental_category::rentalcategorydetails($rentalcaturl);

            $housescategory=Rental_house::whereIn('rentalcat_id',$rentalcategorydetails['catids'])
            ->where(['rental_status'=>1,'is_extraimages'=>1,'is_rentable'=>1])->orderby('id','DESC')->paginate(4);
            $rentalcategories=Rental_category::withCount('rentalhses')->get();

            $rentaltags=Rental_tags::with('tagshouse')->get();

            $rentallocations=Location::where(['status'=>1])->get();

            // get the rental tag results into a pagination

            if(isset($hsedata['rentaltag']) && !empty($hsedata['rentaltag'])){

                $hsetag_id = $hsedata['rentaltag'];

                    
                $housescategory
                    ->whereIn('id', function($q) use ($hsetag_id)
                {
                    $q
                    ->select('rental_id')
                    ->from('rentalhouse_tags')
                    ->where('tag_id', $hsetag_id);
                });

                
            }
        
            return view('Front.Rentalslisting.rentalhsesjson',compact('rentalcaturl','rentallocations','rentalcatcount','rentalcategorydetails','rentaltags','housescategory','rentalcategories'))->render();
        }
    }
}

how can i work out this one

答案1

得分: 1

你应该在函数底部调用paginate方法:

public function get_more_houses(Request $request)
{
    if($request->ajax()){
        $hsedata = $request->all();

        $rentalcaturl = $hsedata['url'];

        $rentalcatcount = Rental_category::where(['rentalcat_url'=>$rentalcaturl,'status'=>1])->count();

        if ($rentalcatcount>0){

            $rentalcategorydetails = Rental_category::rentalcategorydetails($rentalcaturl);

            $housescategoryQuery = Rental_house::whereIn('rentalcat_id',$rentalcategorydetails['catids'])
                ->where(['rental_status'=>1,'is_extraimages'=>1,'is_rentable'=>1]);

            $rentalcategories=Rental_category::withCount('rentalhses')->get();

            $rentaltags=Rental_tags::with('tagshouse')->get();

            $rentallocations=Location::where(['status'=>1])->get();

            // 将租赁标签结果分页显示

            if(isset($hsedata['rentaltag']) && !empty($hsedata['rentaltag'])){

                $hsetag_id = $hsedata['rentaltag'];

                $housescategoryQuery
                    ->whereIn('id', function($q) use ($hsetag_id)
                    {
                        $q
                            ->select('rental_id')
                            ->from('rentalhouse_tags')
                            ->where('tag_id', $hsetag_id);
                    });


            }

            $housescategory = $housescategoryQuery->orderby('id','DESC')->paginate(4);

            return view('Front.Rentalslisting.rentalhsesjson',compact('rentalcaturl','rentallocations','rentalcatcount','rentalcategorydetails','rentaltags','housescategory','rentalcategories'))->render();
        }
    }
}
英文:

You should call paginate method at the bottom of the function:

public function get_more_houses(Request $request)
{
if($request->ajax()){
$hsedata = $request->all();
$rentalcaturl = $hsedata['url'];
$rentalcatcount = Rental_category::where(['rentalcat_url'=>$rentalcaturl,'status'=>1])->count();
if ($rentalcatcount>0){
$rentalcategorydetails = Rental_category::rentalcategorydetails($rentalcaturl);
$housescategoryQuery = Rental_house::whereIn('rentalcat_id',$rentalcategorydetails['catids'])
->where(['rental_status'=>1,'is_extraimages'=>1,'is_rentable'=>1]);
$rentalcategories=Rental_category::withCount('rentalhses')->get();
$rentaltags=Rental_tags::with('tagshouse')->get();
$rentallocations=Location::where(['status'=>1])->get();
// get the rental tag results into a pagination
if(isset($hsedata['rentaltag']) && !empty($hsedata['rentaltag'])){
$hsetag_id = $hsedata['rentaltag'];
$housescategoryQuery
->whereIn('id', function($q) use ($hsetag_id)
{
$q
->select('rental_id')
->from('rentalhouse_tags')
->where('tag_id', $hsetag_id);
});
}
$housescategory = $housescategoryQuery->orderby('id','DESC')->paginate(4);
return view('Front.Rentalslisting.rentalhsesjson',compact('rentalcaturl','rentallocations','rentalcatcount','rentalcategorydetails','rentaltags','housescategory','rentalcategories'))->render();
}
}
}

huangapple
  • 本文由 发表于 2023年4月13日 17:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定