I wanted to create pagination in index page and as I read in Laravel documentation it is possible to use paginate method with orderBy method when I do that I faced error:"Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.". The code I am using:
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->orderBy('asc')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}
#extends('layouts.app')
#section('content')
<h1>Домашняя Бухгалтерия</h1>
<br>
#if($balance==0)
<h2 class="text-center">Ваш Баланс: <span class="badge badge-primary">0.00</span></h2>
#elseif($balance<0)
<h3 class="text-center">Ваш Баланс: <span class="badge badge-danger">{{$balance}}</span></h3>
#else
<h3 class="text-center">Ваш Баланс: <span class="badge badge-success">+{{$balance}}</span></h3>
#endif
<br>
Создать
<br>
#if(count($personal)>0)
#foreach($personal as $pers)
<br>
<div class="card">
<h2><a href="/personal/{{$pers->id}}" >{{$pers->TypeOfAccounting}}</a></h2>
<h5 class="">Наименование:<span class="badge">{{$pers->Name}}</span></h5>
</div>
#endforeach
{{$personal->links()}}
#else
<p>Не найдено записей</p>
#endif
#stop
public function index()
{
$user = auth()->user(); // It's already the user model
$personal = $user->personalAccounting()->orderBy('YOUR_COLUMN')->paginate(2);
$balance = $user->balance;
return view('personal.index', compact('personal','balance'));
}
Notice personalAccounting() instead of personalAccounting The reason is, when you call the attribute without (), Laravel will load all the data from your DB in a collection. Every modification you do after, will be done with PHP.
When you request with the method using (), you're modifying the QueryBuilder which mean the orderBy and the pagination will be executed by MySQL which will also increase the performance and reduce the memory usage.
Give a column name before asc
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->orderBy('**your_column_name**','asc')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}
Try to use latest().
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->latest('***Add column name***')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}
Related
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
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?
I've been trying to get this fixed for days now. I don't know what i'm doing wrong. I'm trying to edit a post in the dashboard.
My route looks like this
Route::get('/edit-post/post_id/{post_id}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostedit',
]);
for that route i have a controller
public function showPostEdit(Request $request, Post $post, $post_id){
$posts= $post->where('post_id',$post_id)->get();
return view('pages.dashboard.user.edit-post',compact('posts',auth()-
>user()->id));
}
My blade syntax looks like this
#if(Auth::check())
#if(auth()->user()->id === $posts->user_id)
<li class="list-inline-item"><span class="fa fa-edit"></span></li>
<li class="list-inline-item"><span class="fa fa-trash"></span></li>
#endif
#endif
Solution 1: primary key should be id instead of post_id in post table.
Route::get('/edit-post/{post}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit(Post $post){
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}
Solution 2:
Route::get('/edit-post/{postId}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit($postId){
$post= (new Post())->where('post_id',$postId)->get();
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}
i want to send filter request to show only my discussions
it's my route
Route::resource('/forum','ForumsController');
<div class="list-group-item">
My Discussions
</div>
its my ForumController
switch (request('filter'))
{
case 'me':
$discussions = Discussion::where('user_id',Auth::id())->paginate(3);
}
Found a solution to send link :)
Home
This make the route like below:-
http://localhost/forum/public/forum?filter=me
If you user Route::resource function, it has default route name .
Route web.php
Route::resource('/forum','ForumsController');
View.php
<div class="list-group-item">
My Discussions
</div>
Controller.php
public function index(Request $request){
switch ($request->filter){
case 'me':
$discussions = Discussion::where('user_id', Auth::id())->paginate(3);
}
return view('View.php', compact('discussions'));
}
I am working on a profile page for my web application. Within this view profile.blade.php I would like to include the view progress/index.blade.php. I have the following structure:
profile.blade.php
<div class="panel panel-default">
<div class="panel-heading clearfix">
<h1 class="panel-title pull-left">{{ $user->name }} Progress</h1>
</div>
<div class="panel-body">
#include('client.progress.index')
</div>
</div>
web.php
Route::group(['middleware' => ['auth', 'client'], 'prefix' => 'client', 'as' => 'client.'], function () {
Route::get('home', 'Client\HomeController#index')->name('home');
Route::get('profile', 'Client\UserController#profile')->name('profile');
Route::get('progress', 'Client\UserController#progress')->name('progress.index');
});
UserController#progress
public function progress(){
$auth = Auth::user();
$progressPictures = Picture::select('*')
->where('user_id', $auth->id)
->get();
return view('client.progress.index', ['progressPictures' => $progressPictures]);
}
client.progress.index
<p>Progress pictures</p>
#foreach($progressPictures as $progressPicture)
<img src="/storage/uploads/progress/{{ $progressPicture }}" style="width:150px; height:150px; float:left; border-radius:50%; margin-right:25px;">
#endforeach
When I remove the php part from the index.blade.php, the site works. but when i add the foreach loop, $progressPictures is undefined. I am not calling the UserController#progress in some way. Could some one help me with this?
Generally based on my observation, the variable is not making it to the views because you are routing to another while the other view is handled by another controller.
One of the ways you can do that is either to have a trait where you can easily reuse the result of getting progressPictures or because you quickly need it, you might have to duplicate this code as well in the profile method in your UserController so that you can have the progressPictures also in profile page:
So you'll have:
public function profile()
{
//codes before or after
$auth = Auth::user();
$progressPictures = Picture::select('*')
->where('user_id', $auth->id)
->get();
//......
return view('profile', compact('progressPictures'));
Ps: unnecessary code repetition is not generally recommended, but I would do this first then clean up things after.
Change this to
return view('client.progress.index', ['progressPictures' => $progressPictures]);
to this
return view('client.progress.index')-> with('progressPictures', $progressPictures);
Change
#include('client.progress.index')
to
#include('client.progress.index', ['progressPictures' => $progressPictures])