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

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

Related

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!

how to make two If condition in laravel

Two conditions I want to apply for a button that will show the application when applied and if the user has a login then the button will show and ask to login.
Blade
#if (Auth::check())
#if ($tuitionJob->checkApply())
<form action="{{url('/tuition/apply')}}" method="POST">
#csrf
<input type="hidden" value="{{$tuitionJob->id}}" name="tuitionid">
<button type="submit" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
Apply <i class="fa fa-send"></i>
</button>
</form>
#else
<a href="/login" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
<i class="fa fa-send"></i>
Login For Apply
</a>
#else
<a href="tuition/job" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
<i class="fa fa-send"></i>
Applied
</a>
#endif
#endif
Model
public function checkApply(){
return \DB::table('tuition_applies')->where('users_id', auth()->user()->id)
->where('tuition_post_id', $this->id)->exists();
}
Try && Logical Operator
#if(Auth::check() && $tuitionJob->checkApply())
//Code
#else
//Code
#endif
May be helpful for you
#if (Auth::check())
#if ($tuitionJob->checkApply())
<form action="{{url('/tuition/apply')}}" method="POST">
#csrf
<input type="hidden" value="{{$tuitionJob->id}}" name="tuitionid">
<button type="submit" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
Apply <i class="fa fa-send"></i>
</button>
</form>
#else
<a href="/login" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
<i class="fa fa-send"></i>
Login For Apply
</a>
#endif
#else
<a href="tuition/job" class="btn btn-primary login-apply-btn my-2 text-light pull-right">
<i class="fa fa-send"></i>
Applied
</a>
#endif
Try this

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

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.

"Too few arguments to function App\Http\Controllers\Backend\BlogController::edit(), 0 passed and exactly 1 expected"

can anybody help me with this error?
Here my complete route
Route::resource('/blog/post', 'Backend\BlogController');
this is my edit function on BlogController
public function edit($id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact('post'));
}
and this my button
<a href="{{ route('post.edit', $post->id) }}" class="btn btn-xs btn-default">
<i class="fa fa-edit"></i>
</a>
While generating a button, a correct way, according to the documentation is:
<a href="{{ route('post.edit', ['id' => $post->id]) }}" class="btn btn-xs btn-default">
<i class="fa fa-edit"></i>
</a>
Note passing an array of options as a second argument.

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.

Resources