Duplicate classes after using foreach - php

I use a foreach to print out an attachment_image from a blog post. But for some reason a button duplicates the amount of posts and I dont know what I am doing wrong.
I am using sage 10 with bootstrap 5:
#php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
#endphp
<section class="masonry-grid">
<div class="container masonry__container">
<div class="masonry__items">
<div class="item masonry__item">
<div class="masonry__images">
#if ( empty($images) )
#else
#foreach ( array_unique($images) as $attachment_id => $attachment )
<div class="masonry__image">
{!! wp_get_attachment_image( $attachment_id, false ) !!}
</div>
#endforeach
#endif
</div>
<div class="masonry__info">
<div class="masonry__category">
{{ get_the_category( $id )[0]->name }}
</div>
<div class="masonry__title">
<a href="{{ get_permalink() }}">
{!! $title !!}
</a>
</div>
</div>
<div class="masonry__socials">
<div class="masonry__social">
<div classs="masonry__authors">
<i class="fas fa-at"></i><span class="masonry__author">#include('partials/entry-meta')</span>
</div>
<div class="masonry__views">
<i class="fas fa-eye"></i><span class="masonry__author">149 views</span>
</div>
<div class="masonry__likes">
<i class="far fa-thumbs-up"></i><span class="masonry__author">8 likes</span>
</div>
</div>
</div>
</div>
</div>
<div class="masonry__buttons">
<div class="masonry__loadmore">
<button type="button" class="masonry__button btn btn-outline-secondary"><i class="fas fa-spinner"></i>Load more</button>
</div>
</div>
</div>
</section>
I intend to display multiple items and a single button, yet, instead of the behavior I expect, as many buttons are displayed as many items there are.

Related

