Call to undefined function App\Transaksi() - php

i have a problem with my coding, i want to count user on my database but it's appear to be like this
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined function App\Transaksi()**
Here's my index.php
<div class="col-lg-4 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
<h3>{{ $list->transaksi }}</h3>
<p>Transaksi</p>
</div>
and my controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BackController extends Controller
{
public function list()
{
$transaksi = \App\Transaksi::get();
$transaksi = \App\Transaksi('id')->count();
return view('/admin', compact('list'));
}
}
and my routes
Route::group(['prefix'=>'user','middleware' => ['auth', 'role:member']],
function (){
Route::get('/mybook','FrontController#mybook');
});
Route::group(['prefix'=>'admin','middleware' => ['auth', 'role:admin']], function(){
Route::get('/',function() {
return view('admin.index');
});
Route::get('/','BackController#list');
Route::get('logout','UserController#logout');
Thanks.

Sorry For the late reply main bug in your list function is
public function list()
{
$transaksi = \App\Transaksi::get();
$transaksi = \App\Transaksi('id')->count();
return view('/admin', compact('list'));
}
You are Using the Same varible name to
Retrive all the records $transaksi = \App\Transaksi::get(); and
also for the Counting all the records $transaksi = \App\Transaksi('id')->count();
and also you are not passing it to the balde viewer
instead You are passing the function name list
return view('/admin', compact('list'));
Solution To you problem
First you need not to write \App\Transaksi in every where
Just Go to the top of the controller and add this line
use App\Transaksi;
Then Your function
There are several ways to count the record
Method 1:
public function list()
{
$transaksi= Transaksi::latest()->paginate(10);
return view('transaksi.index', compact('transaksi'));
}
here view('transaksi.index') refers to
Projectname/resources/views/transaksi/index.blade.php
Then in your index.blade.php to get the count just use the code
<h3 class="modal-title">{{ $transaksi->total() }} {{ str_plural('Transaksi', $transaksi->count()) }} </h3>
Method 2:
public function list()
{
$transaksi = Transaksi::latest()->get();
$recordCount = Transaksi::count();
return view('transaksi.index', compact('transaksi','recordCount'));
}
Then in your blade file
<h3>{{ $recordCount }}</h3>
Hope it helps

Your controller isn't doing much right now. The view needs to be a valid blade template file, e.g. admin.index, not /admin. You then need to pass a variable into the view, not the function name.
public function list()
{
$transaksi = \App\Transaksi::all();
$numTransaksi = $transaksi->count();
return view('admin.index', compact('numTransaksi'));
}
Then in the view you can access the count via {{ $numTransaksi }}.
You also have two routes for / in your web.php. You should get rid of the first one if you want the BackController#list function to call properly.
Is this what you mean?

Related

Call to undefined method App\User::id() (View:

I have created Custom Helper File App/Helpers/Helper.php for common functions, and I am trying to get data from the Helper class that I have created at Helper.php. Everything is fine when I call it in the blade file it shows an error "BadMethodCallException Call to undefined method App\User::id()"
Helper.php
<?php
use App\Cart;
use Illuminate\Support\Facades\Auth;
function totalCartItems()
{
if (Auth::check()) {
$user_id = Auth::user()->id();
$totalCartItems = Cart::where('user_id', $user_id)->sum('quantity');
} else {
$session_id = Session::get('session_id');
$totalCartItems = Cart::where('session_id', $session_id)->sum('quantity');
}
return $totalCartItems;
}
Getting value at cart.blade
<div class="breadcrumbs">
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Shopping Cart ({{ totalCartItems() }} item)</li>
</ol>
</div>
How to resolve it?
You should use:
Auth::user()->id
or
Auth::id()
instead.
Instead of doing this
Auth::check()
Use this
if(Auth::user() !== null)

ErrorException Undefined variable:

Why I am getting this error?
ErrorException
Undefined variable: features (View: C:\xampp\htdocs....views\layouts\index.blade.php)
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(compact('features'));
}
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products')->with(compact('products'));
}
layouts page- index.blade.php
#yield('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f ['features_id'] }}.00</div>
</div>
</li>
#endforeach
view page - index.blade.php
#extends('layouts.index')
#section('content')
#foreach($products as $p)
<div class="mb-2">{{ $p ['prod_sub_category'] }}</div>
<h5 class="mb-1 product-item__title">{{ $p ['prod_name'] }}</h5>
<div class="mb-2">
<img class="img-fluid" src="{{asset('/storage/admin/'.$p ['prod_image_path'] ) }}" alt="Image Description">
</div>
<div class="flex-center-between mb-1">
<div class="prodcut-price">
<div class="atext">LKR {{ $p ['prod_price'] }}.00</div>
</div>
<div class="d-none d-xl-block prodcut-add-cart">
<i class="ec ec-shopping-bag"></i>
</div>
web.php
Route::resource('/products', 'ProductsController');
Route::resource('/layouts/index', 'FeaturedController#index');
Aside from not passing your variables to your blade views appropriately which other answers have pointed out, your trying to access features from a controller that does not have features set.
The controller below sets features and then makes use of it in the layouts.index blade file.
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(['features' => $features]);
// or
// return view ('layouts.index', compact('features'));
}
While this controller sets products but then makes use of a blade file that extends another blade file that has a features variable in it. This is why your getting the error
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products', compact('products'));
}
And to fix it you must pass the features variable along side products like so:
ProductsController.php
public function index()
{
$products = Product::get();
$features = Feature::get();
return view ('products')->with(['features' => $features, 'products' => $products]);
}
But if more than one blade file is going to extend this layouts.index file then this approach is not advisable, and situations like this is why Taylor Otwell introduced Blade Components. You can now move the features blade view and logic to a component that can wrap around any other file you want or be included.
The documentation is straight forward but if you want me to show you how to implement it to solve your dilemma then hit me up on the comment below.
as u r using data in layout u should use laravel view composer to share data to layout file ref link https://laravel.com/docs/7.x/views#view-composers
in your AppServiceProvider.php
inside boot() add this line
public function boot()
{
\View::composer('layouts.index', function ($view) { // here layout path u need to add
$features = Feature::get();
$view->with([
'features'=>$features,
]);
});
}
It share data based on specif view file like here layouts.index data is send to this view so if u not send data from controller it will get data from view composer
You can change your controller to this:
public function index()
{
$features = Feature::all();
return view ('layouts.index', compact('features'));
}
A your blade you should actually do #section instead:
#section('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f->features_id }}.00</div>
</div>
</li>
#endforeach
#endsection

