Render Permissions To HTML In Controller Laravel - laravel

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 #

Related

laravel - I try to delete row out of a table from the database with a href icon

So what I'm trying to do is delete a row from my database table with a icon button (href)
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
Here is my controller
public function destroy($idtask) {
DB::delete('delete from tasks where idtask = ?',[$idtask]);
return redirect('/todo/notstarted');
}
and here is my routing:
Route::get('notstarted/delete/{idtask}','NotstartedController#destroy');
so my button is on page /todo/notstarted , when i click it it goes to /todo/notstarted/delete/1 like I want but it shows the laravel 404|Not Found page. Normally the redirect in my controller should work but for some reason it broke.
here is a picture of my folder structure of my views:
enter image description here
Hopefully someone can help!
the problem is your anchor tag's href
you should use laravel url() helper.
like the code below :
<a href="{{url('notstarted/delete/',$task->idtask)}}" style="color: #8B0000">
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
</a>

Thymeleaf th:onclick event - calling confirm function

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'))"

Refresh view content when there is change in database laravel

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).

Laravel: How to create link buttons on a view dynamically?

I'm making a College Administration website where a professor can log in.
I have a dashboard, where my dynamically generated button should be placed: (right now it just has dummy buttons!)
Generated by this view file, which I will have to modify soon:
<div class="container d-flex flex-column align-items-center justify-content-center">
<h1>IA DASHBOARD</h1>
<br>
<div class="grid2">
SUBCODE 1</button>
SUBCODE 2</button>
SUBCODE 3</button>
</div>
Tables in the Database:
the table iamarks contains the data (student info, and marks) that is to be displayed after /subcode/{subcode} narrows it down to records of just the students that are in the class assigned to current logged-in professor.
classroom_mappers is a table used to map a professor to a classroom with a subject. It makes sure that one classroom only has one professor for a particular subject.
the routes currently in my web.php:
route::get('/ia', 'IAController#show')->middleware('auth');
Route::get('/subcode/{subcode}', 'IAController#showTable')->middleware('auth');
...and these are the methods inside my controller:
//shows buttons to the user:
public function show(){
$subcodes = DB::table('classroom_mappers')
->select('subcode')
->where([['PID','=', auth()->user()->PID]])
->get();
return view('ia',compact('subcodes'));
}
//when user clicks a button, subcode is to be generated and a table is to be shown:
//it works, I tried it by manually typing in subcode value in URL.
public function showTable($subcode){
$sem = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['subcode','=',$subcode]])
->pluck('semester');
$division = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['semester','=',$sem],
['subcode','=',$subcode]])
->pluck('division');
$data = DB::table('iamarks')
->where([['semester','=',$sem],
['division','=',$division],
['subcode','=',$subcode]])
->get();
return view('subcode',compact('data'));
}
My Problem:
To be able to generate the {subcode} in the URL dynamically, I want to create buttons in the dashboard using the data $subcodes. The controller hands over the $subcodes (an array of subject codes which belong to logged in professor) which are to be made into buttons from the show() method.
The buttons should have the name {subcode} and when clicked, should append the same subject code in the URL as {subcode}.
How do I make use of $subcodes and make the buttons dynamically?
How do I make sure the buttons made for one user are not visible to another user?
I managed to find the solution, thanks to Air Petr.
Apparently, you can't nest blade syntax like {{some_stuff {{ more_stuff }} }} and it generates a wrong php code. I modified the solution by Air Petr to:
<div class="grid2">
#foreach ($subcodes as $subcode)
<a href="<?php echo e(url('/subcode/'.$subcode->subcode));?>">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">
<?php
echo e($subcode->subcode);
?>
</button>
</a>
#endforeach
</div>
It generates the buttons perfectly. The buttons for one user are not visible to another, since I'm using PID constraint in a query (['PID','=', auth()->user()->PID]).
Pass the passcodes array to view:
$subcodes = []; // Array retrieved from DB
return view('subcode', compact('subcodes'));
And in subcode.blade.php, loop through each subcode:
<div class="grid2">
#foreach($subcodes as $subcode)
<a href="{{ url('/subcode/' . $subcode->subcode) }}">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">SUBCODE {{ $subcode->subcode }}</button>
</a>
#endforeach
</div>
You can loop your codes to create buttons. Something like this (it's for "blade" template engine):
<div class="grid2">
#foreach ($subcodes as $subcode)
{{ $subcode->subcode }}</button>
#endforeach
</div>
Since you're using PID constrain in a query (['PID','=', auth()->user()->PID]), you'll get buttons for that specific PID. So there's no problem.

Codeigniter redirect design fail

So i have and action button of edit and the fields to be edited on the same page using php. my problem is when i reload the edit button it will come back to the same page and add an additional url data.
function view_branch()
{
$this->load->view('includes/a_header');
$this->profile();
$this->load->view('includes/a_footer');
$this->load->view('admin/content/view_branch');
}
on top is my original method which views my html page view_branch
on the bottom is the button that reloads the page go to controller and finds the edit_branch method
$row[] = '<td class="w3-row" >
<form method="post" class="w3-half"
action="edit_branch/'.$foo->BranchId.'" id="Edit">
<button class="btn btn-default">
<i class="fa fa-edit"></i>
</button>
</form>
</td>';
so this button will go to my controller with the method name edit_branch that holds
public function edit_branch()
{
$this->view_branch();
}
what will happen is that it creates another url that would cause and error
two url edit_branch
http://localhost/GFC/index.php/Admin_Dashboard/edit_branch/edit_branch/CEB1
You could construct the url differently using site_url(). Try this:
$row[] = '<td class="w3-row" >
<form method="post"
class="w3-half"
action="' . site_url('admin_dashboard/edit_branch/'.$foo->BranchId) . '"
id="Edit">
</form>
</td>';
Note: make sure the URL Helper is loaded before using the site_url() function

Resources