I want to fetch data from database table named 'users' and display the output in a table.
I have written below code but it's not working.
I am using Laravel 5.5
#extends('layouts.app')
#section('content')
<div class="container">
<h2>Admin Panel</h2>
<p>Data of the Registered Users.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
$users = DB::table('users')->select('id','name','email')->get();
#foreach($users as $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
Error: Undefined Variable: users
The problem is that you're trying to mix PHP within your (Blade) template. Although it is possible to do so with #php directives, this is bad practice: your view should not contain any business logic.
Ideally you want to pass this data from your controller. It should look something like this:
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index()
{
$users = DB::table('users')->select('id','name','email')->get();
return view('some-view')->with('users', $users);
}
}
Read more on passing data to views here.
You are doing it all wrong, the point of MVC design is to have your controllers do the application logic, your views to represent a representation of that data and models to contain the data you need, in your case you are missing the point completely by using DB::table inside of your view, so here is an example code which you might need to correct a bit:
The example below doesn't show MVC pattern since the data is passed from inside a closure of a route
web.php
Route::get('/', function () {
$users = DB::table('users')->select('id','name','email')->get();
return view('VIEW-NAME-HERE', compact('users'));
});
view.blade.php
#foreach($users as $user)
{{ $user->id }} {{ $user->name }} {{ $user->email }}
#endforeach
Change VIEW-NAME-HERE with the name of your view file, for example index or users.index
You Are using php in blade file
first make controller
for controller run a command in terminal
php artisan make:controller usercontroller
then write this:
class usercontroller extends Controller
{
public function index()
{
$users = DB::table('users')->select('id','name','email')->get();
$data = compact('users')
return view('your-view-name')->with('$data');
}
}
Related
I'm working with Laravel 8 and I have made a table like this at Blade:
<div class="card-body table-responsive p-0">
<table class="table table-hover">
<tr>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Actions</th>
</tr>
#foreach($roles as $role)
#if(count($role->users))
#foreach($role->users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $role->name }} | {{ $role->label }}</td>
<td>
<form action="{{ route('levels.destroy' ,$user->id) }}" method="post">
#method('DELETE')
#csrf
<div class="btn-group btn-group-xs">
Edit
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</td>
</tr>
#endforeach
#endif
#endforeach
</table>
</div>
And the result perfectly showing up:
But now I got problem with Edit & Delete buttons that I have specified $user->id as parameter for both of them.
And when I hover over the buttons I can see the user id properly defined:
But when it comes to edit method which is using Route Model Binding, it does not find the user:
public function edit(User $user)
{
dd($user->id); // return null
}
However if I do not use Route Model Binding and say this instead:
public function edit($id)
{
dd($id); // return 1
}
It properly shows the user id!
I don't know why the Route Model Binding not working here, so if you know what's going wrong or how to fix this issue, please let me know...
You are trying to access the User Model which in this case doesn't know what id is, so you should be passing the id of the user to the edit route using either Get by passing it to the url endpoint , so now you can get it like
public function edit($id)
{
dd($id); // return null
}
or by sending it as a POST form and get it like
public function edit(Request $request)
{
dd($request->id); // return null
}
I saw comments, your resource controller name is not matching with your variable name "$user".
You can look here on official laravel docs.
In your situation, this might help;
Route::resource('levels', LevelController::class)->parameters([
'levels' => 'user'
]);
I just want to show data from database using compact method, but it seem like it doesn't work , and I don't know what is wrong.
Controller:
public function index()
{
$sections = Sections::all();
return view('sections', compact('sections'));
}
View:
#foreach($sections as $section)
<tr>
<td>#</td>
<td>{{ $section->section_name }}</td>
<td>{{ $section->description }}</td>
<td>{{ $section->Created_by }}</td>
<td>
</tr>
#endforeach
route web.php
Route::resource('sections', 'SectionsController');
in web.php route for resource write it like
Route::resource('sections', SectionsController::class);
=>on laravel 8 use this route and import on top on route like
use App\Http\Controllers\SectionsController;
Route::resource('sections', SectionsController::class);
import top on your controller
use App\Models\Sections;
its working your fine.i can try your code its work.
I'm an absolute beginner and I'm trying to do a CRUD in Laravel, but I can not figure it out why my variables aren't being passed over to the view so I can have a dynamic display of them in the table.
My "routes" work fine.
Then my controller
public function getHome()
{
$results = Crud::index();
return view('pages/home', ['results' => $results]);
}
Calls my "Crud" model
class Crud extends Model
{
public static function index()
{
return $results = DB::table('data')
->whereNotNull('id')
->get();
}
}
And goes to my view as seen in the controller
#extends('main')
#section('title', '| Home')
#section('content')
<div class="container">
#include ('partials/_jumbotron')
<table class="table table-inverse">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
#if(!empty($results))
#foreach($results as $result)
<tr>
<td scope="row">$result->$id</th>
<td>$result->$first_name</td>
<td>$result->$last_name</td>
<td>$result->$username</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
#endsection
Thanks for the help in advance...I'm losing my mind
And sorry If I wasn't specific enough..
Edit1: I did as you say, but I'm still not outputing data to my table? Is there anything else wrong in the stated code or is the bug somewhere deeper?
Edit2: Thanks everyone for the pointed out mistakes - blade {{ }} and object properties
$object->propertie not $object->$propertie
When I fixed those mistakes which were obviously denying the output I remembered that I have an empty database.. Case Closed - Thanks everyone for the help
You're not printing the variables. You're printing literals.
If you want to print the variables (echo them) in blade files, you need to wrap them in curly brackets. Also there's not $ when you want to display an object attribute. Like that:
#foreach($results as $result)
<tr>
<td scope="row">{{ $result->id }}</th>
<td>{{ $result->first_name }}</td>
<td>{{ $result->last_name }}</td>
<td>{{ $result->username }}</td>
</tr>
#endforeach
I am a beginner too, I thought maybe you might want to try this as well
public function getHome()
{
$results = Crud::index();
return view('pages/home', compact('results'));
}
But before anything else make sure if your code is actually returning your data from DB using
dd($results)
The first of all cleaner will be using laravel with function for example
return view('pages/home')->with('results', $results);
Next you try to get $result->$first_name it didint working becouse first_name is not variable, laravel return object so you can get first_name like that:
{{$result->first_name}}
I'm new with Laravel and there are some concepts that I don't understand very well. I really need your help on this because I've read many many pages, tutorials and stackoverflow solutions and I dont get the results that I need.
I have a list of "patients" shown on a index page from a database.
Here is the code:
#if($data)
<table class="table">
<thead>
<tr>
<td>Nombre de Paciente</td>
<td>Cedula</td>
<td>Fecha</td>
<td>Telefono</td>
<td>Email</td>
<td>Direccion</td>
<td>Fecha de Creación</td>
<td></td>
</tr>
</thead>
<tbody>
#foreach($data as $row)
<tr>
<td>
{{ $row->paciente }}</td>
<!--<td>{{ $row->paciente }}</td>-->
<td>{{ $row->cedula }}</td>
<td>{{ $row->fecha_nacimiento }}</td>
<td>{{ $row->telefono }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->direccion }}</td>
<td>{{ $row->created_at }}</td>
<td>
</td>
</tr>
</tbody>
#endforeach
</table>
#endif
What I want to do, and as you can see, the "patient" name is a link, which referese to (in this case)a form.
I want to get two things from this.
ONE: I want to get the id of the patient I'm clicking on and the name of the "patient" and send it to the form page.
TWO: on the form page I want to show the "patient" name on the top of the form, and have the "patient_id" hidden to save it on the form table (this patient_id will be a foreing key)
This are my current components:
ROUTE:
Route::get('/care/{id}', [
'as' => 'create',
'uses' => 'CareController#create' ]);
CONTROLLER
public function create($id)
{
//
return view ($this->path.'.care',['id'=>$id]);
}
VIEW:
<div class="form-group">
<label for="exampleInputEmail">Paciente </label>
<input type="hidden" name="id" value="{{$id}}">
</div>
But whenever I load the page, it shows a blank page and on the url I see the following:
http://hospital.dev/care/$row->id
I really appreciate your help with this.
UPDATE:
THANKS.
Its sending now the id. But when trying to display the form page, it still sends the blank page, and the url is the following:
"http://hospital.dev/care/1"
I dont know if it is something on the controller:
CONTROLLER:
public function create($id)
{
//
return view ($this->path.'.care',['id'=>$id ]);
}
how should I pass the id value no to the url but to the view?
You could simply concatenate the href:
<a href='/care/{{$row-id}}'></a>
Once you have passed the id into your route. You can then handle with the route definition and pass it to your other controller.
for example a view that creates dynamic links:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Endpoint</div>
</div>
<table class="table table-bordered" id="endpoint-table">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>ip</th>
<th>mac</th>
<th>proxy</th>
<th>model</th>
</tr>
</thead>
<tbody id="endpoint-table-body">
#foreach($endpoints as $endpoint)
<tr><td>{{$endpoint->id}}</td><td>{{$endpoint->name}}</td><td>{{$endpoint->ip}}</td><td>{{$endpoint->mac}}</td><td>{{$endpoint->proxy}}</td><td>{{$endpoint->model}}</td></tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
then in your routes/web.php:
Route::get('/endpoint/{id}', 'EndpointController#show');
then it gets passed to the controller # function show where you can do some logic:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$endpoint = Endpoint::getObjectById($id);
$e = new \stdClass();
$e->id = $endpoint->id;
$e->name = $endpoint->name;
$e->ip = $endpoint->ipaddress;
$e->mac = $endpoint->macaddress;
$e->model = $endpoint->model_id;
$e->proxy = $endpoint->proxy_id;
$endpoint = $e;
$endpoints = array();
$endpoints[] = $endpoint;
return view('endpoints', ['endpoints' => $endpoints]);
}
and when you return your view. You can pass it data passed from the function you defined on your controller. In this instance that function is show() on the EndpointController.
The reason that the url is http://hospital.dev/care/$row->id is because you are using single quotes, so it won't get the value of the variable.
In the list replace:
{{url('/care/$row->id')}}
With:
{{url('/care/'+$row->id)}}
Here you're echoing variable $row->id as a string <a href="{{url('/care/$row->id')}}">, you need to print the value.
Since you've got you routes named I suggest you rewrite it like this using route() helper:
<a href="{{ route('create', ['id' => $row->id]) }}">
Here I called the route() helper, passed the route name create as first argument and the {id} that the route expects as second argument.
Another way would be to just correct your method, by moving the variable out of '' and appending it to the url like this:
<a href="{{url('/care/' . $row->id)}}">
Period . appends a string to another string in php, not +
The link you are using is like id')}}">
so here you are printing the id as a string. But you need to use this like
id)}}">
OR
id}}">
So I have a page setup with a table of items. In each row there is a link. When the link is clicked I want to pass the ID of them through to the controller so I can pass it on to another view. Although I can't figure out how to do this.
This is the code in my item view
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
As you can see there is a link which leads me to the rent view. In the controller this is all I have.
public function rent()
{
return view('rent');
}
Any help would be appreciated thanks.
I'd probably do it something like this.
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
And then inside your controller you can accept the the value you are passing.
public function rent($value)
{
return View::make('new-view')->with('value', $value);
}
and then inside your new-view.blade.php
<p> The value I passed is: {{ $value }} </p>
Read more about Laravel url helpers here https://laravel.com/docs/5.1/helpers#urls
You can use route() helper:
#foreach($items as $item)
<tr>
<td>{{ $item->title }}</td>
<td>{{ $item->genre }</td>
<td>{{ $item->description }}</td>
<td>View</td>
</tr>
#endforeach
As alternative you can use link to action:
{{ action('RentController#profile', ['id' => $item->someId]); }}
And sometimes it's useful to use url() helper:
{{ echo url('rent', [$item->someId]) }}
More on these helpes here.
If you're using Laravel 5 with Laravel Collective installed or you're on Laravel 4, you can use these constructions to generate URLs with parameters.
PS: If you're using tables for layout, don't do it. You should learn DIVs, because it's really easy now to build cool layout with DIVs using Bootstrap framework which is built-in Laravel.