Highlight row if has duplicate value - laravel

Hello I have these columns 'name', 'email', 'username' and 'ip' how would I check or highlight table row in blade if records has same ip?
This is my controller
$promo = Promo::with(['participants' => function ($q) {
$q->orderBy('winner', 'desc')->orderBy('created_at', 'desc');
}])->find($id);
And this is my blade
#if($promos->participants->count() > 0)
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach
#endforeach

Another way is to get duplicates by taking advantage of Larvel's collection and have them passed in view.
$ips = $promos->participants->pluck('ip');
$duplicateIps = $ips->duplicates()->unique()->all();
So in your blade you would just need to check
#if($promos->participants->isNotEmpty())
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr #class(['duplicate-row' => in_array($participant->ip, $duplicateIps)]>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach

Do like this
$ipList will store IPs when foreach runs and keeps checking whether they exist. If yes, add class duplicate-row to the <tr>
$ipList = [];
#foreach($promos->participants as $participant)
<tr class="align-middle #if(in_array($participant->ip, $ipList)) duplicate-row #endif">
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
#if(!in_array($participant->ip, $ipList))
<?php array_push($ipList, $participant->ip); ?>
#endif
#endforeach
In CSS you can add
.duplicate-row {
background-color: red;
}

Related

How to sum table row data in laravel 8

