Filtering in LARAVEL with checkboxes - php

I have a database filled with articles to create a baby webshop.
At this moment, all articles are shown on my webpage but I have a sidebar with a filter in it that gives the customer the option to choose between different categories, shops and to order the price by ascending or descending price.
How can I say to my foreach function to only show the articles that are called by the filter?
I can't use any external libraries like Jquery or Ajax to do this because school won't let us to..
Here is my code for my page:
<body class="relative min-h-screen">
#include('partials.header')
<main class="m-auto flex flex-row w-3/5">
<div class="p-2 w-3/12 bg-red-100 mt-4 h-fit flex flex-col">
<h3 class=" p-2 m-auto text-2xl w-10/12 border-b border-black ">Verfijnen</h3>
<ul class="p-2 text-xl ">
<ul class="p-2">Categorieën
<div>
<form action="{{route('filter')}}" method="GET">
#foreach ($categories as $category)
<li class="text-sm">
<input type="checkbox" id="{{$category->id}}" name="{{$category->id}}" value="{{ $category->title }}" onChange="this.form.submit()" {{ request()->filled($category->id) ? 'checked' :''}}>
<label for="{{$category->id}}">{{ $category->title }}</label>
</li>
#endforeach
</form>
</div>
</ul>
<ul class="p-2">Website
<div>
#foreach ($websites as $website)
<li class="text-sm">
<input type="checkbox" id="{{$website->id}}" name="{{$website->id}}" value="{{ $website->title }}">
<label for="{{$website->id}}">{{ $website->title }}</label>
</li>
#endforeach
</div>
</ul>
<ul class="p-2">Prijs
<div>
<li class="text-sm">
<input type="checkbox" id="LnH" name="LnH">
<label for="LnH">Laag naar Hoog</label>
</li>
<li class="text-sm">
<input type="checkbox" id="HnL" name="HnL">
<label for="HnL">Hoog naar Laag</label>
</li>
</div>
</ul>
</ul>
</div>
<div class="w-full p-8">
<h3 class="text-center p-4">Overzicht</h3>
<div class="flex flex-row flex-wrap justify-between gap-y-20 gap-x-2">
#foreach ($articles as $article)
<div class=" w-3/12 h-auto flex flex-col justify-between">
<div class="h-2/3 flex justify-center items-center">
<img src="{{$article->image}}" alt="" class="">
</div>
<div class="h-fit mt-2 ">
<h4 class="text-md text-center"><strong>{{ $article->title }}</strong></h4>
</div>
<div class="">
<p class="mt-4">{{$article->prijs}}</p>
</div>
</div>
#endforeach
</div>
</div>
</main>
#include('partials.footer')
</body>

Related

Update loop records via Laravel and livewire

Hi, I would like some help on this.
This loop comes from my products table, using pure laravel I load each product in inputs, so if I make any changes and click on an update button it is redirected to a route to update the changed product.
How could I do this same idea using livewire?
How to display the updated data in this loop without having to go to another route.
My blade:
#forelse($categoryProduct->products as $key => $product)
<li>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
<!-- Show the image -->
<template x-if="imageUrl">
<img :src="imageUrl"
class="object-cover border border-gray-200 rounded"
style="width: 100px; height: 100px;" />
</template>
<!-- Show the gray box when image is not available -->
<template x-if="!imageUrl">
<div class="bg-gray-100 border border-gray-200 rounded"
style="width: 140px; height: 140px;"></div>
</template>
<div class="mt-2">
<input wire:model="img" type="file" name="img" id="img"
#change="fileChosen" />
</div>
#error('img')
#include('components.inputs.partials.error')
#enderror
</div>
</x-inputs.group>
</div>
<div class="col-11">
<div class="row">
<div class="col-10">
<input type="text" class="form-control" placeholder="Name"
value="{{ $product->name }}">
</div>
<div class="col-2">
<button
onclick="confirm('Tem certeza que deseja deletar esse produto?') || event.stopImmediatePropagation()"
wire:click.prevent="deleteProduct({{ $product->id }})"
class="btn btn-user btn-danger">
×
</button>
</div>
<div class="mt-3 col-10">
<textarea class="form-control"
placeholder="Description">{{ $product->description }}</textarea>
</div>
<div class="mt-3 col-2">
<input type="number" class="form-control"
placeholder="Price" value="{{ $product->price }}">
</div>
</div>
</div>
</div>
</div>
</li>
#empty
This is the way I'm feeding the loop:
public function render()
{
$this->shop = Shop::with([
'categoryProducts',
'categoryProducts.products',
'districts'
])->where('user_id', Auth::user()->id)->first();
return view('app.menu-controller')
->extends('layouts.app')
->section('content');
}
You can create a Livewire ProductComponent and in blade have the div card once this loop the same elements
https://www.laravel-livewire.com/docs/2.x/making-components
https://www.laravel-livewire.com/docs/2.x/rendering-components
<div>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
//......................
</div>
</div>
Use model binding in nested component for the loop
https://www.laravel-livewire.com/docs/2.x/nesting-components
#forelse($categoryProduct->products as $key => $product)
<li>
#livewire('product-component',['product' => $product], key($user->id))
</li>
and in component
public Product $product;
public function render()
{
return view('livewire.product-component')
->extends('layouts.app')
->section('content');
}
The rest in in Livewire documentation, when you get on there could come here with issues or questions. Greetings

