Route [report.expAlert] not defined - php

Route report.expAlert error
(View: /home/grocersa/edesimarket.com/resources/views/layout/main.blade.php
(i defined all place but not triggered plz help to find out)
This is my web.php file coding
Route::get('report/product_quantity_alert', 'ReportController#productQuantityAlert')->name('report.qtyAlert');
Route::get('report/product_expiry_alert', 'ReportController#productExpiryAlert')->name('report.expAlert');
Route::get('report/warehouse_stock', 'ReportController#warehouseStock')->name('report.warehouseStock');
This my main.blade.php coding
for sidebar (nav - menu )
#if($product_exp_alert_active)
<li id="expAlert-report-menu">
{{trans('file.Product Expiry Alert')}}
</li>
#endif
for user permission
#if($product_qty_alert_active || $product_exp_alert_active)
for database & roll permission
$product_qty_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-qty-alert'],
['role_id', $role->id] ])->first();
$product_exp_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-exp-alert'],
['role_id', $role->id] ])->first();
for define
#if($alert_product > 0)
<li class="nav-item">
<a rel="nofollow" data-target="#" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-item"><i class="dripicons-bell"></i><span class="badge badge-danger">{{$alert_product}}</span>
</a>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default notifications" user="menu">
<li class="notifications">
{{$alert_product}} product exceeds alert quantity
</li>
<li class="notifications">
{{$alert_product}} product expiry alert
</li>
</ul>
</li>
#endif
This is my reportcontroller.php coding
class ReportController extends Controller
{
public function productQuantityAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-qty-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_quantity')->where('is_active', true)->whereColumn('alert_quantity', '>', 'qty')->get();
return view('report.qty_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function productExpiryAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-exp-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_expiry')->where('is_active', true)->whereColumn('alert_expiry', '>', 'name')->get();
return view('report.exp_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}

Related

In Laravel Pagination how to add first page and last page?

In Laravel 8 pagination how to show less number of pages and also how to add "First Page" and "Last Page"
Now in Laravel when we have more data, it will show the pagination like this:
But I want my pagination to be like the images given below:
Or
To customizing the pagination view, run below command in console
php artisan vendor:publish --tag=laravel-pagination
in /resources/views/vendor/pagination/bootstrap-4.blade.php add bellow code.
<ul class="pagination">
#if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="#lang('pagination.first')">
<span class="page-link" aria-hidden="true">«</span>
</li>
#else
<li class="page-item">
<a class="page-link" href="{{ \Request::url() }}" rel="prev" aria-label="#lang('pagination.first')">«</a>
</li>
#endif
...
#if ($paginator->hasMorePages())
<li class="page-item">
<a class="page-link" href="{{ \Request::url().'?page='.$paginator->lastPage() }}" rel="last" aria-label="#lang('pagination.last')">»</a>
</li>
#else
<li class="page-item disabled" aria-disabled="true" aria-label="#lang('pagination.last')">
<span class="page-link" aria-hidden="true">»</span>
</li>
#endif
</ul>
<?php
class MyPresenter extends Illuminate\Pagination\BootstrapPresenter {
public function render()
{
if ($this->currentPage == 1) {
$content = $this->getPageRange(1, $this->currentPage + 2);
}
else if ($this->currentPage >= $this->lastPage) {
$content = $this->getPageRange($this->currentPage - 2, $this->lastPage);
}
else {
$content = $this->getPageRange($this->currentPage - 1, $this->currentPage + 1);
}
return $this->getFirst().$this->getPrevious('‹').$content.$this->getNext('›').$this->getLast();
}
public function getFirst($text = '«')
{
if ($this->currentPage <= 1)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl(1);
return $this->getPageLinkWrapper($url, $text);
}
}
public function getLast($text = '»')
{
if ($this->currentPage >= $this->lastPage)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl($this->lastPage);
return $this->getPageLinkWrapper($url, $text);
}
}
}

Route [transaksi.index] is not defined. but the route already exists with route:resource

I don't know why the error is being thrown, because the route already exists.
master.blade.php:
<!-- Nav Item - Transaksi -->
<li class="nav-item">
<a class="nav-link" href="{{route('transaksi.index')}}">
<i class="fas fa-fw fa-folder"></i>
<span>Transaksi</span></a>
</li>
web.blade.php:
Route::resource('transaksi','TransaksiController');
TransaksiController:
public function index()
{
$data = DB::table('tbl_transaksi')
->where('tbl_transaksi.nama_peminjam','like',"%{$request->keyword}%")
->paginate(20);
return view('admin.transaksi.index',['data'=>$data]);
}
The error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [transaksi.index] not defined. (View: C:\xampp\htdocs\SistemPerpustakaan\resources\views\admin\master.blade.php)
http://localhost:8000/dashboard
resolved, I just forgot to delete the route with the same name

