Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Other resource routes like order and product controller index function is working and showing index page
but when I request for category index page laravel 5.8 says
Method Illuminate\Database\Eloquent\Collection::currentPage does not exist view categories/index.php
Here is the routing group which all other routes are working with except categories controller for showing index page from index function and dashboard for index function
Route::group(['prefix' => 'dashboard', 'namespace' => 'Dashboard', 'middlware' => ['auth:admin']], function () {
Route::name('dashboard.')->group(function () {
Route::resource('/', 'DashboardController');
Route::resource('/products', 'ProductController'); // ->except(['create', 'index']);
Route::resource('/categories', 'categoryController'); // ->except(['create', 'index']);
Route::resource('/orders', 'orderController'); // ->except(['create', 'index']);
});
});
they are working very well but when I configure multi authentication and make admin auth it starts showing these errors
please help if anyone working on laravel5.8
You're calling currentPage property statically from a collection instance instead of a method on a LengthAwarePaginator instance
Your controller must return a paginated query builder instance Illuminate\Pagination\LengthAwarePaginator like
class categoryController extends Controller
{
public function index()
{
$categories = \DB::table('categories')->orderByDesc('id')->paginate(4);
return view('dashboard.categories.index', compact('categories'));
}
}
Then in your dashboard/categories/index view, use the currentPage() as a function
{{ $categories->currentPage() }}
From the docs
PS: Don't just copy-paste this code, it may cause other errors as I just made my best guess to what your controller looks like
zahid hasan emon bro this is my controller
class categorycontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$data = [];
// $data['categories'] = \DB::table('categories')->get();
$data['categories'] = \DB::table('categories')->orderByDesc('id')->paginate(4);
return view('dashboard.categories.index',$data);
}
and this is my catagory page
#section('page-subtitle','List of all categories')
#section('content')
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"></h3>
New Category
<div class="box-tools pull-right">
{{(($categories->currentPage() * $categories->perPage())- $categories->perPage() )+1 }}
of {{$categories->total()}}
</div>
</div>
<div class="box-body">
<table class="table table-condensed table-striped table-responsive">
<tr>
<td>ID</td>
<td>Name</td>
<td>Action</td>
</tr>
#forelse($categories as $category)
<tr>
<td>{{$category->id}}</td>
<td>{{$category->name}}</td>
<td>
<div class="action-box">
<i class="fa fa-edit"></i>
<i class="fa fa-remove"></i>
</div>
</td>
</tr>
#empty
<tr class="text-center warning">
<td colspan="3">No Record Found</td>
</tr>
#endforelse
</table>
</div>
<!-- /.box-body -->
<div class="box-footer">
#if(count($categories))
<div class="pull-left">
{{$categories->links()}}
</div>
#endif
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
#endsection
i think currentpage() method which i used for showing number of pages is making problem ,but before making multi auth is was working very well
Related
i'm new to Laravel.
I have trouble in showing data to a view page with this error:
ErrorException
Undefined variable $ikus (View: D:\XAMPP\htdocs\SKP-PA-BKT\resources\views\iku.blade.php)
And Laravel give me solution:
$ikus is undefined
Here is code in iku.blade.php :
#extends('layouts.sidebar')
#section('content')
<section class="main-panel">
<div class="container">
<div class="section-title">
</div>
<div class="row">
<div class="col-md-12">
<div class="table-wrap">
<table class="table table-responsive-xl">
<thead>
<tr>
<th>No </th>
<th>Opsi</th>
</tr>
</thead>
<tbody>
<tr class="alert">
#foreach ($ikus as $iku)
<td>{{ $iku->id }}</td>
#endforeach
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
#endsection
Here's code in IkuController.php
<?php
namespace App\Http\Controllers
use App\Models\Iku;
use Illuminate\Http\Request;
class IkuController extends Controller
{
public function getIku()
{
$ikus = Iku::all();
//dd($ikus);
return view('iku', compact('ikus'));
}
}
And here's in web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\IkuController;
Auth::routes();
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/indikator-kinerja', [IkuController::class, 'getIku'])->name('iku');
It seems the $ikus variable from IkuController.php file can not be accessed by iku.blade.php file.
I have looked for solutions and most of what i've found is to add code like this:
return view ('layouts.index')->with(['features' => $features]);
or like this one:
return view ('layouts.index', compact('features'));
in IkuController.php file.
I have added it as written above. But i still get the same error. Is there anything left?
Thanks in advance.
This is most likely because your view is cached, you most likely ran the command
php artisan optimize
So just clear your view cache by doing:
php artisan view:clear
For the future, you can always see what is being cached by doing
php artisan about
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
my controller function is. I passed data through bidder array but array data can't print
public function bidders()
{
$payments = \App\Models\Payment::all();
$posts = Post::all()->where('user_id','=',Auth::user()->id);
foreach ($posts as $post) {
$postid = $post->id;
$bidder[] = Bid::all()->where('post_id','=',$postid);
}
return view('user.student.student-post-bidders',compact('bidder','posts','payments'));
}
Here is my blade in this blade my data print from DB.
How can I print array? indexes of array print in a blade
#php
dd($bidder);
$index = 0;
#endphp
#foreach($bidder as $bid)
#foreach($posts as $post)
#if($post->id == $bid[$index]['post_id'])
<div class="row" >
<!-- Dashboard Box -->
<div class="col-xl-12" >
<div class="dashboard-box margin-top-0">
<!-- Headline -->
<div class="headline">
<h4><strong><span class="bg-danger text-center" style="margin-left: 46px">Your Post Title:</span>{{$bid[$index]->Post->title}}</strong></h4>
</div>
<div class="content">
<ul class="dashboard-box-list">
<li>
<!-- Overview -->
<div class="freelancer-overview manage-candidates">
<div class="freelancer-overview-inner">
<!-- Avatar -->
<div class="freelancer-avatar">
<div class="verified-badge"></div>
#if($bid[$index]->Tutor->profile_Image=='')
<img data-cfsrc="{{asset('asset/images/user-avatar-placeholder.png')}}" alt="" style="display:none;visibility:hidden;">
#else
{{-- {{asset('storage/app/public/profileImages/' . $pic->profile_Image)}}--}}
<img data-cfsrc="{{asset('storage/app/public/profileImages/' . $bid[$index]->Tutor->profile_Image)}}" alt="" style="display:none;visibility:hidden;">
#endif
</div>
<!-- Name -->
<div class="freelancer-name">
<h4>{{$bid[$index]->Tutor->name}}</h4>
<!-- Details -->
<strong><span>Subject:</span></strong>
#foreach($bid[$index]->Tutor->subject as $sub)
<strong><span>{{\App\Models\Subject::find($sub->pivot->subject_id)->subject}}</span></strong>
#endforeach
<!-- Rating -->
<div class="freelancer-rating">
<div class="star-rating" data-rating="{{$bid[$index]->Tutor->rating}}"></div>
</div>
<!-- Bid Details -->
<ul class="dashboard-task-info bid-info">
<li><strong id="{{ $bid[$index]->id}}">PKR:{{$bid[$index]->bid_price}}</strong><span>Fixed Price</span></li>
<li><strong>Days:{{$bid[$index]->days}}</strong><span>Delivery Time</span></li>
</ul>
<!-- Buttons -->
<div class="buttons-to-right always-visible margin-top-25 margin-bottom-0" onclick=" indexID({{$i}})">
<a href="#small-dialog-1"
onclick="moveprice({{ $bid[$index]->id}}), payment({{ $bid[$index]->id}})" class="popup-with-zoom-anim button ripple-effect"
#foreach($payments as $pay) #if($bid[$index]->post_id == $pay->post_id) id="hide" #endif #endforeach><i class="icon-material-outline-check "></i> Accept Bid</a>
{{-- #dd($bid->id)--}}
<i class="icon-feather-mail"></i> Send Message
<a id="btn" class="button gray ripple-effect ico" onclick="removeBid({{$bid[$index]->id}})" title="Remove Bid" data-tippy-placement="top"><i class="icon-feather-trash-2"></i></a>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<span style="visibility: hidden">{{$i++}}</span>
#endif
#endforeach
#php
$index++;
#endphp
#endforeach
When I run code error will be occur
offset 1 I need all data print in a blade
How to print array index wise
you should use relations,
if you want to show the list of bidders then in bidders model
app/bidders.php
public function post()
{
return $this->belongsTo('App\Post');
}
Assuming that you have proper standards in table structure for mapping eloquent.
then you need to change your bidders function as below
public function bidders()
{
$payments = \App\Models\Payment::get();
$bidders = Bidder::whereHas('post', function($post) {
return $post->where('user_id','=',Auth::user()->id);
//Provides a filter for bidders, post which are related to user
})->with('post')->get();
return view('user.student.student-post-bidders',compact('bidders', 'payments'));
}
And there should be only one loop for bidders on blade as below.
Here is your answer why array is not appearing in blade check in comments for json_encode
#foreach($bidders as $bid)
{{$bid->id}} //gives you bid parameters
{{json_encode($bid->post)}} //this will get all the post details array/object is not printable on blade so encoded to json just for display purpose
//suppose you need only name from post then
{{$bid->post->name}};
#endforeach
This will lower your code length, as well as improve performance due to eager loading and less looping. Also removes stuff like creating array and maintain index and if condition on blade for id check.
That's it.
Happy Coding!!
I think you just need to refactoring your code ),
first of all use $bid->{$index} instead of this $bid[$index]. you have collection but you can use it as array
do you want to group your bidder by post_id ?
I am new to Laravel. I am learning Laravel from tutorial and I drive into one problem which I can't solve.
I think I have problem somewhere into Routing, but I can't find it
Funny thing is that if the href is {{route('tag.create'}}, then it goes to creating page, but when I need to use ID it's not working...
I had same functionality for posts and categories, but everything worked fine for those two. So I really need your help to see what I can't see. I have these files:
index.blade.php:
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<th>
Tag name
</th>
<th>
Delete
</th>
</thead>
<tbody>
#if($tags->count()>0)
#foreach($tags as $tag)
<tr>
<td>
{{$tag->tag}}
</td>
<td>
<i class="fa fa-trash" aria-hidden="true"></i>
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">
No tags yet
</th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
web.php - this is the place where I define routes for tags for TagsController.php:
//Tags
Route::get('/tags',[
'uses'=>'TagsController#index',
'as'=> 'tags'
]);
Route::post('/tag/update/{$id}',[
'uses'=>'TagsController#update',
'as'=> 'tag.update'
]);
Route::get('/tag/create',[
'uses'=>'TagsController#create',
'as'=> 'tag.create'
]);
Route::post('/tag/store',[
'uses'=>'TagsController#store',
'as'=> 'tag.store'
]);
Route::get('/tag/delete/{$id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
TagsController.php - at first I tried to destroy the element, then I tried to return create view(because when I go through /tag/create rout everything works), but neither worked here
public function destroy($id)
{
return view ('admin.tags.create');
/*
Tag::destroy($id);
Session::flash('success', 'Tag deleted succesfully');
return redirect()->back();*/
}
I believe that you should set the route to Route::get('/tag/delete/{id}',[ 'uses'=>'TagsController#destroy', 'as'=> 'tag.delete' ]); because in your case you are telling the route to expect a variable called $id
Please change the parameters in the route setup in web.php from $id to id. I should solve your issue.
Eg: Route::get('/tag/delete/{id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
Thanks !!.
I have two table in my database which are user and the attendance table. What I want to do now is I want to show the attendance data from database according to the user in their attendance view which is linked with their profile. This is my attendance function in the userController.
public function attendance($id)
{
$user = UserProfile::findOrFail($id);
$this->authorize('modifyUser', $user);
return view ('user.attendance', ['user'=>$user]);
}
This is my route to attendance view.
Route::get('/attendance/', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
This is my attendance view.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><i class="fa fa-university"></i> Attendance</h1>
</div>
<div class="row">
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Date</th>
<th>Time</th>
<th>Present</th>
</tr>
<?php $no=1; ?>
<tr>
<td>{{ $no++ }}</td>
<td>{{$user->attendance->date}}</td>
<td>{{$user->attendance->time}}</td>
<td>{{$user->attendance->present}}</td>
</tr>
</table>
</div>
</div>
</div>
#stop
This is the error that i got.
Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected".
I am new to laravel.
You're getting this error because attendance() method expects an ID and you don't pass it. Change the route to:
Route::get('attendance/{id}', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
And pass an ID when creating a link to the attendance() method:
{{ route('user.attendance', ['id' => 1]) }}
Or:
{{ url('attendance/1') }}
If you want to get ID of currently logged in user, do not pass ID. Use auth()-user() instead:
public function attendance()
{
$this->authorize('modifyUser', auth()->user());
return view ('user.attendance');
}
I have search box for my Laravel project it seems working because after request I'll get http://project.dev/search?q=fifth but nothing prints in blade template.
here is my SearchController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
class SearchController extends Controller
{
public function index() {
$countTodayOrders = Post::whereRaw('Date(created_at) = CURDATE()')->count();
$yesterday_posts = Post::whereRaw('Date(created_at) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)')->count();
$weekly_posts = Post::whereBetween( 'updated_at', [Carbon::today()->startOfWeek(), Carbon::today()->endOfWeek()] )->count();
$monthy_posts = Post::whereRaw('MONTH(created_at) = ?', Carbon::today()->month)->count();
$q = Input::get('q');
$posts = Post::where('title','LIKE','%'.$q.'%')->orWhere('slug','LIKE','%'.$q.'%')->get();
if(count($posts) > 0)
return view('theme.search', compact('posts', 'countTodayOrders', 'yesterday_posts', 'weekly_posts', 'monthy_posts'))->withQuery ( $q );
else return view ('theme.index')->withMessage('No Details found. Try to search again !');
}
}
Here is my search form
<!-- search box -->
<form action="/search" method="get" role="search">
<div class="input-group">
<input type="text" class="form-control" name="q" placeholder="Search users"> <span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
<!-- search box -->
Here is my routes
Route::any('/search', ['uses' => 'SearchController#index', 'as' => 'search.index']);
Here is my blade
#extends('layout.app')
#section('content')
<div class="col-md-9 technology-left">
<div class="tc-ch wow fadeInDown" data-wow-duration=".8s" data-wow-delay=".2s">
#if(isset($details))
<p> The Search results for your query <b> {{ $query }} </b> are :</p>
<h2>Sample User details</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Post</th>
</tr>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{$post->name}}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
</div>
</div>
#endsection
where is my mistake?
Update
Now I have search box which is work and show data with pagination but when i search for example for word sample which i have 3 posts with that title in first page of results I'm getting results like this:
first page
But when i go to second page everything changes!
second page
Why?
Ok guys i got it thanks, the issue was withQuery method as i used compact i shouldn't use with i just added it to `compact and now it's working. but what about counting the results? how can i pass the number of founded result in blade?