laravel pagination : Call to a member function render() on array - php

In laravel controller I have following code:
public function getAdmins(){
//$users = $this->user->all();
$search[] =array();
$search['name']= Input::get('name','');
$search['uname']= Input::get('uname','');
$search['role']= Input::get('role','');
$users = $this->user->findUsers($search);
$exceptSuperadmin = array();
foreach($users as $user){
if(!$user->isUser())
$staffs[] = $user;
}
$users = #$staffs;
return view('users::admins.list')->with('staffs',$users)->with('search',$search);
}
In Model I have:
public function findUsers($search)
{
return self::where('name','like','%'.$search['name'].'%')
->where('username','like','%'.$search['uname'].'%')
->where('role','like','%'.$search['role'].'%')
->paginate(5);
}
And In blade file I have:
#if($staffs)
#foreach($staffs as $staff)
<!-- Some code here to loop array -->
#endforeach
#else
No Staffs
#endif
{!! $staffs->render() !!} Error comes at this line
I am not geeting why this error comes....staffs is an array and render() a function to echo the pagination pages...but can't getting the error...Anybody to help.

By applying foreach the pager object and assign an array you lose the paging properties, so you will have an array rather than a pager object.
I recommend the following solution for your case:
Controller:
public function getAdmins(){
$search[] =array();
$search['name']= Input::get('name','');
$search['uname']= Input::get('uname','');
$search['role']= Input::get('role','');
$users = $this->user->findUsers($search);
return view('users::admins.list')->with('users',$users)->with('search',$search);
}
Blade file:
#if($users)
#foreach($users as $user)
#if(!$user->isUser())
<!-- Some code here to loop array -->
#endif
#endforeach
#else
No Staffs
#endif
{!! $users->render() !!}

NO, render() doesn't work on an object per se, neither on the array you are creating out of the required object for the pagination to work (LengthAwarePaginator)
Since you have a collection, and you need one, you could use one of the methods provided to do your filtering, such as filter.
Something like (untested but should work):
$staff = $users->filter(function ($value, $key) {
return !$value->isUser();
});

Related

Sending data from Controller to View in Laravel 7

I want to send my data from controller to xedit.blade.php, but I get the same error:
Undefined variable: users
in controller:
public function index3()
{
$users=User::all();
return view('xedit')->with('users' => $users);
}
Routes:
Route::get('/index3','Admin\UsersController#index3');
and I want to use $users in blade.Maybe there is a Route problem?
in your index method
public funtion index()
{
$users=User::all();
return view('xedit', compact('users'));
}
in your view add $users
<table>
#foreach ($users as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
</tr>
#endforeach
</table>
Your code logic is perfect, I guess you have to use proper naming with your routes because of Laravel Standard.
Route::get('/admin/show','Admin\UsersController#index')-name('admin.show');
public function index()
{
$users = User::all();
return view('xedit')->with('users' => $users);
}
In view, blade use a professional approach like below
#isset($users)
... loop ...
#endisset()
check record before sending to view by using dump and die function dd($users);
Want to comment but doesn't have 50 reputation
Replace ('users' => $users); this with (['users' => $users]); as you are using =>

Want to add {{$value['category']}} in {{$value['category']}} in Laravel

