How to make button visible once to admin-type user in Laravel? - laravel

I want to make ADD button only visible to admin-type user. My code looks like this:
#foreach($users as $user)
#if(Auth::user()->type=='admin')
<a href="{{ route('User.create', ['id'=>$user->id ]) }}" class="btn btn-default</i> ADD</a>
#endif
#endforeach
But, it returns lot of ADD button according to number of all users because of foreach loop. If I remove foreach loop, it will show error:
Undefined variable: user
How can I solve this problem?

It's because of you are removing the foreach , but using the variable $user again inside the routes .
Please remove the $user->id and instead, use Auth::user()->id .
#if(Auth::user()->type=='admin')
<a href="{{ route('User.create', ['id'=>Auth::user()->id ]) }}" class="btn btn-default</i> ADD</a>
#endif

You do not need to use foreach loop to check authenticated user having type admin
You need to remove passing id into the user.create route
#if(Auth::user()->type == 'admin')
<a href="{{ route('User.create') }}" class="btn btn-default</i> ADD</a>
#endif

Related

Laravel - Element displaying twice while looping inside a condition

This is the example while looping
despite having a condition and relation many too many it seems that the case
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
in the above snippet
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
code is absolutely fine, but there might be the possibility, you are running with duplicate data, you can use this also.
\App\ClientDocument::where('id_client', $clients->id)->distinct('id_client')->get()
and instead of using #if (\App\Document::where('id',$item->id_document)->get())
you can use
#if (\App\Document::where('id',$item->id_document)->firstOrFail())
#if (\App\Document::where('id',$item->id_document)->count())
That's it.
first you shouldn't loading data inside blade files
do it inside controller function
then you should create relationship inside ClientDocument model name document
and in your blade you can do that
$item->document
or whatever you want

Undefined variable: categories in user.blade.php

I was trying to extend my user.blade.php to my views menu.blade.php. Everything works fine with my other views that use the same user.blade.php too. But not with my menu.blade.php, I get an error saying "Undefined variable: categories (View: D:\xampp\htdocs\mieaceh\resources\views\layouts\user.blade.php)" with "Possible typo $categories
Did you mean $errors?"
Here are the codes to my user.blade.php
#foreach($categories as $category)
<a href="{{ route('menu.index', ['category' => $category->slug]) }}">
<div class="card-category" style="width: 10rem; height: 4rem;">
{{ $category->name }}
</div>
</a>
#endforeach
How do I solve it?
If you want to make a piece of view that appears in multiple places, you can use blade components https://laravel.com/docs/8.x/blade#components.
This will help encapsulating this partials behavior and required data.

Laravel WhereIn Doesn't Accept Array Value

I have this on my blade file:
{{ Form::open(['route' => 'my_route_name']) }}
<button type="submit" class="btn btn-sm btn-success">
<i class="fa fa-file-excel-o" aria-hidden="true"></i> Download
</button>
{{ Form::hidden('my_ids', $my_ids) }}
{{ Form::close() }}
Checking on the chrome's developer mode, the value of my hidden textbox named my_ids is:
[1,2,3,4,5,6]
Upon clicking the Download button, it goes on my controller:
$results= Model::whereIn('id', $request->my_ids)->get();
This is where I am getting an error.
DD-ing dd($request->my_ids) on my controller gives me "[1,2,3,4,5,6]".
However, if I just put the values directly on the eloquent query like below, it would work.
$results= Model::whereIn('id', [1,2,3,4,5,6])->get();
Am I missing something here?
Your dd shows that $request->my_ids is a string, therefore you must parse it before you use it as array.
Try
$results= Model::whereIn('id', json_decode($request->my_ids))->get();

Laravel 5.4 display cookie value in laravel blade

How to display a cookie value in laravel 5.4 blade by interpolation or using conditions ?
Somewhat like this:
#if (Cookie::get('user_first_name') !== null)
<a href="javascript:;" id="user_name">
<i class="fa fa-sign-in" aria-hidden="true"></i>{{ Cookie::get('user_first_name') }}
</a>
#else
The way you suggested is correct:
{{ Cookie::get('user_first_name') }}
Given your comment, you probably did not set the cookie correctly. You may try:
Cookie::queue('user_first_name', 'John', 15);
return view('the-view');

Is it possible to delete record without using forms in laravel 5.4

I want to delete a record but I haven't been successful, apparently my code is wrong. Solutions i came across say i have to use a post in my form method and add the method_field helper. This would mean my view having a form in it, i want to avoid this if possible. Is it then possible to do my delete another way. Below is my code
snippet of my view
<div class="backbtn">
<a class="btn btn-savvy-delete" href="/tasks/{{$task->id}}" data-toggle="tooltip" title="Delete"><i class="fa fa-trash-o" aria-hidden="true"> Delete</i></a>
</div>
<div class="panel-body">
<p><strong>Owner:</strong> {{ ucfirst($task->employee->firstname) }} {{" "}} {{ ucfirst($task->employee->lastname) }}</p>
<p><strong>Task:</strong> {{ $task->title }}</p>
<p><strong>Description:</strong> {{ $task->description }}</p>
</div>
TaskController
public function destroy($id)
{
Task::destroy($id);
Session::flash('status', "Task was successfully deleted.");
return redirect('/tasks');
}
web.php
Route::delete('/tasks/{id}', 'TaskController#delete');
Im not sure what error you are getting, but i can point out a few things. For one use Route::get instead of ::delete, you are calling it via a link not a form method.
Secondly to delete follow what the laravel doc says here eg.
$task = App\Task::find(1);
$task->delete();

Resources