Display data in a treeviewDisplay data in a treeview - laravel

I'm Working on Laravel.
I'm trying to display my data in a treeview. But i success only the first step. Someone of you can help please ?
DataBase Structure
My achievement
Code:
<button class=" btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse" aria-expanded="false" aria-controls=".multi-collapse" >
<span class="glyphicon glyphicon-eye-open"></span></button>
#foreach(App\Account::where('nomencloture', 'LIKE', '_')->get() as $item)
<div class="collapse multi-collapse" id="{{$item->nomencloture}}" >
<div class="list-group-item " >
<div class="col-md-2"> {{ $item->account_id }}</div>
<div class="col-md-2"> {{ $item->account_name }}</div>
<button class=" btn-primary" type="button" data-toggle="collapse" data-target="#PUT SOMETHINGHERE" aria-expanded="false" aria-controls="multiCollapseExample1" >
<span class="glyphicon glyphicon-eye-open"></span></button>
<br>
<br>
#foreach(App\Account::where('nomencloture', 'LIKE', '_______')->get() as $subitem)
<div class="collapse" id="{{ $subitem->nomencloture }}" >
<br>
<div class="list-group-item ">
<div class="col-md-2"> {{ $subitem->account_id }}</div>
<div class="col-md-2"> {{ $subitem->account_name }}</div>
</div>
</div>
#endforeach
</div>
</div>
#endforeach

You need to make some changes in your code, please find below step to change code
Step 1: Please create relations with your foreign key in your model file like
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treeview extends Model
{
protected $table = 'treeview';
public $fillable = ['title','parent_id'];
/**
* Get the index name for the model.
*
* #return string
*/
public function childs() {
return $this->hasMany('App\Treeview','parent_id','id') ;
}
}
Step 2: Add below function in your controller and make some changes according to your table and model object
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function manageCategory()
{
$categories = Treeview::where('parent_id', '=', 0)->get();
return view('treeview/categoryTreeview',compact('categories')); // set the path of you templates file.
}
Step 3 : Add below code in your categoryTreeview templates file
<h3>Category List</h3>
<ul id="tree1">
#foreach($categories as $category)
<li>
{{ $category->title }}
#if(count($category->childs))
#include('treeview/manageChild',['childs' => $category->childs]) // Please create another templates file
#endif
</li>
#endforeach
</ul>
Step 4: Please Create another templates file manageChild add below code.
<ul>
#foreach($childs as $child)
<li>
{{ $child->title }}
#if(count($child->childs))
#include('treeview/manageChild',['childs' => $child->childs])
#endif
</li>
#endforeach
</ul>
Output displayed Like:
Category List
Cat_1
Cat_1_par_1
Cat_1_par_2
Cat_1_par_3
Cat_2
Cat_2_par_1
Cat_2_par_2
Cat_2_par_3
Cat_3
More information I found one link: https://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html

Related

How do I do pagination on my code in Laravel?

So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php
BusinessListController.php
`<?php
namespace App\Http\Controllers;
use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;
class BusinessListController extends Controller
{
public function index(Request $request)
{
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->get();d
return view('pages.business-list', [
'businesses' => $businesses,
'locations' => Location::all(),
'categories' => Category::all()
]);
}
}`
And then here is the code for my view blade front-end
Business-List.blade.php
<div class="row business-list-row mx-auto">
#foreach ($businesses as $business)
<div class="col-md-4">
<div class="card shadow border-light mb-3">
<img
src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
class="card-img-top" alt="...">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title h6" style="font-weight: bold;">
{{Str::limit($business->name, 20, $end='...')}}
</h4>
<div class="">
<p class="card-text">
{{ $business->location?->name }}
</p>
<p class="card-text" style="color: #32a852;">
{{ $business->category?->name}}
</p>
</div>
</div>
<div class="align-self-center">
<a href="{{ route('store', $business->id) }}" class="btn btn-info stretched-link">
Visit
</a>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
So you need to do three things.
In Controller:
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->paginate(15);
put the number of items you need on a single page. here I put 15.
Put this under the </div> of your list.
{{ $business->links() }}
Put this inside the App\Providers\AppServiceProvider boot method.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrapFive(); // or
Paginator::useBootstrapFour();
}
This depends upon which version of Bootstrap you are using.
Still confused? Checkout Laravel Pagination Documentation
Just remove ->get();d and add paginate
example
ModelName()->paginate();

