Laravel - How to Display Submit Edit and Delete based on is_approved - laravel

I have this code in my Laravel-5.8
public function index()
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
$goals = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->get();
return view('appraisal.appraisal_goals.index')->with('goals', $goals);
}
view
<td>
#if ($goal->is_approved == 3)
<span class="badge bg-success" >Approved</span>
#elseif ($goal->is_approved == 2)
<span class="badge bg-danger">Not Approved</span>
#elseif ($goal->is_approved == 1)
<span class="badge bg-info">Awaiting Approval</span>
#else
<span class="badge bg-black">Draft</span>
#endif
</td>
<td>
#can('appraisal_goal_show')
<a class="btn btn-xs btn-primary" href="{{ route('appraisal.appraisal_goals.show', $goal->id) }}">
{{ trans('global.view') }}
</a>
#endcan
#can('appraisal_goal_edit')
<a class="btn btn-xs btn-info" href="{{ route('appraisal.appraisal_goals.edit', ['id'=>$goal->id]) }}">
{{ trans('global.edit') }}
</a>
#endcan
#can('appraisal_goal_delete')
<button class="btn btn-xs btn-danger" type="submit" onclick="deleteTag({{ $goal->id }})">
Delete
</button>
<form id="delete-form-{{ $goal->id }}" action="{{ route('appraisal.appraisal_goals.destroy',$goal->id) }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
#endcan
</td>
<div class="col-12">
<i class="fas fa-arrow-right"></i> Submit
</div>
I want to apply this conditions.
Edit should only be visible when is_approved = 1 or 2 or 3
Delete should only be visible when is_approved = 0 or 2
Submit button should only be visible when is_approved = 1 or 2
How do I achieve these?
Thanks

You may nest each of your buttons inside an #if and check is_approved value with in_array function:
#if(in_array($goal->is_approved, [1, 2, 3]))
#can('appraisal_goal_edit')
<a class="btn btn-xs btn-info" href="{{ route('appraisal.appraisal_goals.edit', ['id'=>$goal->id]) }}">
{{ trans('global.edit') }}
</a>
#endcan
#endif
You may do the same for delete and submit buttons and just change the 2nd argument of in_array.

Related

How to get id of slider for get slider_group?

I have two tables sliders and slider_group.
In my controller in create function in laravel, I want the get the id that shows in browser show when I browse it shows like this.
http://localhost:8000/admin/sliders/create/21
blade
<a href="{{ route('admin::sliders.create', ['groupId', $sliderGroup->id]) }}" class="mr-1">
Now how to get the id of slider_group table to group_id of requisitions.
<tbody>
#foreach($sliderGroups as $sliderGroup)
<tr>
<td class="text-truncate">
<i class="la la-dot-circle-o success font-medium-1 mr-1"></i>
{{ $sliderGroup->id }}
</td>
<td class="text-wrap">
{{ $sliderGroup->title }}
</td>
<td class="text-wrap">
{{ getShamsiDate($sliderGroup->created_at) }}
</td>
<td>
<div class="row">
<a href="{{ route('admin::sliders.create', ['groupId', $sliderGroup->id]) }}" class="mr-1">
<i class="la la-plus text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
<a href="{{ route('admin::slider-groups.edit', $sliderGroup->id) }}" class="mr-1">
<i class="ft-edit text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
<form action="{{ route('admin::slider-groups.destroy', $sliderGroup) }}" method="post" #submit.prevent="confirmDelete">
#method('delete')
#csrf
<button type="submit" class="btn btn-default p-0">
<i class="ft-trash-2 text-grey font-medium-5 font-weight-normal"></i>
</button>
</form>
</div>
</td>
</tr>
#endforeach
</tbody>
I get this in address bar like this
http://localhost:8000/admin/sliders/create?groupId&3
It has an error.
404 | Not Found
Controller
public function index(Request $request)
{
if ($request->search) {
$sliderGroups = SliderGroup::search($request->search)->paginate(30);
} else {
$sliderGroups = SliderGroup::paginate(30);
}
if ($sliderGroups->count() == 0 && $request->search ) {
msg()->warning('it is not found.');
}
return view('slider::admin.groups.index', compact('sliderGroups'));
}
web.php
Route::group([
'prefix' => 'admin',
'as' => 'admin::',
], function() {
Route::resource('sliders', 'Admin\SliderController');
Route::resource('slider-groups', 'Admin\SliderGroupController');
});

How can I reduce executed queries in Laravel?

