Result will display two times in blade file in laravel 5.7 - laravel

I am creating task in which i want to display notification based on notification_status & order_status .
From controller only three record is obtained but in blade file the loop repeat, two record two times.
As expected output, i want to display only first three record but it repeat again second & third record. How can remove this confusion?
controller:
public function get_notification()
{
$userId=Session::get('userid');
$data = DB::select('Select orders.product_id,subcategory.image,GROUP_CONCAT(subcategory.name_of_subcategory) as subcategory,orders.grand_total,orders.deliver_date,orders.order_status,orders.notification_status,orders.orders_id,orders.payment_status,orders.orders_date from orders inner join product_details on FIND_IN_SET(product_details.product_id,orders.product_id) > 0 inner join subcategory on product_details.sub_id=subcategory.sub_id where orders.user_id=? GROUP BY orders.product_id,orders.deliver_date,orders.order_status,orders.orders_id,orders.notification_status,orders.orders_date,orders.payment_status,orders.orders_subtotal,subcategory.image,orders.grand_total',[$userId]);
return view('notification')->with('data',$data);
}
blade:
#foreach($data as $notification)
<div class="card-body noti-card" id="noti-card">
<div class="row mt-2">
<div class="col-sm-2">
<div>
<span><img src="{{asset('images/subcategory/'.$notification->image)}}" class="img-fluid" height="100" width="100"></span><br>
</div>
</div>
<div class="col-sm-8">
<div>
<span>
<?php
if($notification->notification_status == 1 && $notification->order_status ==1){
echo "<b>Order Placed</b><br>";
echo "Your order for ".$notification->subcategory." with order ID ODI".$notification->orders_id." amounting to Rs.".$notification->grand_total." has been received.";
}
if($notification->notification_status == 2 && $notification->order_status == 2)
{
echo "<b>Order Cancelled</b><br>";
echo "Your order for".$notification->subcategory." with order ID ODI".$notification->orders_id." is successfully cancelled.";
}
if($notification->notification_status == 3 && $notification->order_status == 3)
{
echo "<b>Product Packed</b><br>";
echo "Your package containing ".$notification->subcategory." has been packed by seller and will be shipped soon.";
}
if($notification->notification_status == 4 && $notification->order_status == 4)
{
echo "<b>Product Shipped</b><br>";
echo "Your package containing ".$notification->subcategory." has been shipped by seller and will be delivered soon.";
}
if($notification->notification_status == 5 && $notification->order_status == 5){
echo "<b>Out for Delivery</b><br>";
echo "Your package containing ".$notification->subcategory." from Aarch Ayurved will be delivered today";
}
if($notification->notification_status == 6 && $notification->order_status == 6){
echo "<b>Product Delivered</b><br>";
echo "Your package containing ".$notification->subcategory." has been delivered. Thanks for shopping!";
}
?>
</span>
</div>
<div>
<span class="noti-date"><?php
$timestamp = strtotime("$notification->orders_date");
$formattedDate = date('F d, Y', $timestamp);
echo $formattedDate;
?></span>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
#endsection
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
function loadlink(){
$('#noti-card').load('/notification #noti-card');
console.log('TESTING!!!!');
}
loadlink();
setInterval(function(){
loadlink()
}, 1000);
</script>
output:

The duplication is most likely coming from the query. Can youuse the Laravel helper methods dump() or dd() the results right after running the query to confirm? e.g. dd($data) immediately after the query would allow you to see the results the query returns and verify if they match expectations or not.
I believe the issue might be in the join to product_details. What is the intent of using FIND_IN_SET()?
The query would be much easier to read and understand if it where broken onto multiple lines. It might be easier to troubleshoot if you switch to using query builder, which would look something like this:
$data = DB::table('orders')
->select([
'orders.deliver_date',
'orders.grand_total',
'orders.notification_status',
'orders.order_status',
'orders.orders_date',
'orders.orders_date',
'orders.orders_id',
'orders.payment_status',
'orders.product_id',
'subcategory.image',
DB::raw('GROUP_CONCAT(subcategory.name_of_subcategory) as subcategory'),
])
->join('product_details', 'orders.product_id', '=', 'product_details.product_id')
->join('subcategory', 'product_details.sub_id', '=', 'subcategory.sub_id')
->where('orders.user_id', '=', $userId)
->groupBy(
'orders.product_id',
'orders.deliver_date',
'orders.order_status',
'orders.orders_id',
'orders.notification_status',
'orders.orders_date',
'orders.payment_status',
'orders.orders_subtotal',
'subcategory.image',
'orders.grand_total'
)
->get();

