I am using laravel queue to upload a csv file. And, I have used a column completed on the database table.
So, by default the value of this column will be 0 and as soon as the job is completed this column's value will be updated to 1 for that particular row.
Similarly, during uploading phase, this value will be 2 and if some error occured during upload or if some row can't be imported then the value will be 3
On front end, I have a simple button like:
#if($r->completed == 1)
<a class="btn btn-xs btn-primary d-inline-block" href="{{url('/?property='.$r->id)}}">Property</a>
#elseif($r->completed == 2)
<a class="btn btn-xs btn-warning d-inline-block" href="#">Pending</a>
#elseif($r->completed == 3)
<a class="btn btn-xs btn-secondary d-inline-block" href="{{route('property.create',$r->id)}}">Par. Completed</a>
#else
<a class="btn btn-xs btn-secondary d-inline-block" href="{{route('property.create',$r->id)}}">Property</a>
#endif
Yeah, its working totally fine. Here, we need to refresh to check the recent status. However, my requirement is to update the status without refreshing the page.
After some searching, I find that sending ajax request on certain interval might be an option. However, it don't seems ideal, so I don't want to go with that.
Is there any better option available? Currently, I am using jquery/javascript (not any js framework like vue,react).
Related
I am brand new to Laravel and I'm running Version 6.
I want my view to display a button if one of my MySQL tables has rows that meet a specific condition but I'm having trouble figuring out how to code it - and even WHERE to code it - within my Laravel application.
My MySQL table is called diary_entries and various users of the system will contribute zero to n rows to it. Each row of the table contains a user id called client. When a given user goes to the Welcome view, I want the view to determine if that user currently has any rows in the diary_entries table. If he does, I want to display a button that will take him to another page where the entries can be displayed or edited or deleted.
I think I want to construct an #if that counts the number of records for that user; if the count is greater than zero, I want to display the button, otherwise the button is not displayed.
The problem is that I can't figure out how to code this. I've looked at the examples in the Eloquent section of the manual but they aren't particularly clear to me. I found a note near the top that said the count() function expects a Collection as an argument and that the result of an Eloquent statement is always a Collection so I guessed that I just have to execute an Eloquent query, then apply count() to the resulting Collection. But every variation of that idea which I've tried has thrown exceptions.
Here was the guess that seemed most logical to me:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1> Sleep Diary </h1>
</div>
<div>
<h3>{{Auth::user()->name }}</h3>
</div>
<div>
#if (count(App\DiaryEntry::select('*')->where('client', Auth::user()->name) > 0))
<p>
<a class="btn btn-primary"> View / edit existing sleep diary entries </a>
</p>
#endif
</div>
<div>
<p>
<a class="btn btn-primary" href="/diaryEntries"> Create a new sleep diary entry </a>
</div>
</div>
#endsection
This is obviously wrong because it throws an exception so how do I make it right? Does the building of the collection have to move into the Controller? If so, how do I invoke the method and see its result? Or can I do something like I have already done but just adjust the syntax a bit?
EDIT
I've imitated Sehdev's suggestion but I get this error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
Here is my welcome view:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1>Sleep Diary</h1>
</div>
<div>
<h3>{{ Auth::user()->name }}</h3>
</div>
<div>
#if ($count) > 0))
<p>
<a class="btn btn-primary">View/edit existing sleep diary entries</a>
</p>
#endif
</div>
<div>
<p><a class="btn btn-primary" href="/diaryEntries">Create a new sleep diary entry</a>
</div>
</div>
#endsection
And this is the relevant function from DiaryEntryController:
public function countEntriesOneUser()
{
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count();
view("welcome", compact("count"));
}
Should the compact function be returning $count instead of count? I can't find the compact function in the manual with the search function so I'm not clear what it does or what the proper syntax is. I just tried changing the last line of the function to
view("welcome", $count);
but that produced the same error.
Try this,
#php
$count=\App\DiaryEntry::where('client', Auth::user()->name)->count();
#endphp
#if($count>1)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
Using App\DiaryEntry::select('*')->where('client', Auth::user()->name) directly on your blade template is a bad practise.
You can execute your question in your controllers method and then pass the result on your view file
Your function
function test(){
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count(); // get the total no of records using count
view("index", compact("count")) // pass your count variable here
}
then you can directly use $count in your #if condition
Your blade template
<div>
#if ($count > 0)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
</div>
I am new to Thymeleaf. Recently I stumbled in the following situation. Here is a piece of my Thymeleaf html page:
<!-- an delete button link -->
<a th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ?') )) return false" >
Delete
</a>
This code works fine as intended. However I want to add employee name as part of the confirmation. Here is the code:
<!-- an delete button link -->
<a th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ' + '\'+${tempEmployee.firstName}+\'' +'?' ) )) return false" >
Delete
</a>
Unfortunately the result is:
Are you sure you want to delete this employee
'+${tempEmployee.firstName}+'.
Looks like Thymeleaf does not recognize ${tempEmployee.firstName}. It has no problem with it in th:href tag but does not like it in th:onclick.
I would appreciate if somebody can turn me into the right direction.
Not sure exactly what the problem is (though it may be related to onclick vs th:onclick. Regardless, I think a format more like this will work (with some added benefits like no JavaScript injection).
<!-- an delete button link -->
<a
th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:data-confirm-delete="|Are you sure you want to delete this employee ${tempEmployee.firstName}?|"
onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false"
>
Delete
</a>
(Notice I'm using onlick and not th:onclick.
Instead of this line in above code ---onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false">
You can write as:
onclick="return confirm(this.getAttribute('data-confirm-delete'))"
I have some html code in my controller for datatable actions (View , Edit & Delete)
So im busy working on permissions on a blade level. but now i'm struggling to render the same permissions to the HTML code that's in my controller.
I'm using this syntax on my blades
#if(Auth::user()->can('Add New Client'))
<li>Add New Client</li>
#endif
And in my controller i have this code with renders action icons on my datatables
$nestedData['action'] = " <a href='{$show}' title='View more' class='btn btn-xs btn-primary'><i class='fa fa-eye'></i> View</a>
<a href='{$edit}' title='Edit details' class='btn btn-xs btn-success'><i class='fa fa-pencil'></i> Edit</a>";
So just like i did on the blade level i also want to have a permission on the datatable buttons (show & edit) to say
#if(Auth::user()->can('Edit Client'))
<a href='{$edit}' title='Edit details' class='btn btn-xs btn-success'><i class='fa fa-pencil'></i> Edit</a>"
#endif
But when i put this code on the HTML in controller the code gets displayed on the form. I'm pretty new to laravel so any help will be highly appreciated.
#if(Auth::user()->can('Edit Client'))
<a href='{$edit}' title='Edit details' class='btn btn-xs btn-success'><i class='fa fa- pencil'></i> Edit</a>"#endif
I am not sure why you are using this code in conttroller. please use a template and add this code on it.
in the controller you don't need to use #
I'm putting a button on page 1 to navigate to page 2. Page 1 and 2 are on different controllers.
This is the code of my button. The pid is the ID of the project. p.id gives the ID of the project. But I don't know what the problem here is.
<td><a th:href="#{/projects/project/{pid}/clusters(pid=${p.id})" class="btn btn-info" role="button">Clusters</a></td>
The exception:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "#{/projects/project/{pid}/clusters(pid=${p.id})" (projects:53)
You've missed one curly bracket at the end of expression
<a th:href="#{/projects/project/{pid}/clusters(pid=${p.id})}" class="btn btn-info" role="button">Clusters</a>
I have a button in a directive that I only want to appear on the last object in the array. The ng-show evaluates an expression on the controller.
<button class="btn btn-danger button-xs tsid-btn-sch-pad
glyphicon glyphicon-remove"
type="submit" ng-click=""
ng-show="{{$index == sc.schedule.length - 1}}"></button>
The expression is being correctly evaluated in the browser, but the button is displaying anyway.
So the difference between the highlighted row and the one above where the delete button is not displaying is that ng-hide was added to the class attribute of the row above and it has not been added to the row that is displaying the delete button incorrectly. But I don't know why that update isn't taking place since the ng-show expression is being updated.
Try using the $last variable
<button class="btn btn-danger button-xs tsid-btn-sch-pad
glyphicon glyphicon-remove"
type="submit" ng-click=""
ng-show="$last"></button>
http://plnkr.co/edit/Sf6Xw7YjPlfUuqyHIWng?p=preview