I have about 6 custom attributes on my model, and in my blade view, I loop through filtered models and use these attributes in some if-else statements, but it takes a lot of time to render, as it executes a lot of queries.
Here is an example of an attribute I have.
/**
* Determine if an invoice is due.
*
* #return bool
*/
public function getInvoiceDueAttribute()
{
if (!$this->invoice_frequency) {
return $this->finished && (!$this->invoiced || $this->outgoing_invoices()->whereNull('invoice_number')->count() > 0);
}
$last_invoice = $this->outgoing_invoices()
->where('invoiceable', true)
->latest()
->first();
return ($last_invoice ? $last_invoice->created_at : $this->created_at)
->startOfMonth()->addMonth($this->invoice_frequency)->isPast();
}
Blade:
#foreach($building->projects as $project)
...
<td class="text-center text-nowrap align-middle">
<a href="{{ route('outgoing-invoices.indexPre', ['project' => $project->id]) }}"
class="btn btn-sm btn-{{ $project->invoice_state_color }} #cannot ('Invoice view') disabled #endcannot">
#if ($project->invoice_ok)
<i class="fa fa-check"></i> Számlázva
#elseif ($project->invoice_in_progress)
<i class="fa fa-rotate-right"></i> Folyamatban
#elseif ($project->invoice_due)
<i class="fa fa-times"></i> Számlázatlan
#else
<i class="fa fa-times"></i> Nem esedékes
#endif
</a>
</td>
<td class="text-center text-nowrap align-middle">
#if ($project->invoice_with_project_id)
Havi riportban
#else
<a href="{{ route('outgoing-invoices.index', ['project' => $project->id]) }}"
class="btn btn-sm btn-{{ $project->payment_state_color }} #cannot ('Invoice view') disabled #endcannot">
#if ($project->payment_ok)
<i class="fa fa-check"></i> Nincs fizetetlen számla
#elseif (is_null($project->payment_ok))
<i class="fa fa-times"></i> Nem esedékes
#else
<i class="fa fa-rotate-right"></i> Folyamatban
#endif
</a>
#endif
</td>
...
#endforeach
To be clear, my goal is to improve performance by reducing queries or any other way.
I also accept best-practice advice for any part of my code. Thank you for your help!

fail to show allert " No data" when data is does not in database

i am using laravel 5.4 ,, i use filter by serial number, i want to when the serial number is not save in database, show the allert "no data" , but when i filter data serial number is not save to database, the page is not show the allert,,
this is my blade..
#extends('layouts.master')
#section('content')
......................................
<div class="panel panel-default">
#if (!$instrumentType->instruments->isEmpty())
<table class="table">
#foreach ($instruments as $instrument)
<tr>
<td>
Serial Number : {{ $instrument->serial_number }}
#if($instrument->customer !== NULL)
<div class="text-muted"><i class="fa fa-hospital-o" aria-hidden="true"></i> {{ $instrument->customer->name }}</div>
#endif
#if($instrument->contractType !== NULL)
<div class="text-muted"><i class="fa fa-compress" aria-hidden="true"></i> {{ $instrument->contractType->name }}</div>
#endif
</td>
<td>
<div class="pull-right">
<a type="button" href="{{ route('instrument.edit', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
class="btn btn-success ">
<i class="fa fa-pencil"></i> Edit
</a>
#if($instrument->customer == NULL || $instrument->contractType == NULL)
<a href="{{ route('instrument.destroy', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
data-method="delete" data-confirm="" class="btn btn-danger">
<i class="fa fa-trash"></i> <span class="hidden-xs">Delete</span>
</a>
#endif
</div>
</td>
</tr>
#endforeach
</table>
<div class="paginator text-center">{{ $instruments->links() }}</div>
#else
<div class="panel-body">
<div class="alert alert-warning text-center">No data.</div>
</div>
#endif
</div>
#endsection
.
you can get count of array with :
count($instrumentType->instruments)
add this code as your statement :
#if(count($instrumentType->instruments))
//
#else
//
#endif

How Jump to with collections of data in laravel