Related

How to use condition in laravel query

Hello I am new to laravel. I am getting an error saying 'Property [pickupdate] does not exist on this collection instance.', after fowarding to 'overdue_pickup' view.
My Controller code
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
if($overdue_pickup->pickupdate < $curr_date){
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}else{
return "No Overdue Pickup";
}
}
My overdue_pickup view
<?php $i = 1; ?>
#foreach($overdue_pickup as $overdue_pickup)
<div class="col-md-offset-1 col-md-10">
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title" style="padding:5px">Order No: <b><?php echo $i;?> {{$overdue_pickup->status}}</b></h3>
</div>
<div class="box-body">
<div class="col-md-offset-1 col-md-10">
<div style="overflow-x:auto;">
<p><i class="fa fa-dashboard"></i> <b>Pickup Time:</b> {{$overdue_pickup->pickuptime}}</p>
<p><i class="fa fa-dashboard"></i> <b>Delivery Time:</b> {{$overdue_pickup->deliverytime}}</p>
</div>
</div>
</div>
</div>
</div>
<?php $i++; ?>
#endforeach
Considering that your record for $id=2 is one,
Change
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->get();
to first()
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->first();
Then use
if($assign_pickup->pickdate > $curr_date){
return view('Ridder.assign',['assigned_pickup' => $assigned_pickup]);
}else{
return "No Overdue Pickup";
}
If not one then use foreach & no need to change get() statement,
foreach($assign_pickup as $assign_pick){
if($assign_pick->pickdate > $curr_date){
// ... your logic
}else{
// ..your logic
}
}
EDIT:
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}
In your view, you can just foreach loop through it,
#foreach($overdue_pickup as $overdue_pick)
#if($overdue_pick->pickdate > $curr_date)
{{-- show your view --}}
#else
<p class="text-warning">No Overdue Pickup</p>
#endif
#endforeach

Laravel checkbox implementation for updating database