How to improve speed on my laravel website? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I built a website using Laravel and it's extremely slow.
This the code and the files for the website:
web.php:
Route::get('/product/{url}', 'FrontendController#getProduct')->where('url', '^[\w.-]*$');
FrontendController:
public function getProduct(String $url){
$product = Product::where('url', $url)->first();
$imgs = ProductImage::all()->where('product_id',$product->id);
return view('frontend.product', ['product' => $product, 'products' => Product::all(), 'images' => $imgs ]);
}
product.blade.php:
<div class="container-fluid display-products-desktop px-5" style="margin-top:15rem">
<div class="row mx-0 row-filters-desktop">
<div class="col-12 d-flex justify-content-end mb-5">
TALK WITH A DESIGNER
<!-- HOURS CONDITION -->
#php
$dt = new DateTime("now", new DateTimeZone('Portugal'));
#endphp
#if ($dt->format('H:i:s') >= '09:00:00' && $dt->format('H:i:s') <= '18:00:00')
<a class="cta-links" data-toggle="modal" data-target="#contact-five-minutes-modal">CALL ME IN 5 MINUTES</a>
#else
<a class="cta-links" data-toggle="modal" data-target="#contact-us-modal">CONTACT US</a>
#endif
</div>
</div>
<div class="row m-0">
<div class="col-2 p-0 thumb-div" style="border-right: 1px solid rgba(177, 177, 177, 0.8);">
<div class="swiper-container gallery-thumbs m-0">
<div class="swiper-wrapper">
#foreach ($images as $image)
<div class="swiper-slide thumbs"><img src="{{ $image->url }}" alt="" class="gallery-thumbs-slide"></div>
#endforeach
</div>
</div>
</div>
<div class="col-6 p-0">
<div class="swiper-container gallery-top m-0">
<div class="swiper-wrapper">
#foreach ($images as $image)
<div class="swiper-slide top"><img src="{{ $image->url }}" alt="" class="img-fluid"></div>
#endforeach
</div>
<div class="swiper-next-2 swiper-dark"></div>
<div class="swiper-prev-2 swiper-dark"></div>
</div>
</div>
<div class="col-4 p-0 margin-top-text" style="padding-left: 1.5rem !important">
<p class="product-page-pre-title">meet</p>
<h1 class="m-0 main-product-title" style="font-weight: 500">{{ $product->name }}</h1>
<h4 class="mb-5" style="font-weight: 500">by {{ $product->brand }}</h4>
<p class="paragraph-product-page">{{ $product->description }}</p>
<h5 class="mt-5 section-title-product-page">MATERIAL AND FINISHES:</h5>
<p class="paragraph-product-page">{{ $product->finishes }}</p>
<h5 class="mt-4 section-title-product-page">DIMENSIONS</h5>
<p class="paragraph-product-page m-0">{{ $product->dimensions }}</p>
<a class="cta-links" style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#downloadCatalogue" data-bs-toggle="modal" data-bs-target="#downloadCatalogue">DOWNLOAD CATALOGUE</a>
<a class="cta-links" style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#productSheet{{ $product->id}}" data-bs-toggle="modal" data-bs-target="#productSheet{{ $product->id}}">DOWNLOAD PRODUCT SHEET</a>
<a class="cta-links" style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#reqCustom{{ $product->id}}" data-bs-toggle="modal" data-bs-target="#reqCustom{{ $product->id}}">REQUEST CUSTOMIZATION</a>
<button class="product-price-btn" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->id }}">GET PRICE</button>
</div>
</div>
<div class="row mx-0">
<div class="col-12 p-0 text-center" style="margin-top:6rem">
<h1 class="m-5" style="font-weight: 600">Related Products</h1>
</div>
</div>
<div class="d-row d-flex gap-2 justify-content-center">
<div class="col-md-3 px-0">
<img src="{{ $product->related_product_img_1}}" alt="" class="img-fluid">
</div>
<div class="col-md-3 px-0">
<img src="{{ $product->related_product_img_2}}" alt="" class="img-fluid">
</div>
<div class="col-md-3 px-0">
<img src="{{ $product->related_product_img_3}}" alt="" class="img-fluid">
</div>
<div class="col-md-3 px-0">
<img src="{{ $product->related_product_img_4}}" alt="" class="img-fluid">
</div>
</div>
<div class="d-row d-flex gap-2 justify-content-center" style="margin-bottom: 5rem">
#php
$slug_1 = str_replace(' ', '-', $product->related_product_name_1);
$url_1 = strtolower( $slug_1 );
$slug_2 = str_replace(' ', '-', $product->related_product_name_2);
$url_2 = strtolower( $slug_2 );
$slug_3 = str_replace(' ', '-', $product->related_product_name_3);
$url_3 = strtolower( $slug_3 );
$slug_4 = str_replace(' ', '-', $product->related_product_name_4);
$url_4 = strtolower( $slug_4 );
#endphp
<div class="col-md-3 px-0">
<div class="text-center mt-1">
<p class="product-subtitle">new</p>
<h4 class="product-title">{{ $product->related_product_name_1}}</h4>
<a class="product-price-link" href="#getPrice{{ $product->related_product_id_1}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_1}}">get price</a>
</div>
</div>
<div class="col-md-3 px-0">
<div class="text-center mt-1">
<p class="product-subtitle">new</p>
<h4 class="product-title">{{ $product->related_product_name_2}}</h4>
<a class="product-price-link" href="#getPrice{{ $product->related_product_id_2}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_2}}">get price</a>
</div>
</div>
<div class="col-md-3 pr-0 pl-1">
<div class="text-center mt-1">
<p class="product-subtitle">new</p>
<h4 class="product-title">{{ $product->related_product_name_3}}</h4>
<a class="product-price-link" href="#getPrice{{ $product->related_product_id_3}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_3}}">get price</a>
</div>
</div>
<div class="col-md-3">
<div class="text-center mt-1">
<p class="product-subtitle">new</p>
<h4 class="product-title">{{ $product->related_product_name_4}}</h4>
<a class="product-price-link" href="#getPrice{{ $product->related_product_id_4}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_4}}">get price</a>
</div>
</div>
</div>
</div>
These are the results shown on a speed test website:
I am a beginner, so I´ve been studying ways to improve this. Do any of you have suggestions on how to improve the website performance? Thanks in advance.
Solved it by changing the following code and removing a few bootstrap modals from the product.blade.
FrontEndController:
public function getProductTest(String $url){
$product = Product::where('url', $url)->first();
$imgs = ProductImage::where('product_id',$product->id)->get();
return view('frontend.product-test', ['product' => $product, 'images' => $imgs ]);
}
The speed went from almost eleven seconds to 1.22 seconds.
First change the below code in your frontendController
$imgs = ProductImage::where('product_id',$product->id)->get();
This will improve your query loading time.
Then in the below code
return view('frontend.product', ['product' => $product, 'products' => Product::all(), 'images' => $imgs ]);
why are you passing all the products to blade
'products' => Product::all(),
This is a bad idea, and it must be slowing down your page
only get what you need. calling everything in database is really not a good idea