I'm implementing the Jump to... functionality in my project. What I want is:
If the user is in the first item the left button is NOT included.
If the user is in the last item the right button is NOT included.
The left button should have a link same with the link before the current/selected item.
The right button should have a link same with the link after the current/selected item.
Please see my code below:
Controller
$course = Course::with([
'assignments' => $undeleted,
'quizzes' => $undeleted,
'add_links' => $undeleted
])->findOrFail($id);
$course_items = collect($course->assignments);
$course_items = $course_items->merge(collect($course->quizzes));
$course_items = $course_items->merge(collect($course->add_links));
$course_items = $course_items->sortBy('item_number');
View
<div class="jump-to">
<div class="pull-right">
<div class="btn-group" role="group">
#foreach($course->topics as $topic)
#foreach(collect($topic->assignments)->merge(collect($topic->quizzes))->merge(collect($topic->add_links))->sortBy('item_number') as $first_item)
#if($first_item->id != $assignment->id)
<a href="#" class="btn btn-primary">
<i class="fa fa-angle-left"></i>
</a>
#endif
<?php break 2; ?>
#endforeach
#endforeach
<div class="btn-group" role="group">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Jump to <div class="caret"></div>
</button>
<ul class="dropdown-menu" role="menu">
#foreach($course->topics as $topic)
<li class="jdivider">
Topic {{ $topic->topic_number }}
</li>
#foreach(collect($topic->assignments)->merge(collect($topic->quizzes))->merge(collect($topic->add_links))->sortBy('item_number') as $topic_item)
<li>
#if($topic_item->item_type() == 'assignment')
<a title="Assignment: {{ $topic_item->name }}" class="regular-link" href="{{ route('assignment.view', ['course_id' => $course->id, 'id' => $topic_item->id]) }}">
<i class="fa fa-tasks"></i> {{ str_limit($topic_item->name, 15) }}
</a>
#elseif($topic_item->item_type() == 'quiz')
<a title="Quiz: {{ $topic_item->name }}" class="regular-link" href="{{ route('quiz.view', ['course_id' => $course->id, 'id' => $topic_item->id]) }}">
<i class="fa fa-file-text"></i> {{ str_limit($topic_item->name, 15) }}
</a>
#elseif($topic_item->item_type() == 'add_link')
<a href="#">
#if(!empty($topic_item->file_location) && in_array(strtolower(last(explode('.', $topic_item->file_location))), ['pdf', 'docx', 'doc']))
<i class="fa fa-file-pdf-o"></i>
#elseif(empty($topic_item->file_location))
<i class="fa fa-globe"></i>
#else
<i class="fa fa-file-text-o"></i>
#endif
{{ str_limit($topic_item->name, 15) }}
</a>
#endif
</li>
#endforeach
#endforeach
</ul>
</div>
<a href="#" class="btn btn-primary">
<i class="fa fa-angle-right"></i>
</a>
</div>
</div>
</div>
Example
dropdown links:
Assignment1
Assignment2
Quiz1
QUiz2
Reviewer1
If I clicked Assignment2, the left button, has the id or link to Assignment1. If I clicked Reviewer1, the right button disappears and the left button has the id or link to Quiz2 and same with the other links.

bllim / laravel4-datatables-package -> get the id from the query

I'm trying to get the ID from the query when I use edit_column but I get it only for the 'id' column, when I try to get it for the 'operations' column I get the html markup of the 'id' column.
Here is the code:
public function ajaxGetAllPages()
{
$pages = Page::select(array('id','title','updated_at','status'));
return Datatables::of($pages)
->edit_column('id', '<input type="checkbox" class="checkboxes tooltips" value="{{ $id }}" data-placement="left" data-original-title=" אינדקס # {{ $id }} " />')
->edit_column('title','{{ $title }}')
->edit_column('updated_at', '{{ date("d-m-Y | H:i",strtotime($updated_at)) }}')
->edit_column('status', '{{ $status }}')
->edit_column('operations',
'<i class="fa fa-pencil"></i>
<i class="fa fa-trash-o"></i>
<i class="fa fa-times"></i>
<div class="btn-group">
<button class="btn default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">עוד <i class="fa fa-angle-down"></i></button>
<ul class="dropdown-menu pull-right" role="menu">
<li>פעולה</li>
<li>פעולה</li>
</ul>
</div>'
)
->make();
}
Markup result - http://screencast.com/t/nIefrpqc8
Am I doing something wrong or is it a bug?
Thanks,
Chen
According to Bllim docs:
If you use double quotes while giving content of add_column or edit_column, you should escape variables with backslash () else you get error. For example:
edit_column('id',"- {{ \$id }}")
Use backslash before $id.
->edit_column('operations',
'<i class="fa fa-pencil"></i>
<i class="fa fa-trash-o"></i>
<i class="fa fa-times"></i>
<div class="btn-group">
<button class="btn default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">עוד <i class="fa fa-angle-down"></i></button>
<ul class="dropdown-menu pull-right" role="menu">
<li>פעולה</li>
<li>פעולה</li>
</ul>
</div>'
)

Resources