I want to set {{$value['category']}} in {{route('')}}
This is my code:
#foreach($details as $value)
{{$value['category']}}
#endforeach
Controller function:
public function viewhome(Request $req){
$product = product::all()->unique('category');
return view('homepage',['details'=> $product]);}
Route:
Route::get('/homepage/db_val', 'HomeController#db_val')->name('db_val');
How to declare href properly. And what will be the route. Thank you.
1) warn : you call db_val function in HomeController but you show viewhome method in your question.
Route::get('/homepage/db_val', 'HomeController#<b>db_val</b>')->name('db_val');
if you want use method viewhome :
Route::get('/homepage/db_val', 'HomeController#<b>viewhome</b>')->name('db_val');
2) route is used with named route
you have a route named 'db_val' --> Route::.... ->name('db_val');
so it must be used like that ,
<a href='{{route('db_val')}}
3) in your case , assuming $value in foreach is an array with a 'category' index inside
you can use url instead route
#foreach($details as $value)
<a href="{{url('/your_url')}}/{{$value['category']}}">
link to {{$value['category']}}
</a>
#endforeach
4) but blade spirit is
route.php
Route::get('/showcategory/{id}','HomeController#showcategorie')->name('showcat');
view
#foreach($details as $value)
<a href="{{route('showcat', $value['category'])}}">
#endforeach
it means : you have one named route /showcategory with a parameter /{id}
Route:
Route::get('/homepage/db_val_1', 'HomeController#db_val_1')->name('db_val_1');
Route::get('/homepage/db_val_2', 'HomeController#db_val_2')->name('db_val_2');
Route::get('/homepage/db_val_3', 'HomeController#db_val_3')->name('db_val_3');
...
Controller Function :
public function db_val_1(Request $req){
$all = product::all()->where('category', 'db_val_1');
return view('homepage', ['details'=> $all]);
}
public function db_val_2(Request $req){
$all = product::all()->where('category', 'db_val_2');
return view('homepage', ['details'=> $all]);
}
public function db_val_3(Request $req){
$all = product::all()->where('category', 'db_val_3');
return view('homepage', ['details'=> $all]);
}
...
View home: In route the value will be ($value->category) like this.
#foreach($details as $value)
{{$value['category']}}
#endforeach
I got the solution. Thank you for helping.

Grabbing the ID number in a blade

I have a route that takes me to my item page.
Route::get('/items/{item}', 'ItemsController#show');
In this page I have a form if the form is not filled out properly it will redirect to the forms method which currently is
action="/items"
How can I access that same {item} id number while in the blade?
Edit: Here is the Controller code
public function index()
{
$item = Post::get();
$price = $item->price;
return view('item');
}
public function show(Item $item)
{
return view('item', compact('item'));
}
You're returning compact('item') which gives you $item in your blade template.
You can use it within blade syntax, just like this:
{{ $item->id }}

How do I fetch data based on user id using Laravel 5.4

I have models User and Diares set up with the hasMany and BelongTo relstionship. I'm trying to fetch data from the Diaries table associated with a particular user. I tried auth but it didnt work for me. How can i achieve that and display it in the blade view as well. Here is my current code:
public function index(User $id)
{
$user = User::find($id);
$record = $user->diary;
return view('home')->with('record', $record);
}
The blade file it should display to:
#foreach ($record as $diary)
{{ $diary->error }}
{{ $diary->fix }}
#endforeach
In your index function the $id is not integer - this is a User instance, so you can try use this:
public function index(User $user)
{
$record = $user->diary;
return view('home')->with('record', $record);
}

Passing data from controller to view in Laravel

I am new to Laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.
I have a controller - ProfileController and inside that a function:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
In my view, I have this code:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
#foreach ($students as $student)
{{ $student->name }}
#endforeach
#stop
</body>
</html>
I am receiving this error: Undefined variable: students (View:regprofile.blade.php)
Can you give this a try,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
While, you can set multiple variables something like this,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
For Passing a single variable to view.
Inside Your controller create a method like:
function sleep()
{
return view('welcome')->with('title','My App');
}
In Your route
Route::get('/sleep', 'TestController#sleep');
In Your View Welcome.blade.php. You can echo your variable like {{ $title }}
For An Array(multiple values) change,sleep method to :
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
You can access you variable like {{ $author }}.
The best and easy way to pass single or multiple variables to view from controller is to use compact() method.
For passing single variable to view,
return view("user/regprofile",compact('students'));
For passing multiple variable to view,
return view("user/regprofile",compact('students','teachers','others'));
And in view, you can easily loop through the variable,
#foreach($students as $student)
{{$student}}
#endforeach
You can try this as well:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
Also, use this variable in your view.blade file to get students name and other columns:
{{$students['name']}}
Try with this code:
return View::make('user/regprofile', array
(
'students' => $students
)
);
Or if you want to pass more variables into view:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
In Laravel 5.6:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
try with this code :
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
#foreach($datas as $student)
{{$student}}
#endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
In laravel 8 and above, You can do route binding this way.
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
In the view file, you can access it like below.
#foreach($students as $student)
{{$student->name}}
#endforeach

Categories