update a section of a view

I have a problem updating a section of a laravel view. that section is in another view and is filled with the data of a db. What I want to do is that when clicking on a link that content is filtered and the other parts of the page remain as they were.
thanks
coursesCategories.blade.php
#extends('courses')
#section('content')
#extends('courses')
#section('content')
<div class="tab-pane fade show active" id="pills-todas" role="tabpanel" aria-labelledby="pills-todas-tab">
<div class="row justify-content-sm-center">
#foreach($cursos as $curso)
<div class="col col-12 col-sm-12 col-md-6 col-lg-4 vista_curso">
<div class="card column_course">
<a><img class="card-img-top img_course" src="{{ asset('assets/images/pictureDefault.png') }}" alt="Foto de curso" width="240" height="135"></a>
<div class="card-body column_course_interna">
<div class="include">
<div class="card-title titulo_curso">
<h3><b>{{ $curso->nombre }}</b></h3>
</div>
<ul>
<li>
<i class="fas fa-file col-1"></i>
<span class="lista_contenido col-11"> 4 documentos explicativos </span>
</li>
<li>
<i class="fas fa-clock col-1"></i>
<span class="lista_contenido col-11"> 32 minutos </span>
</li>
</ul>
</div>
<div class="compra">
<div><button type="button" class="btn btn-lg btn_suscribe btn_ver" style="width: 84%;">Ver Curso</button></div>
<div><button type="button" class="btn btn-lg btn_suscribe" style="width: 84%;">Suscribirse</button></div>
</div>
<div class="texto_precio">
<span class="price">20.99€</span>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
#stop#stop
courses.blade.php
<div id="mostrar_cursos" class="container-fluid justify-content-center mostrar_cursos">
<div class="btn_filtros"> <button id="btn_filtros" type="button" class="btn btn-primary btn-sm btn-block d-md-none" onclick="viewfilter();ocult()">Filtrar</button></div>
<div class="row">
<div class="d-none d-md-block col col-md-4 col-lg-3 filter" style="max-width: 304px">
<div class="nav flex-column nav-pills" id="pills-tab" role="tablist" aria-orientation="vertical">
<a class="nav-link active" id="pills-todas-tab" href="{{ action('CoursesController#showList') }}" role="tab" aria-controls="pills-todas" aria-selected="true">Todas</a>
#foreach($categorias as $categoria)
<a class="nav-link" id="pills-ofimatica-tab" href="{{ action('CoursesController#showCourses', $categoria->nombre) }}" role="tab" aria-controls="pills-".$categoria aria-selected="false">{{ $categoria->nombre }}</a>
#endforeach
</div>
<div class="rango_precio">
<form>
<label for="customRange">Rango de precio</label>
<input type="range" class="custom-range" id="customRange">
</form>
<div id="result">Valor menor que:
<b id="valor"></b>
<strong>€</strong>
</div>
</div>
<div class="order_by">
<button id="btn_ordenar" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Ordenar por precio
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" type="button">Precio más alto</button>
<button class="dropdown-item" type="button">Precio más bajo</button>
</div>
</div>
</div>
<div class="col col-md-8 col-lg-9 row">
<div class="navegador_category container">
<div class="tab-content" id="pills-tabContent">
#yield('content')
</div>
</div>
</div>
</div>
</div>
coursesController
class CoursesController extends Controller
{
// list all categories and courses
public function showList() {
// categories extracted from the bd
$categorias = \App\Categoria::listaTodasLasCategorias();
// filtering courses according to html
$cursos = \App\Curso::listaTodosLosCursos();
return view('courses.coursesCategories',['cursos' => $cursos, 'categorias' => $categorias]);
}
// list all categories and courses
public function showCourses(Request $request, String $category) {
// categories extracted from the bd
$categorias = \App\Categoria::listaTodasLasCategorias();
// filtering courses according to html
$cursos = \App\Curso::listarCursosPorCategoria($category);
$view = View::make('courses.coursesCategories', ['cursos' => $cursos]);
if(Request::ajax()) {
$sections = $view->renderSections(); // returns an associative array of 'content', 'head' and 'footer'
return $sections['content']; // this will only return whats in the content section
}
return $view;
}
}
routes
Route::get('/courses', 'CoursesController#showList');
Route::get('/courses/{category}', 'CoursesController#showCourses');