I want to sum up the data in my table. Please tell, How can I do it. I have tried the below method but it is not working.
This is my controller code
public function CreateRentCertificateReport(Request $request)
{
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
This is my view code
<tbody>
#foreach($reports as $data)
<tr>
<td>{{$data->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$data->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
#foreach($reports as $data)
<td>{{$data['column_one']->countBy()}}</td>
#endforeach
</tr>
</tbody>
You can use pluck() and sum() :
<tbody>
#foreach($reports as $data)
<tr>
<td>{{ $data->upazila->upazila_name }}</td>
<td class="numeric_bangla">{{ $data->column_one }}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ $reports->pluck('column_one')->sum() }}</td>
</tr>
</tbody>
Maybe by doing something like this:
#php
$total = 0;
foreach($reports as $report) {
$total += $report['column_one']
}
#endphp
<tbody>
#foreach($reports as $report)
<tr>
<td>{{$report->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$report->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{$total}}</td>
</tr>
</tbody>
Instead of calling ->get() on an Eloquent query, you can call ->sum(“column_name”) to get a number which is the sum of that column’s values

How to display this unserialize data to blade in Laravel

public function getcustomerorder($id)
{
$orderss = Orders::find($id);
$orders = $orderss->all();
$orders->transform(function($order){
$order->cart = unserialize($order->cart);
return $order;
});
return $orders;
}
The output of that from the controller is below:
[{"id":3,"cart":{"1":{"itemcode":"20062872","itemname":"AXE BS TWIST 50ML","quantity":4,"price":125},"2":{"itemcode":"20062881","itemname":"DOVE RO ORIGINAL 40ML","quantity":5,"price":50}},"firstname":"Joseph Vincent","lastname":"Limbaroc","phonenumber":"09197963942","email":"joseph.coquilla#outlook.com","street":"094 B Brgy. 40-D Bolton Ext. St.","street2":null,"city":"Davao City","province":"Davao Del Sur","zip":"8000","status":"new","created_at":"2020-04-20T05:15:09.000000Z","updated_at":"2020-04-20T05:15:09.000000Z"}]
How do I display the array of cart into blade using
#foreach($orders as $item)
<tr>
<td>{{ }}</td>
</tr>
#endforeach
Hi Sarvil you could change the structure of your JSON a little. You can make "cart" to be an array not an object but I hope this syntax will help you:
#foreach($orders as $item)
<tr>
<td>{{$item->id}}</td>
</tr>
#foreach($item->cart as $item_cart)
<tr>
<td>{{$item_cart->itemcode}}</td>
</tr>
<tr>
<td>{{$item_cart->itemname}}</td>
</tr>
#endforeach
<tr>
<td>{{$item->firstname}}</td>
</tr>
#endforeach

How to fix Empty data give error Get Property

I have a Function to showing a single data . but if this data empty its having error. I using first because just want to
Trying to get property of non-object
its my Controller
public function PDF_profile(Request $request,$id){
$users = User::findOrFail($id);
$pns = Data_pns::where('user_id',$id)->first();
$pendidikan = Data_riwayat_pendidikan::where('user_id',$id)->latest()->first();;
$r_kgb = DB::table('data_riwayat_kgb')->latest()->first();
$pdf = PDF::loadView('admin.pdf_profile',
['users' => $users,
'pendidikan'=>$pendidikan,
'r_kgb'=>$r_kgb,
'pns' => $pns,
]);
// dd($pns);
return $pdf->stream('Profile.pdf')->header('Content-Type','application/pdf');
}
my View
<table align="left" border="" >
<thead>
<tr>
<th> <b>Riwayat Pendidikan </b></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td> <b>Tingkat Pendidikan </b></td>
<td>:
{{ $pendidikan->jenjang}}
</td>
</tr>
<tr>
<td> <b> Tempat Pendidikan</b></td>
<td>:{{$pendidikan->nama_tempat}}</td>
</tr>
<tr>
<td><b>Konsentrasi/Jurusan</b></td>
<td>:{{ $pendidikan->jurusan}}</td>
</tr>
<tr>
<td><b>Tahun Lulus</b></td>
<td>:{{$pendidikan->lulus_tahun}}</td>
</tr>
</tbody>
</table>
its just part on my view .
and this line is mentioned error .
<td>:
<?php echo e($pendidikan->jenjang); ?>
</td>
but if this data is inputed or not empty its not having error . how to solved it ?
You can first check if the object is not null as first() returns null hence the error, so do this instead:
<td>:
#if($pendidikan)
{{$pendidikan->nama_tempat}}
#endif
</td>
You can also replace this: ->latest()->first() with just calling last().
You can use the optional() helper method
{{ optional($pendidikan)->nama_tempat}}
or short form #
{{ #$pendidikan->nama_tempat}}

Foreach loop has undefined variable in my view

The error is on the view which says that
my $audio variable is not defined.
I want to show everything from the database using a foreach loop.
I tried base on paginate on the laravel documentation https://laravel.com/docs/5.7/pagination#basic-usage
My controller is below:
use Illuminate\Http\Request;
use App\Audio_lectures;
use Illuminate\Support\Facades\DB;
public function index(){
$audio = DB::table('audio_lectures')->latest()->paginate();
return view('welcome', ['audio_lectures' => $audio]);
}
The welcome view is below:
<div class="container">
<div class="row">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Subject</th>
<th scope="col">Date Added</th>
<th scope="col">Download</th>
</tr>
</thead>
<tbody>
#foreach ($audio as $audio_lectures)
<tr>
<th scope="row">{{$audio_lectures->id}}</th>
<td>{{$audio_lectures->subject}}</td>
<td>{{$audio_lectures->created_at}}</td>
<td>Download</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
My route web.php is below:
Route::get('/','Audio_lecturesController#index');
I expect to show all the audio lectures from my database to my view.
try this one
#foreach ($audio_lectures as $audio)
<tr>
<td scope="row">{{$audio->id}}</td>
<td>{{$audio->subject}}</td>
<td>{{$audio->created_at}}</td>
<td>
Download
</td>
</tr>
#endforeach
you are passing from controller audio_lectures array but you
getting as in your view audio
On your view file looping try this
#foreach ($audio_lectures as $value)
<tr>
<td scope="row">{{$value->id}}</td>
<td>{{$value->subject}}</td>
<td>{{$value->created_at}}</td>
<td>Download</td>
</tr>
#endforeach

Use all the elements of the table in Laravel

I have a table in the page "map.blade.index.php" as:
<table id="table">
<thead>
<tr><th>ID</th>
<th>Name</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td id="0">Name 174</td>
<td id="lat0">41.1230199</td>
<td id="lng0">14.73767010000006</td>
</tr>
</tbody>
</table>
and I try to use the table elements in my function Controller called "MapController#saved" , but I think that I can't use the table by her id.
there a way to use all the elements of the table ? Now I must use one item at a time.
You can parse your table with any HTML parser. I'm using this one.
$html->find('td[id=lat0]')->innertext; // Returns 41.1230199
If I understood then what you need is a multi input like:
<form action="map/saved">
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td id="0">Name 174</td>
<td id="lat0">41.1230199</td>
<td id="lng0">14.73767010000006</td>
<input type="hidden" name="entry[0][name]" value="Name 174">
<input type="hidden" name="entry[0][lat]" value="41.1230199">
<input type="hidden" name="entry[0][lng]" value="14.73767010000006">
</tr>
</tbody>
</table>
<button type="submit">
</form>
Your controller method will looks like this:
public function map(Request $request) {
foreach ($request->input('entry') as $i => $entry) {
// here you get an array with:
// array (3) [
// 'name' => 'Name 174'
// 'lat' => '41.1230199'
// 'lng' => '14.73767010000006'
// ]
}
}

Resources