Shahzaiib's avatar

How can i get multiple dynamic products in dynamic pages on laravel

I am using laravel query builder not eloquent

0 likes
11 replies
Cronix's avatar

Not enough info to answer, really. Can you provide more details? Do you have any code to share?

2 likes
Shahzaiib's avatar

i am using shortcodes for products fetching process, but issue is that only single product worked fine, if i have more than one rows then page design fetched 2 times and i want to irate only sub div instead of full design, and i put the code here but msg length is too short.

1 like
RamjithAp's avatar

Show us the code where you iterate products loop.

1 like
munazzil's avatar

@Shahzaiib

try to post the code in the middle using of this example=

                                   Your code is here.
2 likes
Shahzaiib's avatar

here is my controller code

     function index(Request $request, $slug){
        //Query For Getting Pages 
        $query = DB::table('tbl_pages')
                     ->select('*')
                     ->where('slug', $slug)
                     ->where('status', 0);
        $content = $query->first();

        //check either page is exist or not
        if(!empty($content)){
            //check either page have shortcode or not
            $short_code_0 = strpos($content->content, '{type == 0}');

            //if short code exist
            if(!empty($short_code_0)){
                $content = ShortCodes::sale_products($content);
            }

            //Page Data
            $result = array(
                'page_title' => $content->title,
                'meta_keywords' => $content->meta_keywords,
                'meta_description' => $content->meta_description,
                'content' => $content->content,
            );
            
            //call page
            return view('site.pages.index', $result); 
        }else{
            print_r("<center><h4>Error 404 !!<br> The page you're looking for is not available<br> Please move back<h4></center>");
        }
    }
Shahzaiib's avatar

here is my helper code

     //here i am using Query For Getting Products
    //make html codes
    if(!empty($result)){
        //shortcodes
        $shortcodes[] = [
            '{id}','{p_name}','{p_slug}','{b_name}','{b_slug}','{img_src}','{sale_price}','{regural_price}','{sale_tag}','{featured_tag}','{rating}','{type == 0}',
        ];

        foreach($result as $row){
            if($row->featured_product == 0){
                $featured = '<span class="tg-themetag" style="margin-left: 15px;">Featured</span>';
            }else{
                $featured = '';
            }

            //shortcodes_data
            $shortcodes_data[] = [
                '{id}' => $row->p_id,
                '{p_name}' => $row->p_name,
                '{p_slug}' => $row->p_slug,
                '{b_name}' => $row->b_name,
                '{b_slug}' => $row->b_slug,
                '{img_src}' => URL('public/assets/user/images/ecommerce/products/'.$row->featured_image),
                '{sale_price}' => $row->sale_price,
                '{regural_price}' => $row->regural_price,
                '{sale_tag}' => 'Sale',
                '{featured_tag}' => $featured,
                '{rating}' => '5',
                '{type == 0}' => ' ',
            ];
        }
    } 
    $content->content = str_replace($shortcodes, $shortcodes_data, $content->content);
    return $content;
Shahzaiib's avatar

and this code that is stored in table

         <div class="main">
                  <div class="child">
                           {id}
                           {p_name}
                           {p_slug}
                           {b_name}
                           {b_slug}
                           {img_src}
                           {sale_price}
                           {regural_price}
                           {sale_tag}
                           {featured_tag}
                           {rating}
                           {type == 0}   
                 </div>
         </div>
Shahzaiib's avatar

and i want to just iterate child div after every product, not whole column

RamjithAp's avatar

If you want to loop only child class div then in your content table you must save only this

       <div class="child">
                           {id}
                           {p_name}
                           {p_slug}
                           {b_name}
                           {b_slug}
                           {img_src}
                           {sale_price}
                           {regural_price}
                           {sale_tag}
                           {featured_tag}
                           {rating}
                           {type == 0}   
                 </div>

also your controller should loop the content as below

     //here i am using Query For Getting Products
    //make html codes
    if(!empty($result)){
        //shortcodes
        $shortcodes[] = [
            '{id}','{p_name}','{p_slug}','{b_name}','{b_slug}','{img_src}','{sale_price}','{regural_price}','{sale_tag}','{featured_tag}','{rating}','{type == 0}',
        ];
        $temp_content = '';
        foreach($result as $row){
            if($row->featured_product == 0){
                $featured = '<span class="tg-themetag" style="margin-left: 15px;">Featured</span>';
            }else{
                $featured = '';
            }

            //shortcodes_data
            $shortcodes_data[] = [
                '{id}' => $row->p_id,
                '{p_name}' => $row->p_name,
                '{p_slug}' => $row->p_slug,
                '{b_name}' => $row->b_name,
                '{b_slug}' => $row->b_slug,
                '{img_src}' => URL('public/assets/user/images/ecommerce/products/'.$row->featured_image),
                '{sale_price}' => $row->sale_price,
                '{regural_price}' => $row->regural_price,
                '{sale_tag}' => 'Sale',
                '{featured_tag}' => $featured,
                '{rating}' => '5',
                '{type == 0}' => ' ',
            ];
        $temp_content. =  str_replace($shortcodes, $shortcodes_data, $content->content);
        }
    } 
    $content->content = $temp_content;
    return $content;
1 like

Please or to participate in this conversation.