folder to locate the pop up $error message in this code

I need to edit the error that shows if a user enters an amount below minimum but I can't find the file to edit the pop-up error response that shows if the amount I less than the withdrawal limit.
Here is the code.
<div class="row">
<div class="col-md-9">
<div class="card">
<div class="card-header">
<h4 class="card-title">withdrawal -
<small class="category">Cash Out Fund.</small>
</h4>
</div>
<div class="card-content">
<div class="row">
<div class="col-md-2">
<ul class="nav nav-pills nav-pills-icons nav-pills-primary nav-stacked" role="tablist">
<li class="active">
<a href="#withdraw" role="tab" data-toggle="tab">
<i class="material-icons">redeem</i>Withdraw
</a>
</li>
</ul>
</div>
<div class="col-md-10">
<div class="tab-content">
<div class="tab-pane active" id="withdraw">
<div class="alert alert-info">
<span class="text-center">Please Read before you proceed. You need to know processing fee:</span><br> #php $id=0;#endphp #foreach($gateways as $gateway) #php $id++;#endphp
<span>{{$id}}. <b>#if($gateway->name){{$gateway->name}}#endif #if($gateway->local_name){{$gateway->local_name}}#endif</b> we would charge you <b>{{config('app.currency_symbol')}} {{$gateway->fixed}}</b> fixed + <b>{{$gateway->percent}}%</b> to Withdraw.</span> #endforeach
</div>
<form action="{{route('userWithdraw.post')}}" method="post">
{{ csrf_field() }} #if(count($errors) > 0)
<div class="alert alert-danger alert-with-icon" data-notify="container">
<i class="material-icons" data-notify="icon">notifications</i>
<span data-notify="message">
#foreach($errors->all() as $error)
<li><strong> {{$error}} </strong></li>
#endforeach
</span>
</div>
<br> #endif
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group label-floating">
<select class="selectpicker" name="gateway" data-style="btn btn-warning btn-round" title="Select Withdraw Gateway" data-size="7">
#if($gate->status == 1)
<option value="1000">{{$gate->name}}</option>
#endif #foreach($gateways as $gateway)
<option value="{{$gateway->id}}">{{$gateway->name}}</option>
#endforeach
</select>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group label-floating">
<label class="control-label" for="account">Your PayPal</label>
<input id="account" name="account" type="text" class="form-control">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group label-floating">
<label class="control-label" for="amount">Withdraw Amount</label>
<input id="amount" name="amount" type="text" class="form-control">
</div>
</div>
</div>
I need to edit all error response that pops up in the file and any other error response on the entire website.

My Php Script Exception message: count(): Parameter must be an array or an object that implements Countable