How to display notifications in Laravel?

I'm having a weird issue.
I have two notification classes InterviewRequestReceived.php and SendJobSeekerResume.php.
I'm trying to display the total number of notifications using count() next to a little bell icon and then a message related to the respective notification class. The messages are simple, they are located in /layouts/partials/notification.
1. interview_request_received.blade.php
<div>
<span class="font-weight-bold">You've been sent an Interview request!</span>
</div>
2. send_job_seeker_resume.blade.php
<div>
<span class="font-weight-bold">You've been sent a new Job Profile!</span>
</div>
in my admin file I have 2 roles, depending on if the user is logged in as a jobseeker or employer.
admin.blade.php:
<!-- Nav Item - Alerts -->
<li class="nav-item dropdown no-arrow mx-1">
<a class="nav-link dropdown-toggle" href="#" id="alertsDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Notifications<i class="fas fa-bell fa-fw"></i>
#if(Auth::user()->role_id === 1)
<!-- Counter - Alerts -->
<span class="badge badge-danger badge-counter">{{ $jobSeekerProfile->unreadNotifications->where('type', 'App\Notifications\InterviewRequestReceived')->count() > 0 ? $jobSeekerProfile->unreadNotifications->count() : '' }}</span>
#endif
#if(Auth::user()->role_id === 2)
<!-- Counter - Alerts -->
<span class="badge badge-danger badge-counter">{{ Auth::user()->unreadNotifications->where('type', 'App\Notifications\SendJobSeekerResume')->count() }}</span>
#endif
</a>
<!-- Dropdown - Alerts -->
<div class="dropdown-list dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="alertsDropdown">
<h6 class="dropdown-header">
Notifications
</h6>
#if(Auth::user()->role_id === 1)
#foreach($jobSeekerProfile->unreadNotifications as $notification)
<a class="dropdown-item d-flex align-items-center" href="#">
#include('layouts.partials.notification.'. Str::snake(class_basename($notification->type)))
</a>
#endforeach
#endif
#if(Auth::user()->role_id === 2)
#foreach(Auth::user()->unreadNotifications as $notification)
<a class="dropdown-item d-flex align-items-center" href="#">
#include('layouts.partials.notification.'. Str::snake(class_basename($notification->type)))
</a>
#endforeach
#endif
<a class="dropdown-item text-center small text-gray-500" href="#">Show All Alerts</a>
</div>
</li>
Both items with role_id === 1 are working, I get the count and the item with the message,
but the items where role_id === 2 I get count 0 and no item with message, it is very strange. Of course I'm viewing and testing with 2 accounts where I either have role_id set to 1 or role_id set to 2.
Here is a screenshot of my Notifications Table:
When I login with the user_id 12 (also role_id is set to 2) it should display all of the notifications where notifiable_id is set to 12.
I die and dumped my two models to see if the notifications are there,
employerProfile and jobSeekerProfile models like this, and I can see that there are no notifications in my employerProfile model, it is just an empty array. Below are screenshots.
dd($employerProfile->notifications);
dd($jobSeekerProfile->notifications);
EmployerProfile.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class EmployerProfile extends Model
{
use Notifiable;
protected $fillable = [
'user_id',
'company_name',
'company_phone',
'immediate_contact',
'email',
'company_address',
'number_of_employees'
];
public function user(){
return $this->belongsTo('App\User');
}
}
JobSeekerProfile.php model:
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class JobSeekerProfile extends Model
{
use Notifiable;
protected $fillable = [
'user_id',
'photo_id',
'resume_id',
'video_one_id',
'video_two_id',
'video_three_id',
'first_name',
'last_name',
'email',
'date_of_birth',
'full_or_part_time',
'experience',
'additional_skills',
'file'
];
public function user(){
return $this->belongsTo('App\User');
}
public function resume(){
return $this->belongsTo('App\Resume');
}
public function video(){
return $this->belongsTo('App\Video');
}
}
Can anyone help?
You are getting notifications from wrong Model.
According to your code and database structure,
assume login user id is 3,employer_profile_user_id is 12
$user->unreadNotifications get records from notifications table where notifiable_type is App\User and notifiable_id is 3;
$user->employerProfile->unreadNotifications get records from notifications table where notifiable_type is App\EmployerProfile and notifiable_id is 12;
so try this for count
#if(Auth::user()->role_id === 2)
<!-- Counter - Alerts -->
<span class="badge badge-danger badge-counter">{{ Auth::user()->employerProfile->unreadNotifications->where('type', 'App\Notifications\SendJobSeekerResume')->count() }}</span>
#endif
and this for detail
#if(Auth::user()->role_id === 2)
#foreach(Auth::user()->employerProfile->unreadNotifications as $notification)
<a class="dropdown-item d-flex align-items-center" href="#">
#include('layouts.partials.notification.'. Str::snake(class_basename($notification->type)))
</a>
#endforeach
#endif

array to string conversion in laravel

I am trying to fetch some data from table in laravel 5.0 like so
public function index()
{
$data = DB::table('modules')->get();
return view("BaseView.home")->with('data',$data);
}
this is my view
#foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
#foreach($moduleCategories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
#endforeach
</div>
</li>
#endforeach
$module->id is obtained from another query result. Now when I try to run this I am getting Array to string conversion. Can someone point out the mistake. The expected output is > 1 in the sense there can be multiple categories names matching that condition.
The problem is that you are trying to put php logic inside an echo {{ ... }}. You are trying to echo out a collection or an array of data, hence the error you are getting, Array to string conversion. The proper way to do this is to do the logic in the controller. But for a quick fix, replace:
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
to
<php $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get(); ?>
Never use {{ ... }} or {!! ... !!} to put logic, those are to echo out a string.
==EDIT==
I suggest to use laravels eloquent relationship methods, this will simplify the code, and seperate the logic from the view.
Note: Expecting if you are using laravel naming convention.
On your Modules model, add a relationship to ModuleCategory like so:
public function module_categories()
{
return $this->hasMany('App\ModuleCategory');
}
On your controller, on the index method replace :
$data = DB::table('modules')->get();
with
$data = Modules::get();
and finally on the view, change it like so:
#foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
#foreach($modules->module_categories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
#endforeach
</div>
</li>
#endforeach
As I commented on Carlos's solution, here is how I solved the same error...
//Controller
$users = User::where('blah', 'blah')->get();
return view('index', [
'users' => $users,
]);
//View
<script type="text/javascript">
angular.module("app").factory("DataService", function() {
return {
users: {!! $users !!},
};
});
</script>
As discussed above, this will result in the Array to string conversion error, since $users is an array.
However, this error won't happen if $users is a `collection'
I.e.
//Controller
$users = User::all();
return view('index', [
'users' => $users,
]);
//This is ok
Or,
//Controller
$users = collect([]);
return view('index', [
'users' => $users,
]);
//This is fine too

how to call controllers from the cakephp default layout?

I have a controller called UsersController, is there a way I can output some values from this controller to the cakephp default layout?
Here is what I have tried, the default.ctp:
<?php foreach ($users as $user): ?>
<ul class="nav navbar-nav navbar-right">
<?php if (!$this->Session->read('Auth.User.id')) : ?>
<li>
<?php echo $this->Html->link('Login',array('controller' => 'us 'action' => 'login', 'full_base' =>'true)); ?></li>
<li>
<?php echo $this->Html->link('Register', array('controller' => 'users', 'action' => 'add', 'full_base' => true)); ?>
</li>
<?php else: ?>
<li class="dropdown">
<a href="<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'edit', 'full_base' => true)); ?>" class="dropdown-toggle" data-toggle="dropdown">
<?php if(empty($user[ 'User'][ 'filename'])){ ?>
<i class="fa fa-user fa-lg"></i>
<?php }else{ echo $this->Html->image($user['User']['filename'], array('alt' => $user['User']['username'],'class'=>'img-responsive img-rounded','width'=>'32','height'=>'32','style'=>'display:inline; background:red;')); } ?>
<?php $users[ 'User'][ 'username']; ?> <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<i class="fa fa-gear fa-lg"></i> Settings
</li>
<li>
<i class="fa fa-power-off fa-lg"></i> Logout
</li>
<li>Something else here
</li>
<li class="divider"></li>
<li>Separated link
</li>
</ul>
</li>
<?php endif; ?>
</ul>
<?php endforeach; ?>
And here is my AppController.php:
public function beforeFilter(){
return $this->getuser();
}
public function getuser($id = null){
$this->loadModel('User');
$user = $this->User->find('all', array(
'conditions' => array('User.id' => $id)
));
$this->set('users',$user);
}
The problem with this code is that,it keeps returning the following notice,
Notice (8): Undefined index: User [APP\View\Layouts\default.ctp, line
71]
please how do i resolve this issue?
If you want all fields from logged user, you can use this in your view.
$user = $this->Session->read('Auth.User'));
This will return all storage data from users ( username, filename, all fields from user table)
Just change in your controller you have used
public function getuser($id = null){
insted of this use
public function beforeRender($id = null) {
Your AppController.php code should look like this
public function beforeFilter(){
return $this->getuser();
}
public function beforeRender($id = null){
$this->loadModel('User');
$user = $this->User->find('all', array(
'conditions' => array('User.id' => $id)
));
$this->set('users',$user);
}

Categories