Laravel Livewire component not re-rendering

I have a notifications component that I'm trying to emit an event to and get it to refresh and show new notifications to the user. I've verified that I'm receiving the event, and the refreshNotifications method is being called, but no matter what I try I can't seem to get the actual component to update. It's getting super frustrating at this point.
Here's my component:
namespace App\Http\Livewire\Components;
use Livewire\Component;
use Illuminate\Support\Facades\Auth;
class Notifications extends Component
{
public $notifications;
public $notificationCount = 0;
protected $listeners = ['refreshNotifications'];
public function mount()
{
$this->user = Auth::user();
}
public function render()
{
$this->notifications = $this->user->fresh()->unreadNotifications;
$this->notificationCount = $this->user->fresh()->unreadNotifications->count();
return view('livewire.components.notifications');
}
public function clearNotifications()
{
$this->user->unreadNotifications()->update(['read_at' => now()]);
}
public function refreshNotifications()
{
//can verify here that the event successfully emitted to this component. No refresh happens though.
$this->render();
}
}
And here's my blade view:
<li class="nav-item dropdown" x-data="{ open: false }">
<a #click="open = !open" #click.outside="open = false" class="nav-link dropdown-toggle show" id="notificationDropdown" role="button">
<i class="mdi mdi-bell-outline h3"></i>
#if ($notificationCount)
<div class="indicator">
<div class="circle"></div>
</div>
#endif
</a>
<div x-show="open" class="dropdown-menu p-0 show" data-bs-popper="none">
<div class="px-3 py-2 d-flex align-items-center justify-content-between border-bottom">
<p>{{ $notificationCount }} New Notifications</p>
#if ($notificationCount)
<a role="button" wire:click="clearNotifications" class="text-muted"><i class="mdi mdi-notification-clear-all"></i>Clear all</a>
#endif
</div>
#foreach ($notifications as $notification)
<div class="px-3 py-2" wire:key="notification-{{ $notification->id }}">
<a role="button" class="d-flex align-items-center py-2">
<div class="flex-grow-1 me-2">
<h5>{{ $notification->data['title'] ?? 'Test Title' }}</h5>
<p class="tx-14 text-muted"><em>{{ $notification->data['body'] ?? 'Test body' }}</em></p>
<p class="tx-12 text-muted">{{ Helper::timeElapsedForHumans($notification->created_at) }}</p>
</div>
</a>
</div>
#endforeach
</div>
</li>
Not sure if it's helpful, but the xhr response is this: {"effects":{"html":null,"dirty":[]},"serverMemo":{"checksum":"c980388fe95899e9055bfc363bd57c5d80916fe581b5b7983109cdba20db2f33"}}
Other questions on SO are related to not being in a single root element. That's not my issue. I have no idea why this isn't working. I feel like I've tried everything.
You need to define what event is called in the listener, for example:
protected $listeners = ['refreshNotifications' => '$refresh'];
See here:
https://laravel-livewire.com/docs/2.x/actions#magic-actions

Laravel Livewire Select Search Dropdown not working