"count(): Parameter must be an array or an object that implements
Countable (View:
D:\xampp\htdocs\btrade\core\resources\views\dashboard\user-details.blade.php)"
Looking for I saw that some have this error in their code, but I did not find how can i solve my problem please help me
i think This is the failing case:
#if(count($user))
My php version : PHP 7.2.0
this my error imge
this is my code
#extends('layouts.dashboard')
#section('style')
<link href="{{ asset('assets/admin/css/bootstrap-toggle.min.css') }}" rel="stylesheet">
#endsection
#section('content')
#if(count($user))
<div class="row">
<div class="col-md-12">
<div class="portlet blue box">
<div class="portlet-title">
<div class="caption font-dark">
<strong>User Details</strong>
</div>
<div class="tools"> </div>
</div>
<div class="portlet-body" style="overflow:hidden;">
<div class="col-md-3">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption uppercase bold">
<i class="fa fa-user"></i> PROFILE </div>
</div>
<div class="portlet-body text-center" style="overflow:hidden;">
<img src="#if($user->image == 'user-default.png') {{ asset('assets/images/user-default.png') }} #else {{ asset('assets/images') }}/{{ $user->image }}#endif" class="img-responsive propic" alt="Profile Pic">
<hr><h4 class="bold">User Name : {{ $user->username}}</h4>
<h4 class="bold">Name : {{ $user->name }}</h4>
<h4 class="bold">BALANCE : {{ $user->balance }} {{ $basic->currency }}</h4>
<hr>
#if($user->login_time != null)
<p>
<strong>Last Login : {{ \Carbon\Carbon::parse($user->login_time)->diffForHumans() }}</strong> <br>
</p>
<hr>
#endif
#if($last_login != null)
<p>
<strong>Last Login From</strong> <br> {{ $last_login->user_ip }} - {{ $last_login->location }} <br> Using {{ $last_login->details }} <br>
</p>
#endif
</div>
</div>
</div>
<div class="col-md-9">
<div class="row">
<div class="col-md-12">
<div class="portlet box purple">
<div class="portlet-title">
<div class="caption uppercase bold">
<i class="fa fa-desktop"></i> Details </div>
<div class="tools"> </div>
</div>
<div class="portlet-body">
<div class="row">
<!-- START -->
<a href="{{ route('user-repeat-all',$user->username) }}">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="margin-bottom: 20px;">
<div class="dashboard-stat blue">
<div class="visual">
<i class="fa fa-recycle"></i>
</div>
<div class="details">
<div class="number">
<span data-counter="counterup" data-value="{{ $total_repeat }}">0</span>
</div>
<div class="desc uppercase bold"> REPEAT </div>
</div>
<div class="more">
<div class="desc uppercase bold text-center">
{{ $basic->symbol }}
<span data-counter="counterup" data-value="{{ $total_repeat_amount }}">0</span> REPEAT
</div>
</div>
</div>
</div>
</a>
<!-- END -->
<a href="{{ route('user-deposit-all',$user->username) }}">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="margin-bottom: 20px;">
<div class="dashboard-stat green">
<div class="visual">
<i class="fa fa-cloud-download"></i>
</div>
<div class="details">
<div class="number">
<span data-counter="counterup" data-value="{{ $total_deposit }}">0</span>
</div>
<div class="desc uppercase bold "> DEPOSIT </div>
</div>
<div class="more">
<div class="desc uppercase bold text-center">
{{ $basic->symbol }}
<span data-counter="counterup" data-value="{{ $total_deposit_amount }}">0</span> DEPOSIT
</div>
</div>
</div>
</div>
</a>
<!-- END -->
<a href="{{ route('user-withdraw-all',$user->username) }}">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="margin-bottom: 20px;">
<div class="dashboard-stat red">
<div class="visual">
<i class="fa fa-cloud-upload"></i>
</div>
<div class="details">
<div class="number">
<span data-counter="counterup" data-value="{{ $total_withdraw }}">0</span>
</div>
<div class="desc uppercase bold "> WITHDRAW </div>
</div>
<div class="more">
<div class="desc uppercase bold text-center">
{{ $basic->symbol }}
<span data-counter="counterup" data-value="{{ $total_withdraw_amount }}">0</span> WITHDRAW
</div>
</div>
</div>
</div>
</a>
<!-- END -->
<a href="{{ route('user-login-all',$user->username) }}">
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="margin-bottom: 20px;">
<div class="dashboard-stat yellow">
<div class="visual">
<i class="fa fa-sign-in"></i>
</div>
<div class="details">
<div class="number">
<span data-counter="counterup" data-value="{{ $total_login }}">0</span>
</div>
<div class="desc uppercase bold "> Log In </div>
</div>
<div class="more">
<div class="desc uppercase bold text-center">
VIEW DETAILS
</div>
</div>
</div>
</div>
</a>
<!-- END -->
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="portlet box blue-ebonyclay">
<div class="portlet-title">
<div class="caption uppercase bold">
<i class="fa fa-cogs"></i> Operations </div>
</div>
<div class="portlet-body">
<div class="row">
<div class="col-md-6 uppercase">
<i class="fa fa-money"></i> add / substruct balance
</div>
<div class="col-md-6 uppercase">
<i class="fa fa-envelope-open"></i> Send Email
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="portlet box green">
<div class="portlet-title">
<div class="caption uppercase bold">
<i class="fa fa-cog"></i> Update Profile </div>
</div>
<div class="portlet-body">
<form action="{{ route('user-details-update') }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="user_id" value="{{ $user->id }}">
<div class="row uppercase">
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>Name</strong></label>
<div class="col-md-12">
<input class="form-control input-lg" name="name" value="{{ $user->name }}" type="text">
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>Email</strong></label>
<div class="col-md-12">
<input class="form-control input-lg" name="email" value="{{ $user->email }}" type="text">
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>Phone</strong></label>
<div class="col-md-12">
<input class="form-control input-lg" name="phone" value="{{ $user->phone }}" type="text">
</div>
</div>
</div>
</div><!-- row -->
<br><br>
<div class="row uppercase">
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>STATUS</strong></label>
<div class="col-md-12">
<input data-toggle="toggle" {{ $user->status == 0 ? 'checked' : ''}} data-onstyle="success" data-size="large" data-offstyle="danger" data-on="Active" data-off="Blocked" data-width="100%" type="checkbox" name="status">
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>EMAIL VERIFICATION</strong></label>
<div class="col-md-12">
<input data-toggle="toggle" {{ $user->email_verify == 1 ? 'checked' : ''}} data-onstyle="success" data-size="large" data-offstyle="danger" data-on="Verified" data-off="Not Verified" data-width="100%" type="checkbox" name="email_verify">
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-12"><strong>PHONE VERIFICATION</strong></label>
<div class="col-md-12">
<input data-toggle="toggle" {{ $user->phone_verify == 1 ? 'checked' : ''}} data-onstyle="success" data-size="large" data-offstyle="danger" data-on="Verified" data-off="Not Verified" data-width="100%" type="checkbox" name="phone_verify">
</div>
</div>
</div>
</div><!-- row -->
<br><br>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn blue btn-block btn-lg">UPDATE</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div><!-- col-9 -->
</div>
</div>
</div>
</div><!-- ROW-->
#else
<div class="text-center">
<h3>No User Found</h3>
</div>
#endif
#endsection
#section('scripts')
<script src="{{ asset('assets/admin/js/bootstrap-toggle.min.js') }}"></script>
<script src="{{ asset('assets/admin/js/jquery.waypoints.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('assets/admin/js/jquery.counterup.min.js') }}" type="text/javascript"></script>
#endsection
The $user variable isn't an array. If that variable must not be an array remove the count function from if statement.
In php5, php7.0, php7.1 it works, but in php7.2 does not work.
#if($user)...
for laravel use below code in web.php
if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
// Ignores notices and reports all other kinds... and warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
}
PHP had changed function count() in version 7.2.
In PHP 7.3 will add is_countable();
You can add this function:
function is_countable($var) {
return (is_array($var) || $var instanceof Countable);
}
if (is_countable($user) && count($user)) {
//
}
and remove it in PHP 7.3

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

Laravel multipule relationships count

I have these models:
ForumCategory
Forum
Topic
Reply
In the main forum page, I want to count and display the number of replies that associated to this forum.
All the models have relationships.
This is the view:
#foreach ($categories as $category)
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default striped">
<div class="panel-heading">
<div class="row">
<div class="col-md-11">{{ $category->name }}</div>
<div class="col-md-1 text-right"><a><span class="fa fa-arrow-down"></span></a></div>
</div>
</div>
#foreach ($category->forums as $forum)
<div class="panel-body">
<div class="row">
<div class="col-md-1 text-right"><span class="fa fa-envelope fa-4x"></span></div>
<div class="col-md-7">
<h4>
{{ $forum->name }}
<br>
<small>{{ $forum->description }}</small>
</h4>
</div>
<div class="col-md-1 post-num">
<h5>{{ $forum->topics->count() }}<br>
<small>Topics</small></h5>
<h5>{{ $forum->topics->replies->count() }}<br>
<small>Replies</small></h5>
</div>
<div class="col-md-3">
<img src="" class="img-circle last-comnt-img">
Last comment<br>
By Username<br>
<span class="text-muted">12:31</span>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
#endforeach
This line won't work:
$forum->topics->replies->count()

Categories