I've been trying to make an attendance management system in Laravel.
In the attendance view,when a checkbox is checked ,the controller function is supposed to increment the appropriate database column.
I've been having some issues.Here's my code:
views:
<div class="container">
<!--<div class="row">-->
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Take Attendance</div>
<h4><center>Teacher: {{ auth()->user()->name}}</center></h4>
<div class="form-inline">
#foreach ($second as $sec)
<br>
<div class = "form-group">
{{$sec->Roll }}
&nbsp&nbsp&nbsp&nbsp
{{ $sec->Name}}
</div>
<div class = "form-group">
<!--{{Form::checkbox('agree') }}-->
<input tabindex="1" type="checkbox" value="{{$sec->Roll}}" name="" />
</div>
#endforeach
</div>
<br>
<form action ="report_generate&<?php echo $name ?>" method = "post" enctype = "multipart/form-data" >
<!--<input type = "hidden" name = "_token" value = "{{{ csrf_token() }}}"/>-->
<div class = "form-group">
<input type="submit" value= "Submit">
<center>Report</center>
</div>
</form>
</div>
</div>
</div>
Controller(for submit button):
public function report_generate($tabu)
{
$year = DB::table('sub')->select('Year')-
>where('sub.Subject_Name','=',$tabu)->get();
$sub_id = DB::table('sub')->select('Subject_Id')-
>where('sub.Subject_Name','=',$tabu)->get();
ob_start();
echo $year;
$output=ob_get_contents();
ob_end_clean();
if ( $output=='[{"Year":1}]')
{
$req = "first";
}
elseif ( $output=='[{"Year":2}]')
{
$req = "second";
}
elseif ( $output=='[{"Year":3}]')
{
$req = "third";
}
elseif ( $output=='[{"Year":4}]')
{
$req = "fourth";
}
$final = DB::table($req)->get();
//dd($final);
//$columns = Schema::getColumnListing($req);
//dd($sub_id);
ob_start();
echo $sub_id;
//dd($sub_id);
$va=ob_get_clean();
$va = stripslashes($va);
$txt = rtrim($va,"}]");
$txt = ltrim($txt,"[{Subject_Id:");
$txt = ltrim($txt,"Subject_Id:");
$txt = explode(":",$txt);
$txt = str_replace('"','', $txt);
//dd($txt[1]);
$columns = Schema::getColumnListing($req);
//dd($columns);
//dd($txt);
foreach ($columns as $col)
{
//dd("Y");
if($col == $txt[1])
{
$got=DB::table($req)->select($col)->get();
//dd($got);
foreach($got as $g)
{
//want to increment that cell value
}
}
}
}
Looks like your checkboxs are outside of the form divs which is an issue. You also seem to be skipping over the Laravel features, for example having a Sub Model you can call on, or using the Request facade. Also not sure why you're using ob for those string comparisons (might actually want to look at Switch for that long elseif block) Lastly, you might want to to use a raw DB statement to do the increments within a SQL query
DB::statement("UPDATE " . $table . " set " . $column . " to " . $column . " + 1 WHERE id IN (" implode(', ', $ids) . ')")

Get value of data-href attribute in xpath

Hello friends of the forum.
I want to get the value of the data-href attribute of a tag to
My code is the following:
<div id="myId">
<h3>
Domain 1
</h3>
<h3>
Domain 2
</h3>
<h3>
Domain 3
</h3>
</div>
I tried the following way but I do not get results:
$entries = $xpath->query("//div[#id='myId']/h3");
foreach ($entries as $entry) {
$node = $xpath->query("a/attribute::data-href", $entry);
echo $node->item(0)->value."<br>";
}
How to show the result show:
http://domain1.com
http://domain2.com
http://domain3.com
Thank you very much for your future answers.
Try using below code :
$entries = $xpath->query("//div[#id='myId']/h3");
foreach ($entries as $entry) {
$nodes = $xpath->query("a/attribute::data-href", $entry);
foreach( $nodes as $node ) {
echo $node->nodeValue."</br>";
}
}

Call to a member function tasks() on null on laravel 5

Let me explain with a couple of word my problem.
On my controller i have this line:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
This works for existing users, but when i try to create new ones i receive
Call to a member function tasks() on null
Can i with something like this or what do you suggest ?
if(!is_null($tasks))
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
}
This i my show function on controller
public function show(){
$user = Auth::user();
if(!$user)
return redirect('/')->withErrors(config('constants.NA'));
$countries = Country::all()->lists('name', 'id')->toArray();
$profile = $user->profile;
$student = $profile->student;
// Tasks showed on students profile
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// Classmates
if ($student->group) {
$classmates = $student->group->students()
->where('id', '!=', $student->id)
->get();
}
// Activated books
$books = Auth::user()->books()->orderBy('grade_id', 'asc')->get();
if(!is_null($student->group))
$iTasks = $student->group->tasks->count();
else {
$iTasks = 0;
}
$iTodos = $user->todos->count();
return view('student.profile.show',compact('user','profile', 'countries', 'iTasks', 'iTodos', 'tasks', 'classmates', 'books'));
}
This is my show view, for the tasks
<div class="tab-pane <?php if(isset($tab) && $tab == 'timeline'): ?> active <?php endif; ?>" id="timeline">
#if($tasks->count())
<div class="timeline">
#foreach($tasks as $task)
<!-- TIMELINE ITEM -->
<div class="timeline-item">
<div class="timeline-badge">
<div class="timeline-icon">
<i class="mdi mdi-clipboard-text font-red-intense"></i>
</div>
</div>
<div class="timeline-body">
<div class="timeline-body-arrow"> </div>
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<span class="timeline-body-title font-blue-madison">
{{ $task->professor->profile->user->name }}
</span>
<span class="timeline-body-time font-grey-cascade">ju ca caktuar një detyrë të re.</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-grey-cascade">
{{ $task->pivot->comment }}
</span>
</div>
<hr>
Lenda: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->subject->name }}</span>
<div class="pull-right">
Krijuar më: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->created_at->format(Config::get('klasaime.date_format')) }}</span>
</div>
</div>
</div>
<!-- END TIMELINE ITEM -->
#endforeach
</div>
#else
<div class="alert">
Ju nuk keni asnje detyrë të caktuar!
</div>
#endif
</div>
Looks to me like you haven't added the student to a group yet, or that somehow hasn't been persisted. If you have added the student to a group after creating and saving the student, try this:
$student->load('group')
Before running:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
I'll need to see more of your code to give a more accurate answer. But the error you're getting is related to the ->group, of your student, not your student itself.
Do a simple check to see if the group exists and then carry on with whatever action you need to perform.
// controller
$tasks = null;
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// view
#if($tasks)
{{ $tasks->id }}
#else
No tasks found
#endif
Edit : In your controller add $tasks = null; and $classmates = null; at top. And in your view change #if($tasks->count()) to #if($tasks). I'm not sure where your use the classmates variable, but add a check to see if it's null before you use it.