I have a form in the Laravel Livewire project and try to search and select users from the database. When I search User then search results in users, but nothing to select. Also, no error showing just page refresh. my code is below:
I'm trying this
livewire component view
<div class="col-md-4">
<div class="form-group row">
<label class="control-label col-md-2 col-sm-2">Select User</label>
<div class="col-md-10 col-sm-10 ">
<div>
<select wire:model="user_id" class="form-control" name="user_id" required>
<option></option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->username}} : {{ $user->fullname }}</option>
#endforeach
</select>
</div>
<div style="position:relative">
<input wire:model.debounce.500ms="inputsearchuser" class="form-control relative" type="text"
placeholder="search..."/>
</div>
<div style="position:absolute; z-index:100">
#if(strlen($inputsearchuser)>2)
#if(count($searchusers)>0)
<ul class="list-group">
#foreach($searchusers as $searchuser)
<li class="list-group-item list-group-item-action">
<spanwire:click
="selectuser({{$searchuser->id}},{{$searchuser->terms}})">{{$searchuser->username}}
: {{$searchuser->fullname}}</span>
</li>
</ul>
#endforeach
#else
<li class="list-group-item">User Not Found...</li>
#endif
#endif
</div>
</div>
</div>
</div>
livewire component
class MyLiveWireComponent extends Component
{
public $j = 1;
public $users = [];
public $inputsearchuser = '';
public $user_id;
public function selectuser($user_id,$terms)
{
$this->user_id = $user_id;
$this->terms = $terms;
$this->inputsearchuser = '';
}
public function render()
{
$searchusers = [];
if (strlen($this->inputsearchuser) >= 2) {
$searchusers = User::Where('username', 'LIKE', '%' . $this->inputsearchuser . '%')
->get();
}
return view('livewire.admin.orders.add-order', ['searchusers' => $searchusers])->layout('components.layouts.admin');
}
}
Change this:
<spanwire:click="selectuser({{$searchuser->id}},{{$searchuser->terms}})">
{{$searchuser->username}} : {{$searchuser->fullname}}
</span>
To:
<span wire:click.prevent="selectuser({{$searchuser->id}},{{$searchuser->terms}})">
{{$searchuser->username}} : {{$searchuser->fullname}}
</span>
Your component should have a single-level root element only. Also, add wire:key in your element inside of the loop.
<ul class="list-group">
#foreach($searchusers as $key => $searchuser)
<li wire:key="{{'users'.$key}}" class="list-group-item list-group-item-action">
<span wire:click="selectuser({{$searchuser->id}},
{{$searchuser->terms}})">{{$searchuser->username}}
: {{$searchuser->fullname}}</span>
</li>
#endforeach
</ul>
Did you change it to wire:click.prevent as suggested above? Otherwise, it does not prevent the default action from occurring, and can cause that exact behavior.

LaravelShoppingcart package, rowId property not working

Hi I'm using the LaravelShoppingcart package(https://github.com/bumbummen99/LaravelShoppingcart) and When I output {{ $item->rowId }} on the blade template I get the rowId on the browser when the blade is rendered but when I pass it in the qtyIncreased({{ $item->rowId}}) method and try to dump and die it in the livewire component it doesn't work. Mark you when I pass the $item->id and dump and die it, it works. The only error I'm getting is on the console and is:
Error handling response: TypeError: Cannot destructure property 'yt' of 'undefined' as it is undefined.
at chrome-extension://cmedhionkhpnakcndndgjdbohmhepckk/scripts/contentscript.js:535:6
Any assistance is highly appreciated.
Live wire blade component
<div class="cart-overlay {{ $active ? 'transparent' : '' }}">
<div class="cart {{ $active ? 'showCart' : '' }}">
<span class="close-cart" wire:click="$emit('closeCart')">
<i class="fas fa-window-close"></i>
</span>
<h2>Cart</h2>
<div class="cart-content">
#foreach ($cartContent as $item)
<div class="cart-item">
<img src="{{ $item->options->image }}" alt="product" />
<div>
<h4>{{ $item->name }}</h4>
<h5>${{ $item->price }}.99</h5>
<span class="remove-item">remove</span>
</div>
<div>
<i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item-
>rowId }})"></i>
<p class="item-amount">{{ $item->qty }}</p>
<i class="fas fa-chevron-down"></i>
</div>
</div>
#endforeach
</div>
<div class="cart-footer">
<h3>your total : $ <span class="cart-total">{{ $cartTotal }}</span></h3>
<button class="clear-cart banner-btn">clear cart</button>
</div>
</div>
</div>
Livewire Class component
<?php
namespace App\Http\Livewire;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class CartOverlay extends Component
{
public $active = false;
public function render()
{
$cartContent = Cart::content();
$cartTotal = Cart::total();
return view('livewire.cart-overlay', compact(['cartContent', 'cartTotal']));
}
public $listeners = [
'showCart',
'closeCart'
];
public function qtyIncreased($rowId)
{
dd($rowId);
}
public function showCart()
{
$this->active = true;
}
public function closeCart()
{
$this->active = false;
}
}
I managed to fixed it, the parameter in the function should be passed in as a string. So, I added quotes around the parameter. Changed it from <i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item->rowId }})"></i>
TO
<i class="fas fa-chevron-up" wire:click="qtyIncreased('{{ $item->rowId }}')"></i>