Why can't I have two separate functions returning two different views simultaneously?

I'm playing around with Laravel but I'm having an issue with the controller returning views. $aboutUs works but for $footerText I get an error that says:
Undefined variable: footerText (View:
/Users/user/sublime/blog/resources/views/about-
us.blade.php)
What am I doing wrong and how can I fix it?
Here's AboutUsController.php:
<?php
namespace App\Http\Controllers;
class AboutUsController extends Controller
{
public function index()
{
$aboutUs = "About Us";
return view('about-us', compact("aboutUs"));
}
public function indexTwo()
{
$footerText = "Some more text here";
return view('footer-content', compact("footerText"));
}
}
Here's views/about-us.bladephp:
#extends('layouts.about-us')
#section('title', $aboutUs)
#section('about-content')
<div class="container">
<h1>{{ $aboutUs }}</h1> {{-- This works --}}
<h1>{{ $footerText }}</h1> {{-- This doesn't work --}}
</div>
#endsection
In returning 2 things in a single function, try this using with.
public function index()
{
$aboutUs = "About Us";
$footerText = "Some more text here";
return view('about-us', compact("aboutUs"))
->with('footer-content', compact("footerText"));
}

Undefined variable: names in Laravel 5.6 app?

I am going to count some table column values using following controller function,
public function showcategoryname()
{
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
return view('_includes.nav.usermenu')->withNames($names);
}
then my route is,
Route::get('_includes.nav.usermenu', [
'uses' => 'VehicleController#showcategoryname',
'as' => '_includes.nav.usermenu',
]);
and my usermenu blade file is include with other blade files like this,
div class="col-md-3 ">
#include('_includes.nav.usermenu')
</div>
and usermenu blade view is,
#foreach($names as $name)
{{ $name->categoryname }} ({{ $name->cnt }})
#endforeach
in my url like this
http://localhost:8000/_includes.nav.usermenu
this is working fine. but when i visit other pages include usermenu blade it is generated following error,
Undefined variable: names (View: C:\Users\banda\Desktop\dddd\resources\views\_includes\nav\usermenu.blade.php) (View: C:\Users\banda\Desktop\dddd\resources\views\_includes\nav\usermenu.blade.php)
how can fix this problem?
it's clear that you are just using showcategoryname() method in _includes.nav.usermenu route not in every routes so it can't recognize that variable, it's better to use a global variable in all routes
so in app\Providers\AppServiceProviders.php in boot function use this code to have that variable in all routes:
view()->composer('*', function ($view) {
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
$view->with('names', $names);
});
this code runs before any code or controller! actually is feature of boot function!
You can insert this code into boot function in App\Providers\AppServiceProvider class
public function boot(){
$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();
View::share('names', $names);
}

Laravel Datatables - Multiple tables on one view

I'm using laravel 5.4 and the latest version of yajra/laravel-datatables as a service.
I have it working fine with one datatable on a page. I want to create a dashboard of unrelated tables. Users, products, bookings etc.
I was hoping to have my controller look something like this:
public function index(ProductsDataTable $productDatatable, UsersDataTable $userDatatable)
{
$user = Auth::user();
$products = $user->products;
return view('admin.dashboard', compact('products', 'user', 'productDatatable', 'userDatatable'));
}
and in my blade do
#section('content')
{!! $productDatatable->table() !!}
{!! $userDatatable->table() !!}
#endsection
#push('scripts')
{!! $dataTable->scripts() !!}
#endpush
However this obviously doesn't work. I'm unsure how to proceed.
I tried to create a route for each datatable but wasn't sure how to reference it from my dashboard controller.
I'm sure there's a better way of implementing multiple tables in one view, but this is what I came up with after reviewing this. Comments/improvements would be highly appreciated.
Controller
The controller will render the tables once in the index() method but will fetch data from both the getUsers() method or getProducts() method.
// DashboardController.php
public function index(UsersDataTable $usersDataTable, ProductsDataTable $productsDataTable)
{
return view('dashboard.index', [
'usersDataTable' => $usersDataTable->html(),
'productsDataTable' => $productsDataTable->html()
]);
}
//Gets Users JSON
public function getUsers(UsersDataTable $usersDataTable)
{
return $usersDataTable->render('admin.dashboard');
}
//Gets Products JSON
public function getProducts(ProductsDataTable $productsDataTable)
{
return $productsDataTable->render('admin.dashboard');
}
Routes
Add two extra routes that will be used to fetch Users and Projects data.
// web.php
Route::get('/', 'DashboardController#index')->name('dashboard.index');
Route::get('projects', 'DashboardController#getProjects')->name('dashboard.projects');
Route::get('users', 'DashboardController#getUsers')->name('dashboard.users');
DataTables Service Class
For both the UsersDataTable and ProductsDataTable service classes, include the relevant routes we created above.
// UsersDataTable.php
public function html()
{
return $this->builder()
->minifiedAjax( route('dashboard.users') );
}
View
// dashboard.blade.php
#section('content')
{!! $productsDataTable->table() !!}
{!! $usersDataTable->table() !!}
#endsection
#push('scripts')
{!! $productsDataTable->scripts() !!}
{!! $usersDataTable->scripts() !!}
#endpush
Submitted a question to the creator of the package. This is his response:
Unfortunately, DataTable service class is designed to handle single instance. However, I think we can make a workaround for it by adding additional query param in our request for us to identify which class is being requested to handle the request.
Maybe something like below:
public function index(ProductsDataTable $productDatatable, UsersDataTable $userDatatable)
{
if (request()->has('product') {
return $productDatatable->render('view');
}
if (request()->has('user') {
return $productDatatable->render('view');
}
$user = Auth::user();
$products = $user->products;
return view('admin.dashboard', compact('products', 'user', 'productDatatable', 'userDatatable'));
}
Step 1:
Define a route '/home-page' in web.php/route.php' (depending on the laravel version you are using) that returns the view called 'dt.blade.php'. (we will create this view in step 4)
i.e. Route::get('/home-page', function(){
return view('dt');
});
Step 2:
Suppose you want to display two dataTables in 'dt.blade.php' view. (first datatable shows all the students in a school while other shows all the classes in a school)
To do that, you need to create two Builder instances ('Builder' class belongs to DataTables package) in the '/home-page' route's callback function and pass them to the 'dt.blade.php' view . i.e
Route::get('/home-page', function() {
$student_dt = app(Builder::class)->columns(['id', 'student_name'])->ajax('/show-students-datatable')->setTableId('t1');
$classes_dt = app(Builder::class)->columns(['id', 'class_name'])->ajax('show-classes-datatable')->setTableId('t2');
return view('dt', compact('student_dt', 'classes_dt'));
});
Step 3
Now define two more routes in web.php/route.php file:
Route::get('/show-students-datatable', function () {
return datatables(App\Student::query()->select(['id', 'student_name']))->toJson();
});
Route::get('/show-classes-datatable', function () {
return datatables(App\Class::query()->select(['id', 'class_name'])))->toJson();
});
Step 4
Define the 'db.blade.php' view, this view show both the dataTables that were passed to it in step 1.
#extends('layouts.master')
#section('content')
{{ $student_dt->table() }}
{{ $classes_dt->table() }}
#endsection
#push('scripts')
{{$student_dt->scripts()}}
{{$classes_dt->scripts()}}
#endpush

Categories