Form dropdown is giving me index and not the text

Can you help out a new learner of Codeigniter? for some reason my form dropdown is giving me the index as input->post. I just need the text selected.
Model
function get_items(){
$this->db->select('item_name');
$this->db->from('commissary_items');
$query=$this->db->get();
$result=$query->result();
$item_names=array('-SELECT-');
for($i=0;$i<count($result);$i++){
array_push($item_names,$result[$i]->item_name);
}
return $item_names;
}
View
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="item_name" class="control-label">Item</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "item_name"';
echo form_dropdown('item_name',$item_name,set_value('item_name'),$attributes);?>
<span class="text-danger"><?php echo form_error('item_name'); ?></span>
</div>
</div>
</div>
Controller
public function new_inventory(){
$data['item_name']=$this->commissary_model->get_items();
$this->form_validation->set_rules('date_added','Date Added','required');
$this->form_validation->set_rules('item_name','Item Name','callback_combo_check');
$this->form_validation->set_rules('quantity','Quantity','required');
$this->form_validation->set_rules('amount','Amount','required|numeric');
$this->form_validation->set_rules('username','User Name');
if($this->form_validation->run()==FALSE){
// $data="";
$this->load->view('new_inventory_view',$data);
}else{
$data=array(
'date_added'=>#date('Y-m-d',#strtotime($this->input->post('date_added'))),
'item_name'=>$this->input->post('item_name'),
'quantity'=>$this->input->post('quantity'),
'amount'=>$this->input->post('amount'),
'username'=>$this->session->userdata('username')
);
$this->db->insert('add_inventory',$data);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Item added to inventory.</div>');
redirect('commissary/added_to_inventory');
}
}
Instead of the "text value" inside the form dropdown, I get the index 1 or 2 or 3 or 4 or 5 etc.... Thank you.
That is how <select> options work - the value of the select option is returned.
If you want the text you will have to setup $item_names differently and have the index be the same as the text. Easily accomplished with a little restructuring in the model.
function get_items()
{
$this->db->select('item_name');
$this->db->from('commissary_items');
$query = $this->db->get();
$result = $query->result();
$item_names = array();
foreach($result as $item)
{
$item_names[$item->item_name] = $item->item_name;
}
return $item_names;
}

Resources