Laravel search from multi select dropdown

I am trying to create a search filter using dropdown selections. The code only shows results when all dropdowns (make and model) are selected. But it should work even single dropdown (either make or model)is selected. At the moment, if I select a single dropdown, the result page shows "No Matching Data found". this is what I have done -
Controller
class CarFrontController extends Controller
{
public function index_classiccars(Request $request)
{
$filterMakes = DB::table('cars')->select('make')->distinct()->get()->pluck('make');
$filterModels = DB::table('cars')->select('model')->distinct()->get()->pluck('model');
$classiccar = Car::query();
if ($request->has('make')) {
$classiccar->where('make', $request->make);
}
if ($request->has('model')) {
$classiccar->where('model', $request->model);
}
return view(
'layouts.frontend.cars.classiccars_index',
[
'filterMakes' => $filterMakes, 'filterModels' => $filterModels,
'classiccars' => $classiccar->get()
]
);
}
public function store(Request $request)
{
return $request->all();
return view('layouts.frontend.cars.classiccars_index');
}
}
data blade
#forelse ($classiccars as $car)
<div class="row">
<div class="col-lg-5 col-md-5 col-pad">
<div class="car-thumbnail">
<a href="car-details.html" class="car-img">
<div class="price-box">
<span>£{{ $car->price }}</span>
</div>
#php $i=1; #endphp
#foreach ($car->join_caralbum as $image)
#if ($i>0)
<img class="d-block w-100" src="{{asset($image->image_location)}}" alt="car" height="225px" >
#endif
#php $i--; #endphp
#endforeach
</a>
<div class="carbox-overlap-wrapper">
<div class="overlap-box">
<div class="overlap-btns-area">
<a class="overlap-btn" href="{{ url('car_show/'.$car->id) }}">
<i class="fa fa-eye-slash"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-7 col-md-7 col-pad align-self-center">
<div class="detail">
<h3 class="title">
{{ $car->make }}
</h3>
<ul class="custom-list">
<li>
{{ $car->model }} /
</li>
......
</ul>
<ul class="facilities-list clearfix">
.....
.....
....
</ul>
</div>
</div>
</div>
</div>
#empty
No matching data found
#endforelse
filter blade
{!! Form::open(['action'=>'CarFrontController#index_classiccars','method'=>'GET']) !!}
<select class="form-control" name="make" id="make">
<option> Make(All)</option>
#foreach ($filterMakes as $make)
<option value="{{ $make }}">{{ $make }}</option>
#endforeach
</select><br>
<select class="form-control" name="model" id="model">
<option> Model(All)</option>
#foreach ($filterModels as $model)
<option value="{{ $model }}">{{ $model }}</option>
#endforeach
</select><br>
{{ Form::submit('Update Search',['class'=>'btn btn-primary']) }}
Your query results empty because even if you don't select any of your options, you actually do. If options don't have a value, the value is the text you give it to the option.
In your case, if you select just a model, actually you are selected 'Make(All)' option too. You can see this simply by putting dd($request->all()) on your index_classiccars method.
For checking selects, you can give empty value to first(default) option and control it with request()->filled('input').

Resources