mercredi 10 novembre 2021

How to Fetch Dynamic Id of an object and show data related to it in Laravel

So let me give you an overview of the problem I am facing. I am working on a project which has handpicked project as shown below on home page.click to see image

So now I want to show all the associated properties with the specific projects like this click to see image

I have a databse table named "properties" which has a column "project_id". I want this "project_id" to be dynamic . Right Now I am entering the project id manually i.e. 1,2,3,4.

This is the ajax I am using in my blade file

<script>
     $(document).ready(function(){
        getProperties();
        function getProperties() {
            $.ajax({
        url:'/handpickedproperties/{$id}',
        type:'POST',
        data:{'property_type':property_type},
        success:function(response){
            if(response.success){
                var str = '';
                stringlength = 135;
                if($( window ).width()<1363)
                    var stringlength = 100;
                $.each(response.data, function(k,v){
                    description = v.description;
                    if (description.length > stringlength)
                        description = description.substr(0,stringlength)+'.....more';
                    photos = JSON.parse(v.images);
                    if(jQuery.isEmptyObject(photos)){
                        image_count = 0;
                        cover_photo = '/images/default/photo not available.png';
                    }
                    else{
                        image_count = Object.keys(photos['withPhotos']).length;
                        cover_photo = photos.coverPhoto;
                    }
                    area_conversion = JSON.parse(v.area_conversions);
                    str += '<div class="property-box" data-aos="fade-up">\
                                <div class="pic-area">\
                                    <figure><img src="'+cover_photo+'">\
                                        <span class="photo-count">\
                                        <img src="images/photos-icon.png">'+image_count+'</span>\
                                    </figure>\
                                </div>\
                                <div class="listing-property-details">\
                                    <a href="/viewPropertyDetails/'+v.id+'">\
                                        <h5>'+v.name+' in '+v.address+'</a><a href="javascript: void(0)" onclick="addFavourite(`'+v.id+'`)" class="wishlist" style="margin-top:0"><img src="/images/home/heart2.png" id="pic_'+v.id+'"></a><a href="/viewPropertyDetails/'+v.id+'"></h5>\
                                        <span class="price-box">\
                                            <strong>'+v.currency+v.price+v.price_unit+'</strong>\
                                            '+v.currency+' '+v.price_per_area+'/sq.ftadsds.\
                                        </span>\
                                        <span class="area-box">\
                                            <img src="/images/listing-area.png">\
                                            <strong><span class="sq_ft" style="width:75%;display:inline-block;">'+v.area_per_sq_ft+' sq.ft.</span>\
                                            <span class="sq_mt" style="width:75%;display:none;">'+area_conversion.sq_mt+' sq.mt.</span>\
                                            <span class="sq_yrd" style="width:75%;display:none;">'+area_conversion.sq_yrd+' sq.yrd.</span></strong>\
                                            ('+v.carpet_area+') Carpet Area\
                                        </span>\
                                        <span class="bed-box">\
                                            <img src="/images/listing-bed.png">\
                                            <strong>'+v.bedroom+' BHK</strong>\
                                        </span>\
                                        <span class="bath-box">\
                                            <img src="/images/listing-bath.png">\
                                            <strong>'+v.bathroom+' Bath</strong>\
                                        </span>\
                                        <div class="clear"></div>\
                                        <p class="property-brief" style="color:black;">'+description+'</p></a>';
                    if(v.featured == 1)
                        str += '<label style="margin-right: 5px;" class="featured-highlight-buttton">Featured</label>';
                    if(v.verified == 1)
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Verified</label>';
                    if(v.construction_status == 3)                      
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Under Construction</label>';
                    if(v.construction_status == 4)
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Ready to Move</label>';
                                // <label class="highlight-buttton">Resale</label>
                    str += '</div>\
                                <div class="listing-box-bottom">\
                                    <span>\
                                        Posted on '+v.creation_date+' by '+v.created_by+'\
                                        <a href="/total-listed-properties" target="_blank"><strong>'+v.posted_by+'</strong></a>\
                                    </span>\
                                    <a href="#" onClick = "contactDetails(`'+v.id+'`);">Contact '+v.posted_by+'</a>\
                                </div>\
                            </div>';
                });
                $('#results_number').empty();
                $('#results_number').append(Object.keys(response.data).length);
                $('#property-data').append(str);
                $('#no-result-found-div').hide();
            }
            else{
                $('#property-data').empty();
                $('#results_number').empty();
                $('#results_number').append(0);
                $('#no-result-found-div').show();
            }
        }
    });
        }
     });   
</script>

enter code here

This is my controller Code

 public function handpickedProperties(Request $request,$id)
  
    {
        try
        {
            $data           = $request->all();
            $validator      = Validator::make($data, [
            ]);
            if ($validator->fails())
                return $this->sendError("Validation Failure",$validator->errors(),403);

            $price_min = 0;
            $price_max = 10000000;
            $search = PropertyServices::fetchHandpickedProperties($request->search,$request->type,$request->city,$request->property_type,$price_max,$price_min,$id);
            if(is_array($search))
            return view('site.handpicked-properties');
            else if($search == 0)
                return $this->sendError('Property not found',[],206);
            else if($search == null)
                return $this->sendError('Property not found. Due to Exception in Service',[],500);
        }
        catch(Exception $e)
        {
            Log::error('Exception encountered. PropertyController->searchProperties Exception: '.$e);
            return $this->sendError('Property not found. Due to Exception.',[],500);
        }
    }
enter code here

This is the code I have used in listing

public function fetchHandpickedProperties($search,$type,$city,$property_type,$price_max,$price_min,$id)
    {
        {
            try
            {
               
                $result = Property::where('project_id','2')
                                    ->select('description','images','id','address','name','currency','price','price_unit','price_per_area','area_per_sq_ft','area_conversions','bedroom','bathroom','created_by','posted_by',\DB::raw('DATE_FORMAT(created_at,"%M-%d-%Y") as creation_date'),'carpet_area','verified','construction_status','featured','lat','long')
                                    ->get()->toArray();
                    
                return $result;

            }
            catch(Exception $e)
            {
                Log::error('Exception encountered. PropertyServices->searchProperty Exception: '.$e);
                return $result = null;
            }
        }
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3HanBYz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire