Show counter result in dashboard - laravel

I would like to create a dasboard with Laravel 8. I want to count all tickets in the database and display the number in the dashboard. Unfortunately it does not work do you have an idea?
Controller Code
namespace App\Http\Controllers;
use App\Models\Ticket;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
//
$ticketsCount = Ticket::count();
return view('dashboard.index', compact('ticketsCount'));
}
}
View Code
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount->count() }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>

As I can see, your router returns view file, and not getting into controller.
Route::get('/dashboard', function () {
return view('dashboard.index');
});
change your router to (Laravel version before 8)
Route::get('/dashboard', 'DashboardController#index');
After Laravel version 8
use App\Http\Controllers\DashboardController;
Route::get('/dashboard', [DashboardController::class, 'index']);
Docs

Remove ->count() from your view, you already counted it in controller
$ticketsCount = Ticket::count();
The view code:
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>

I have tried to get the number of users displayed on the view. Unfortunately I always get an error message...
Error:
ErrorException
Undefined variable: counter (View: /laravel/resources/views/dashboard/index.blade.php)
Route (web):
Route::get('/dashboard', function () {
return view('dashboard.index');
});
Model (Dashboard)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model
{
use HasFactory;
}
Controller (DashboardController)
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function index()
{
$counter = DB::table('users')->count();
return view('dashboard.index', compact('counter'));
}
}
View (dashboard/index.blade.php):
Users: {{ $counter }}

Related

How do I do pagination on my code in Laravel?

So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php
BusinessListController.php
`<?php
namespace App\Http\Controllers;
use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;
class BusinessListController extends Controller
{
public function index(Request $request)
{
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->get();d
return view('pages.business-list', [
'businesses' => $businesses,
'locations' => Location::all(),
'categories' => Category::all()
]);
}
}`
And then here is the code for my view blade front-end
Business-List.blade.php
<div class="row business-list-row mx-auto">
#foreach ($businesses as $business)
<div class="col-md-4">
<div class="card shadow border-light mb-3">
<img
src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
class="card-img-top" alt="...">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title h6" style="font-weight: bold;">
{{Str::limit($business->name, 20, $end='...')}}
</h4>
<div class="">
<p class="card-text">
{{ $business->location?->name }}
</p>
<p class="card-text" style="color: #32a852;">
{{ $business->category?->name}}
</p>
</div>
</div>
<div class="align-self-center">
<a href="{{ route('store', $business->id) }}" class="btn btn-info stretched-link">
Visit
</a>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
So you need to do three things.
In Controller:
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->paginate(15);
put the number of items you need on a single page. here I put 15.
Put this under the </div> of your list.
{{ $business->links() }}
Put this inside the App\Providers\AppServiceProvider boot method.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrapFive(); // or
Paginator::useBootstrapFour();
}
This depends upon which version of Bootstrap you are using.
Still confused? Checkout Laravel Pagination Documentation
Just remove ->get();d and add paginate
example
ModelName()->paginate();

Facade\Ignition\Exceptions\ViewException Error on Laravel?

0
I reached a part I cannot figure out. When trying to display a product on home page I get the following error:
Undefined variable: products (View: /home/acer/test/project_basket/basket/resources/views/home.blade.php)
For me is the first project in php and i'm not very practice in this language.
home.blade.php:
#section('content')
<div class="card-deck">
/*Problem Here */
#foreach ($products as $product)
<div class="card">
<img src="{{ $product->imagePath }}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ $product->title }}</h5>
<p class="color">{{ $product->color }}</p>
Buy Now
<button type="button" class="btn btn-primary float-right">Add to Cart</button>
<div class="price">${{ $product->price }}/div>
</div>
</div>
#endsection
Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['imagePath', 'title', 'price', 'color'];
}
ProductController.php:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
//use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
*#return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::inRandomorder()->take(6)->get();
return view('home')->with('products', $products);
}
}
Routes:
//Route::view('/`home`', 'home');
Route::get('/', 'ProductController#index')->name('home');
Auth::routes();
Route::get('/home', 'ProfilesController#index')->name('home');
Route::get('/', 'ProfilesController#index')->name('welcome');
//Route::get('/home', 'DasboardController#index')->name('dashboard');
You hare "hitting" the /home endpoint, which looking at your route is pointing to ProfilesController, but you are working on ProductController so have written the wrong Controller in web.php

Call to a member function get() on null for a function in a model

I am trying to have a button of an employee show the text “Already booked” if the variable
count($bookingRequests) returns something and if nothing then show “Book Me’
I get the error above.
I have the following models and their relationships:
User.php and Bookings.php
User:
public function bookings() {
return $this->hasMany('App\Models\Bookings');
}
public function BookingRequest()
{
$this->bookings()->where('booking_status','=','confirmed')->get();
}
Bookings:
public function user(){
return $this->belongsTo('App\Models\User');
}
The view mentioned above is Employeeblock.blade.php:
<div class="row no-gutters mb-5 mb-lg-0" style="padding:10px">
#if(count($bookingRequests))
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target=“#bookingModal">Already Booked</a>
#else
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target="#bookingModal">Book Me</a>
#endif
</div>
Which has data passed onto it by a controller SearchController:
In the following way:
<?php
namespace App\Http\Controllers;
use DB;
use Auth;
use App\Models\Employee;
use App\Models\User;
use App\Models\Bookings;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Session;
.
.
.
$bookingRequests=Auth::user()->BookingRequest()->get();
return view(‘search.results’)
->with('bookingRequests',$bookingRequests)
results.blade.php above has the following html:
<div class="panel panel-default">
<div class="panel-heading"><h3></h3></div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
#foreach($Employees as $Employee)
#include('user/partials/employeeblock')
#endforeach
</div>
</div>
</div>
</div>
</div>
Replace:
$bookingRequests=Auth::user()->BookingRequest()->get();
with
$bookingRequests=Auth::user()->BookingRequest();
Because your BookingRequest() method already use ->get() method to obtain data from db.

Two controllers one route laravel

I can currently show the index page along with a list of posts. The user can select a post to view post details. For this I have:
Routes:
Route::get('/', 'PostsController#showPosts');
Route::get('post/{slug}', 'PostsController#showPostDetails');
Controller:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function showPosts()
{
$posts = Post::simplePaginate(2);
return view('index', ['posts' => $posts]);
}
public function showPostDetails($slug)
{
$post = Post::findBySlug($slug);
return view('post.show',['post'=>$post]);
}
}
Model:
public static function findBySlug($slug)
{
return static::where('slug', $slug)->first();
}
index
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Train Testing</h1>
<span class="subheading">Testing Times</span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
</div>
</div>
#stop
partials/post.blade.php
<div class="post-preview">
<a href="/post/{{ $post->slug }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->excerpt }}
</h3>
</a>
<p class="post-meta">Posted by
{{ $post->author->name }}
on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>
I now want to show the posts on all other pages. My other pages currently have the route Route::get('{slug}', 'PagesController#show'); And it makes sense initially to refer back to existing code so use Route::get('{slug}', 'PostsController#showPosts'); like I did on the home page to display posts there.
However I am not sure of the best way to deal with this as I believe you can not have two controllers for one route.
For other pages I currently have:
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$page = Page::findBySlug($slug);
return view('page', ['page' => $page]);
}
}
page.blade:
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading"> <h1>{!! $page->title !!}</h1>
<span class="subheading"></span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{!! $page->body !!}
</div>
</div>
</div>
#stop
Pages Controller:
use App\Page;
use App\Post;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$posts = Post::simplePaginate(2);
$page = Page::findBySlug($slug);
return view('page', ['page' => $page, 'posts' => $posts]);
}
Then in page.blade.php I can call $posts without any errors:
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
Not sure if this is the neatest way but it works.

Undefined Variable in view, Laravel

I am very new in Laravel. I have made a controller. And there I have declared a variable. I want to pass it to a view. But it says variable undefined.
this is the controller.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
//
public function about(){
$name= 'XYZ';
return view('pages.about')->with('name', $name);
}
}
this is the view
<html>
<body>
<div class="container">
<div class="content">
<div class="title"> About Me: {!! $name !!}} </div>
</div>
</div>
</body>
</html>
Try and return your data to the view like this:
return View::make('pages.about' , array(
'name' => $name
));
Then echo the data in the view with blade like this:
<div class="title"> About Me: {{ $name }} </div>

Resources