Undefined variable: design_json_data (View: D:\xampp\htdocs\shopist\resources\views\pages\frontend\frontend-pages\frontend-designer.blade.php)

I get this error:
Undefined variable: design_json_data (View: D:\xampp\htdocs\shopist\resources\views\pages\frontend\frontend-pages\frontend-designer.blade.php)
There is a designer where I add photos and text, while in admin panel it works I cannot fix this on the front-end
#section('frontend-designer-html-content')
<div id="product_designer" class="container new-container">
#if(count($designer_hf_data)>0 && count($single_product_details['_product_custom_designer_data'])>0)
<br>
<div class="row">
<div class="col-sm-12">
#include('pages.common.designer-html', array('designer_hf_data' => $designer_hf_data, 'designer_img_elments' => $single_product_details['_product_custom_designer_data'], 'design_save_data' => $design_json_data))
</div>
</div>
<br>
<div class="row">
<div class="col-sm-12">
<h1 class="product-title">{{ $single_product_details['post_title'] }}</h1>
#if(count($attr_lists) >0 && count(get_product_variations_with_data($single_product_details['id']))>0)
<div class="product-pricing"><span class="solid-price">{!! get_product_variations_min_to_max_price_html($currency_symbol, $single_product_details['id']) !!} </span></div><hr>
#include('includes.frontend.variations-html', array('attr_lists' => $attr_lists, 'single_product_details' => $single_product_details))
#else
<div class="clearfix">
<div class="product-pricing">
#if(get_product_type($single_product_details['id']) == 'customizable_product' && count(get_product_variations($single_product_details['id'])) == 0 )
#if(!is_null($single_product_details['offer_price']))
<span class="offer-price">{!! price_html( $single_product_details['offer_price'] ) !!}</span>
#endif
<span class="solid-price">{!! price_html( $single_product_details['solid_price'] ) !!}</span>
#endif
</div>
<hr>
<button class="btn btn-sm btn-style cart customize-page-add-to-cart" type="button" data-id="{{ $single_product_details['id'] }}">
<i class="fa fa-shopping-cart"></i>
{{ trans('frontend.add_to_cart') }}
</button>
</div>
#endif
</div>
</div>
<input type="hidden" name="hf_custom_designer_data" id="hf_custom_designer_data" value="{{ $single_product_details['product_custom_designer_json'] }}">
<br>
#else
<br>
<div class="row">
<div class="col-sm-12">
<h5>{{ trans('frontend.no_content_yet') }}</h5>
</div>
</div>
#endif
</div>
#endsection
This is where it gives error
#include('pages.common.designer-html', array('designer_hf_data' => $designer_hf_data, 'designer_img_elments' => $single_product_details['_product_custom_designer_data'], 'design_save_data' => $design_json_data))

