In my accordion I am showing the parent items. Which I am getting by using getItems() method. It is being linked to every item. And every parent item has some child items. Which I am getting using ($item->children as $child) And when clicks any parent item, the child items of that parent item should be visible from drop down.
The problem is whenever I click any parent item, every child item is being shown in the drop down. But I want only the child item of the parent item to be shown. It would be nice If someone can help me out. Thanks in advance.
This is my blade file code.
<div class="container my-4">
<div class="accordion md-accordion" id="accordionEx" role="tablist" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" id="headingTwo2">
#foreach(getItems() as $item)
<a class="collapsed" data-toggle="collapse" data-parent="#accordionEx" href="#collapseTwo2"
aria-expanded="false" aria-controls="collapseTwo2">
<h5 class="mb-0">
{{ $item->name }} <i class="fas fa-angle-down rotate-icon"></i>
</h5>
</a>
<div id="collapseTwo2" class="collapse" role="tabpanel" aria-labelledby="headingTwo2"
data-parent="#accordionEx">
<div class="card-body">
#foreach ($item->children as $child)
<a href="{{ route('category.island.list') }}?category={{ $child->id }}"
class="dropdown-item" data-id="{{ $child->id }}" data-name="{{ $child->name }}">
<span>{{ $child->name }}</span>
</a>
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
This is normal, because they all have the same href here:
<a class="collapsed" data-toggle="collapse" data-parent="#accordionEx" href="#collapseTwo2"
aria-expanded="false" aria-controls="collapseTwo2">
and they all have make reference to the same "id" in this div:
<div id="collapseTwo2" class="collapse" role="tabpanel" aria-labelledby="headingTwo2"
data-parent="#accordionEx">
So just add a variable in your foreach as a "count" that will increase foreach loop and you just add that into your href and Id with twig and that is fine :)
Twig way:
Out of 1st foreach : TWIG : {% set count = 0 %} PHP : <?php $count = 0; ?>
In your 1st foreach at the end: TWIG : {% set count = count + 1 %} PHP : <?php $a++; ?>
As follow:
<div class="container my-4">
<div class="accordion md-accordion" id="accordionEx" role="tablist" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" id="headingTwo2">
<?php $count = 0; ?>
#foreach(getItems() as $item)
<a class="collapsed" data-toggle="collapse" data-parent="#accordionEx" href="#collapseTwo2{{ $count }}"
aria-expanded="false" aria-controls="collapseTwo2{{ $count }}">
<h5 class="mb-0">
{{ $item->name }} <i class="fas fa-angle-down rotate-icon"></i>
</h5>
</a>
<div id="collapseTwo2{{ $count }}" class="collapse" role="tabpanel" aria-labelledby="headingTwo2"
data-parent="#accordionEx">
<div class="card-body">
#foreach ($item->children as $child)
<a href="{{ route('category.island.list') }}?category={{ $child->id }}"
class="dropdown-item" data-id="{{ $child->id }}" data-name="{{ $child->name }}">
<span>{{ $child->name }}</span>
</a>
#endforeach
</div>
</div>
<?php $count++; ?>
#endforeach
</div>
</div>
</div>
</div>
Related
Tried all the possible solution to refresh the component but failed. Is there any option not to push the new comments and refresh the $comments variable whatever it contains?
Want to refresh $comments and its relation with replies in the blade on click of the send button
Calling $refresh action on the send button but it doesn't seem to work. Even tried to re-render the component with $this->render(). This also fails.
<div>
#foreach($comments as $item)
<div class="row" #if($item->reply_id != null) style="margin-left:40px;" #endif>
<div class="col-md-12">
<div class="py-md-4 py-3 border_bottom" >
<div class="row">
<div class="col-md-1 col-sm-1 col-1">
<div class="rating_person pt-3">
<img src="{{ isset($item->owner->profile_picture) ? asset($item->owner->profile_picture) : asset('images/default.jpg') }}">
</div>
</div>
<div class="col-md-11 col-sm-12 col-12">
<div class="rating_view_here_content_single_border">
<div class="rating_view_here_content_single">
<div class="rating_view_stars d-flex justify-content-between pt-3">
<div class="rating_view_heading">
<h4>{{ $item->owner->name }}</h4>
<div class="rating_wrapper">
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
</div>
</div>
<span class="rating_days">{{ \App\Library\Helper::getTime($item->created_at) }}</span>
</div>
<div class="rating_comment_here pt-3">
<p>{{ $item->comment }}</p>
<p>
<a href="javascript:void(0)" class="reply" data-id="{{ $item->id }}" wire:click="toggleCommentBox({{ $item->id }}, 'true')">
<i class="fa fa-reply" aria-hidden="true"></i> <span>Reply</span>
</a>
</p>
</div>
#if($item_id == $item->id && $show=="true")
<div class="video_inner_form_wrapper commentBox-{{ $item->id }}">
<form wire:submit.prevent="sendReply" class="row">
<input type="hidden" name="reply_id" value="{{ $item->id }}">
<div class="comment_input col-md-8">
<input type="text" class="bluish_label video_inner_input #error('replyText') is-invalid #enderror" wire:model.lazy="replyText" placeholder="Write your comment here">
#error('replyText')
<div class="invalid-feedback">{{$message}}</div>
#enderror
</div>
<div class="col-md-4">
<button type="submit" class="btn_edit text-uppercase" wire:click="$refresh">Send</a>
<button type="button" class="btn_edit text-uppercase cancel" wire:click="toggleCommentBox({{ $item->id }}, 'false')">Cancel</a>
</div></form>
</div>
#endif
</div>
</div>
</div>
</div>
</div>
<livewire:frontend.discussion-chat :key="'chat-' . $item->id" :comments="$item->replies" :id="$model_id" :type="$model_type" />
</div>
</div>
#endforeach
</div>
Since none of the properties is updated, the livewire chat component will not refresh.
Possible solution is not to pass the list as a param but fetch it inside the chat component. Use events to trigger a refresh:
#click="$emit('discussion.chat.{{ $id }}.refresh')"
Inside frontend.discussion-chat have a
public function listeners(): array
{
return [
"discussion.chat.$this->id.refresh" => "reload"
];
}
public function reload(): void
{
// E.g.
$this->comments->fresh();
}
I am learning PHP Laravel and I am developing shopping cart project. I am able to add the product into list but, when I click shopping cart link. I am getting following error.
Facade\Ignition\Exceptions\ViewException
Invalid argument supplied for foreach() (View: C:\Users\Khundokar Nirjor\Desktop\laravel Tutorial\shopping-cart\resources\views\shop\shopping-cart.blade.php)
Here is my Router
Route::get('/shopping-cart',[
'uses' => 'ProductController#getCart',
'as' => 'product.shopppingCart'
]);
Here is my header.blade.php code.
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a href="{{route ('product.shopppingCart')}}"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
<span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
</a>
</li>
<li class="dropdown">
<i class="fa fa-user" aria-hidden="true"></i> User Mangemetn <span class="caret"></span>
<ul class="dropdown-menu">
#if(Auth:: check())
<li>User Profile</li>
<li>Logout</li>
#else
<li>Sign Up</li>
<li>Sign In</li>
#endif
<li role="separator" class="divider"></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
Here is my shopping-cart.blade.php
#extends('layouts.master')
#section('title')
Laravel Shopping Cart
#endsection
#section('content')
#if(Session::has('cart'))
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<ul class="list-group">
#foreach($products as $product)
<li class ="list-group-item">
<span class ="badge"> {{ $product['qty'] }}</span>
<strong >{{ $product['item']['title']}}</strong>
<span class ="label label-success"> {{ $product['price']}}</span>
<div class="btn-group">
<button type="button" class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">Action<span class="caret"></span></button>
<ul class="dropdown-menu">
<li> Reduce By 1 </li>
<li> Reduce All </li>
</ul>
</div>
</li>
#endforeach
</ul>
</div>
</div>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<strong > Total : {{ $totalPrice}}</strong>
</div>
</div>
<hr>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<button type="button" class="btn btn-success">Checkout</button>
</div>
</div>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<strong>Total : {{$totalPrice}} </strong>
</div>
</div>
<hr>
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<button type="button" class ="btn-btn-success">Checkout</button>
</div>
</div>
#else
<div class ="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<h2>No Items In Cart</h2>
</div>
</div>
#endif
#endsection
Here is my controller code
public function getCart()
{
if(!Session :: has('cart') ){
return view ('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view ('shop.shopping-cart' , ['products' => $cart->items, 'totalPrice'=>$cart->totalPrice]);
}
Here is screen shot when I clicked the shopping-cart link
Try like this
#if(!empty($products) && count($products) > 0)
#foreach($products as $product)
// your code here
#endforeach // typo in foreach fixed
#endif
You're getting this error cuase of sometime your products array is empty. So whenever you call foreach make sure to check the array is not empty.
#if(!empty($products)
#foreach($products as $product)
//code here
#endforech
#endif
Make your code like this your error will be gone.
I have a page "https://proj.test/user/profile?user=2#myRegistrations" where I have two tabs, one tab show the past registrations of a user in conferences and other tab the next registrations of the user in a conference. The user can click "Past Registrations" to check the past registrations and click in "Next Registrations" to check his next registrations in conferences. And its working.
In this same page, above the tabs, I have a form for the user to search for a conference name where he his registered. But when the user enters some text in the form search input like "test" and click in "Search" it appears "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$participants (View: /Users/john/projects/proj/resources/views/users/index.blade.php)". Do you know what can be the issue?
Code in he view:
<div class="tab-pane clearfix fade" id="myTickets" role="tabpanel" aria-labelledby="contact-tab">
<div class="d-flex justify-content-between">
<form method="get" class="clearfix w-75" method="POST" action="{{ route('user.searchRegistration') }}">
{{ csrf_field() }}
<div class="form-group col col-md-6 px-0">
<div class="input-group" data-provide="datepicker">
<input type='text' class="form-control" placeholder="Search Registrations"
name="search_registration"
value="{{old('search_registration')}}"/>
<button class="input-group-addon">Search</button>
</div>
</div>
</form>
<a href="{{route('user.cleanSearchRegistration')}}" class="btn btn-outline-primary"
id="cleanSearchRegistration">Clean Search</a>
</div>
#if(session('searchedRegistrations'))
#if(session('searchedRegistrations')->count() != null)
#foreach(session('searchedRegistrations') as $registration)
#foreach(session('searchedRegistrations')->participants as $participant)
#if(!empty($registration))
<li class="list-group-item">
<h5>{{optional($registration->conference)->name}}</h5>
#if ($registration->status == 'C')
<a href="{{route('conference.registrationInfo',
['id' => $registration->conference->id,
'slug' => $registration->conference->slug,
'regID'=> $registration->id])}}"
class="btn btn-primary ml-2"><i class="fa fa-file-pdf-o"></i> Registration document
</a>
#endif
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',
[
'regID'=> $registration->id])}}"
class="btn btn-primary ml-2"><i class="fa fa-file-pdf-o"></i> Download certificate
</a>
#break
#endif
</li>
#endif
#endforeach
#endforeach
<div class="text-center d-flex justify-content-center mt-3">
{{session('searchedRegistrations')->fragment('searchRegistrations')->links("pagination::bootstrap-4")}}
</div>
#else
<div class="alert alert-info" role="alert">
<i class="material-icons">info</i>
<span>Your search didnt return any result.</span>
</div>
#endif
#else
<div class="d-flex mb-3">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active border" href="#nextConferences" data-toggle="tab" role="tab">Next
Conferences</a>
</li>
<li class="nav-item">
<a class="nav-link border" href="#PastRegistrations" data-toggle="tab" role="tab">Past
Conferences</a>
</li>
</ul>
</div>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active clearfix" id="nextConferences" role="tabpanel"
aria-labelledby="home-tab">
<ul class="list-group">
#foreach($nextRegistrations as $nextRegistration)
#foreach($nextRegistration->participants as $participant)
#if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
<li class="list-group-item">
<h5>{{optional($nextRegistration->conference)->name}}</h5>
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',
[
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Download Certificate</a>
#break
#endif
#if ($nextRegistration->status == 'C')
<a href="{{route('conferences.registrationInfo',
[
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Registration document
</a>
#endif
</li>
#endif
#endforeach
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$nextRegistrations->fragment('nextConferences')->links("pagination::bootstrap-4")}}
</div>
</div>
<div class="tab-pane fade show clearfix" id="pastConferences" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group">
#foreach($pastRegistrations as $pastRegistration)
#foreach($pastRegistration->participants as $participant)
#if(!empty($pastRegistration->conference) || !empty($pastRegistration->conference->start_date))
<li class="list-group-item">
<h5>{{optional($pastRegistration->conference)->name}}</h5>
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',['regID'=> $pastRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Download Certificate
</a>
#break
#endif
#if ($pastRegistration->status == 'C')
<a href="{{route('conferences.registrationInfo',
[
'regID'=> $pastRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Registration info
</a>
#endif
</li>
#endif
#endforeach
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$pastRegistrations->fragment('pastConferences')->links("pagination::bootstrap-4")}}
</div>
</div>
</div>
</div>
I assume session('searchedRegistrations') is the Pagination instance. There is no property named participants nor would there be any random properties (it is a container, not what it contains). I would imagine a model instance contained in the collection might have a participants property though.
#foreach(session('searchedRegistrations') as $registration)
#foreach(session('searchedRegistrations')->participants as $participant)
I am guessing that should be
#foreach($registration->participants as $participant)
i have a page "deposits" here are plans are availble with the use of "foreach"
now i wnt to show only plan number 3 here. mean plan of id 3.
<div class="row">
#foreach($plan as $p)
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
#endforeach
</div>
You can simply extract the plan from your $plan collection , does not need loop in below case
$p = $plan ->find(3);
if you want to use foreach and show plan id 3 , then try this :
<div class="row">
#foreach($plan as $p)
#if($p == 3)
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
</div>
But i suggest not to use foreach as you already know you need to show only plan id 3 . Try this :
<div class="row">
<?php $p = $plan["your_desired_id"]; ?> // means 2 or 3 .. whatever your desired id
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
</div>
If you need to find out a single value of plan from the collection .
You can just find it by it's plan id
$plan = Plan::find(3);
Write this in your controller and then pass it down to your view blade file by using with.
Example :
return view("blade file name")->with(['plan'=>$plan])
Later in your blade file just access it's fields.
{{$plan->field_name}}
If I was looping through a number of results in a foreach and the list was as such -
-item 1
delete
-item 2
delete
-item 3
delete
-item 4
delete
If I want to delete the item 3 record how can I find the correct id for that iteration for my query. Normally I would use a get request but that wouldn't work in this case.
Thanks
#foreach ($statuses as $status)
<div class="media post-margin">
<a class="pull-left" href="{{ route('profile.index', ['username' => $status->user->username]) }}">
<img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src="{{ $status->user->getAvatarUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $status->user->getNameOrUsername() }}</h4>
<span>{{ $status->created_at->diffForHumans() }}</span>
</div>
<p>{{ $status->body }}</p>
<hr>
<ul class="list-inline">
<li><i class="fa fa-thumbs-up fa-liked"></i> {{ $status->likes->count() }} {{ str_plural('like', $status->likes->count()) }}</li>
<li><i class="fa fa-comments-o"></i> Comment</li>
<li><i class="fa fa-retweet"></i> Share</li>
#if ($status->user->id === Auth::user()->id)
<li id="remove"><a href="#"><i class="fa fa-times"></i> Delete</li>
#endif
</ul>
<hr>
</div>
#endforeach
You just need to pass the record id to be deleted.
#foreach ($statuses as $status)
<div class="media post-margin">
<a class="pull-left" href="{{ route('profile.index', ['username' => $status->user->username]) }}">
<img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src="{{ $status->user->getAvatarUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $status->user->getNameOrUsername() }}</h4>
<span>{{ $status->created_at->diffForHumans() }}</span>
</div>
<p>{{ $status->body }}</p>
<hr>
<ul class="list-inline">
<li><i class="fa fa-thumbs-up fa-liked"></i> {{ $status->likes->count() }} {{ str_plural('like', $status->likes->count()) }}</li>
<li><i class="fa fa-comments-o"></i> Comment</li>
<li><i class="fa fa-retweet"></i> Share</li>
#if ($status->user->id === Auth::user()->id)
<li id="remove"><a href="{{route('status.delete',['statusId',$status -> id])}}"><i class="fa fa-times"></i> Delete</li>
#endif
</ul>
<hr>
</div>
#endforeach
Routes
route::get('/delete/{$id}',['as' =>'status.delete' , 'uses' => 'YourController#delete']);
Controller
public function delete($id){
Model::destroy($id); //
}
Hope this help.