Sending data from Controller to View in Laravel 7 - php

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 =>

Related

Undefined variable $result (View:/resources/views/frontend/CustomerRequest.blade.php)

when i run code then it gives this error: "Undefined variable $result (View:/resources/views/frontend/CustomerRequest.blade.php) ", anyone can plz suggest any solution. Thanks
controller:
public function index()
{
$result = DB:: select('select * from form_request');
$result= form::all();
return view('frontend.CustomerRequest')->with('result', $result);
}
view:
#foreach($result as $form)
<tr>
<td>{{ $form->id }}</td>
<td>{{ $form->name }}</td>
</tr>
#endforeach
route:
Route::get('customerrequest',[PostsController::class, 'index']);
Using the syntax return view('frontend.CustomerRequest')->with('result', $result); is wrong, that way does not pass the variable to the view page.
Instead it should be return view('frontend.CustomerRequest', compact('result');
So change your controller function to this
public function index()
{
$result = DB:: select('select * from form_request');
$result= form::all();
return view('frontend.CustomerRequest', compact('result');
}
Now in your view file you should be able to access the $result variable.

Passing an array of models to route to display in view from controller

So I have a view that would show all of the rows available and display them.
A route calls the controller to fetch the data:
Route::get('/list', array('as' => 'index', [ContactListController::class, 'getContacts']));
I assign my controller to grab all of the rows using:
class ContactListController extends Controller
{
public function getContacts()
{
$list = Contact::all();
return view('list')->with($list);
}
}
Then I display them in my view:
<tbody>
#foreach ($list as $contact)
<tr>
<form action="" method="post">
<input type="hidden" name="id" value="{{ $contact->id }}">
<td scope="row">{{ $contact->fname }}</td>
<td>{{ $contact->lname }}</td>
<td>{{ $contact->number }}</td>
</form>
</tr>
#endforeach
</tbody>
But the page says:
I would just like to display these data as a clickable table.
Please be easy on me since this is my first week of using Laravel ;)
At the first change your route to this:
Route::get('/list', [YourController::class, 'yourMethod'])->name('index');
and then in the controller:
class ContactListController extends Controller
{
public function getContacts()
{
$list = Contact::all();
return view('list')->with('list', $list);
}
}
I think you are using older route declaration with Laravel 8. The syntax of fromAction method in the screenshot of the error is Laravel 8 implementation.
Use either of this solutions:
// Using PHP callable syntax...
Route::get('/list', [ContactListController::class, 'getContacts'])->name('index');
// Using string syntax...
Route::get('/list', 'ContactListController#getContacts')->name('index');
More info on this is on Laravel 8.x Upgrade Guide

Destroy() in Basic Controller is not working

So I'm printing user complaints in table where I'm also printing a Delete button with every row. When I click that delete button, I want to delete that specific complaint from the table. I'm not using Resource Controller for this but a Basic Controller. Now, this is my code:
ViewComplaint.blade.php (Complaints Table with Delete Button):
<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Web.php (Routes):
Route::get('/complaint/create','ComplaintController#create')->name('complaint.create');
Route::post('/complaint','ComplaintController#store')->name('complaint.store');
Route::get('/complaint','ComplaintController#index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController#destroy')->name('complaint.destroy');
ComplaintController.php (Basic Controller):
class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user = User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}
Now when I click the Delete button on the table, it just gives me "404 | Not Found" error. What am I doing wrong here? I would really appreciate some help.
remove the $id from the route
Route::delete('/complain/{id}','ComplaintController#destroy')->name('complaint.destroy');
public function destroy($id) {
}
The route parameter is just a name; you are saying this particular route segment is dynamic and I want the parameter named complaint:
Route::delete('complaint/{complaint}', 'ComplaintController#destroy')->name('complaint.destroy');
Then you can adjust your destroy method to take the parameter complaint typehinted as Complaint $complaint to get the implicit binding:
public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}
Seems to me you're defining your route wrong. Change your route to:
Route::delete('/complaint/{id}','ComplaintController#destroy')->name('complaint.destroy');
You don't need an array() in your form opening, so hange your form opening to this:
{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}
And remove the $complaint->id from your submit button, you don't need it there.
All you have to do now inside your function is to find Complaint that has the id you passed in your form:
public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}
Let me know if you stumble on any errors.

Undefined variable: libros (View: C:\Users\CDS22\Desktop\Crud\resources\views\libros\index.blade.php) on laravel

I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}

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

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();
});

Categories