Trying to read from a database using laravel

I am trying to create a blog using Laravel 5.5 but I have received this error.
"Missing required parameters for [Route: posts.show] [URI: posts/{post}]. (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
On my postcontroller.php file. I did this so I can output all posts on my database:
public function index()
{
//Create a variable and store all the blog posts in it from the data base
$posts = Post::all();
//Return a view and pass in the above variable
return view('posts.index', array('posts' => $posts));
}
This is the loop I wrote on the /posts page to display all the blog posts.
<div class="events mb-100">
<div class="container">
<div class="row">
#foreach ($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show'), $post->id }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
I honestly don't know what I did wrong.
Update:
People have noticed there was a syntax issue and I have replaced {{ route('posts.show'), $post->id }} with {{ route('posts.show', $post->id) }}
#foreach($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show', $post->id) }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
#endforeach
However, I am now getting an "Invalid argument supplied for foreach() (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
<?php $__currentLoopData = $posts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $post): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<div class="col-md-6">
<a href="<?php echo e(route('posts.show', $post->id)); ?>">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2><?php echo e($post->title); ?></h2>
<p><?php echo e($post->main_body); ?></p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
You're using the wrong syntax. Change it to:
{{ route('posts.show', $post->id) }}
The error is here.
<a href="{{ route('posts.show'), $post->id }}">
It needs the ID to be part of the route. So it would be
<a href="{{ route('posts.show', $post->id) }}">

Retrieve Data in dynamic tabs

here i'm trying to click on any tabs to retrieve it's own data i did it well and it really retrieves the data of one raw only ?! any help?
<div class="col-lg-12">
<div class="tabs-container">
<ul class="nav nav-tabs">
#foreach($departments as $department)
<li class=""><a data-toggle="tab" href="#tab-{{ $department->id }}" aria-expanded="false">{{ $department->name }}</a></li>
#endforeach
</ul>
<div class="tab-content">
#foreach($works as $work)
<div id="tab-{{ $work->department_id }}" class="tab-pane">
<div class="panel-body">
<div class="row employees">
<div class="col-md-3">
<div class="thumbnail">
<div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
<div class="caption">
<h4>{{ $work->address }} </h4>
<p>{{ $work->body }}</p>
</div>
</div>
<a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
<button class="btn btn-danger demo4"> حذف
</button>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
<!-- -->
</div>
i don't need to make it ajax if anyone have a good reason for why it give me only one record for each department?
The reason why you get one record for department is that your loop create one tab panel per work, for example if you have 2 works (1,2) belongs to one department (1) the result would be 2 tabs with the same id set to 1:
departments:
id
-----
1
works:
id department_id
---------------------
1 1
2 1
Html result:
<div id="tab-1" class="tab-pane">
...
</div>
<div id="tab-1" class="tab-pane">
...
</div>
note that both tabs have the same id tab-1.
Solution
group works by department_id, something like this:
$works = Work::all()->groupBy('department_id');
and then in your view:
<div class="tab-content">
#foreach($works as $department_id => $department_works)
<div id="tab-{{ $department_id }}" class="tab-pane">
<div class="panel-body">
<div class="row employees">
#foreach($department_works as $work)
<div class="col-md-3">
<div class="thumbnail">
<div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
<div class="caption">
<h4>{{ $work->address }} </h4>
<p>{{ $work->body }}</p>
</div>
</div>
<a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
<button class="btn btn-danger demo4"> حذف </button>
</div>
#endforeach
</div>
</div>
</div>
#endforeach
</div>
Hope this helps

Categories