Laravel 5.4: execute a raw query and show data in view - php

I want to run a raw query which I have written in my controller and want to show the data from database in my view.
This is my controller function:
public function unverified_jobs_page()
{
$query = "SELECT jd.*, cd.`company_name`, jc.`category_title`, jt.`job_type_title`, cc.`city_name`
FROM `job_details` AS jd
JOIN `company_details` AS cd ON cd.`company_id`=jd.`company_id`
JOIN `job_category` AS jc ON jc.`category_id`=jd.`category_id`
JOIN `job_type` AS jt ON jt.`job_type_id`=jd.`job_type_id`
JOIN `city` AS cc ON cc.`city_id`=jd.`city_id`
WHERE jd.`is_verified`='NO'
ORDER BY jd.`date_posted` DESC";
$sql = General_model::rawQuery($query);
return view('adminpanel.jobs.unverfied_jobs',compact($sql));
}
This is my model function rawQuery()
public static function rawQuery($query)
{
return DB::select( DB::raw($query) );
}
This is my view:
<table class="table table-striped border-top" id="sample_1">
<thead>
<tr>
<th>JOB ID </th>
<th>TITLE</th>
<th>COMPANY</th>
<th>CITY</th>
<th>DATE POSTED</th>
<th>CLOSING DATE</th>
<th> </th>
</tr>
</thead>
<tbody>
#foreach($sql as $d)
<tr>
<td>{{$d->job_id}}</td>
<td>{{$d->job_title}}</td>
<td>{{$d->company_name}}</td>
<td>{{$d->city_name}}</td>
<td>{{$d->date_posted}}</td>
<td>{{$d->expiry_date}}</td>
<td><a class='btn btn-info' href='#'>DETAILS</a></td>
</tr>
#endforeach
</tbody>
</table>
Now the problem is, every time I try to run the page, it gives me the following error:
ErrorException in 10f46e45beef75fdd05b083aa636ae7821fe6936.php line 30:
Undefined variable: sql (View: C:\AppServ\www\jobs\jobs\unverfied_jobs.blade.php)
Please Help.

You need to pass the variable $sql in to your view properly.
Try
compact('sql');

Related

Get desired Data from pivot Table

I have three model one is pivot table (grade_studentattendances) and other model are (grades and studentattendances ). i just want to show data of student from one grade to eight grade only.
my view code is:
<table border="1">
<tr>
<th>Id</th>
<th>Full name</th>
<th>Roll no</th>
<th>Date</th>
<th>Status</th>
<th>Grade</th>
</tr>
#php
$i=1;
#endphp
#foreach($studentattendance as $item)
#php $grades=$item->Grade; #endphp
<tr>
<th>{{$item->id }}</th>
<th>{{$item->name}} </th>
<th>{{$item->roll_no}}</th>
<th>{{$item->date}}</th>
<th>{{$item-> status}}</th>
<th>
#foreach($grades as $grade)
{{$grade->class_name }}
#endforeach
</th>
</tr>
#endforeach
</table>
my controller is:
public function sattend()
{
$studentattendance=studentattendance::all();
return view('primaryhead.s_attend',compact('studentattendance'));
}
i just want to show the of students from grade one to grade eight in the form of table

Undefined property: stdClass::$point

I want to display user data in table but when I display point i got the error
Undefined property: stdClass::$point (View:
C:\xampp\htdocs\Gelecek_Üyeliği_Sistemi_f\Gelecek_Üyeliği_Sistemi_f\resources\views\backend\usersaction\total.blade.php)
this is the function i use it to display the data in view
public function ViewActionUserTotal() {
$total = DB::table("action_users")
->join("users", "users.id", "action_users.user_id")
->select("users.id","users.name","users.email", DB::raw("sum(action_users.point)"))
->groupBy("users.id","users.name","users.email",)->get();
return view('backend.usersaction.total',compact('total'));
}
here is the foreach in the table
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>Total Point</th>
</tr>
</thead>
<tbody>
#foreach($total as $tot)
<tr>
<td>{{$tot->name}}</td>
<td>{{$tot->email}}</td>
<td>{{$tot->point}}</td>
</tr>
#endforeach
</tbody>
</table>
when i use dd for the data it show like that
I think you forgot to make an alias for sum query .Your query should be like this :
$total = DB::table("action_users")
->join("users", "users.id", "action_users.user_id")
->select("users.id","users.name","users.email", DB::raw("sum(action_users.point) as point"))
->groupBy("users.id","users.name","users.email",)->get();
For more :https://laravel.com/docs/7.x/database#running-queries

My table headers are being displayed but no data is being fetched from the database

This here is my join controller. There is no error but the problem is that the data is not being shown in the table. The headers from blade.php file are being shown but the data is not being fetched or shown in the table.
My Join Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class JoinController extends Controller
{
//
public function report()
{
$data = DB::table('pmedicalreadings')
->join('medrecords','medrecords.id','=','pmedicalreadings.id')
->join('payments','payments.id','=','pmedicalreadings.id')
->select('pmedicalreadings.bp','pmedicalreadings.temp','pmedicalreadings.ecg','medrecords.prerecord','payments.ptype','payments.tpayment','payments.rpayment')
->get();
return view('report',compact('data'));
}
}
My Routes from web.php:
Route::post('/report','JoinController#report');
Route::get('/report', function () {
return view('/report');
});
My report.blade.php file:
<center><h1>Patient Report</h1></center>
<form >
<table class="table table-bordered table-striped">
<tr class="bg-danger text-light">
<th>Patient Blood Pressure Reading</th>
<th>Patient Temperature Reading</th>
<th>Patient ECG Reading</th>
<th>Patient Medical Record</th>
<th>Payment Type</th>
<th>Total Payment</th>
<th>Remaining Payment</th>
</tr>
#if(isset($data))
#foreach($data as $row)
<tr class="bg-secondary text-light">
<td>{{$row->bp}}</td>
<td>{{$row->temp}}</td>
<td>{{$row->ecg}}</td>
<td>{{$row->prerecord}}</td>
<td>{{$row->ptype}}</td>
<td>{{$row->tpayment}}</td>
<td>{{$row->rpayment}}</td>
</tr>
#endforeach
#endif
</table>
</form>
Here is the output I'm getting,
The problem is in your web routes. Your get request doesnt return any data. It just returns the blade. You return the data in the post request. When you access the url your are making a get request

Laravel - Get last 5 registered users attributes

I'm trying to make a dashboard and I want to display the last 5 users that are registered on the site, So the most recent 5 registered people. How can I accomplish this? I need to have access to all their attributes, for instance, Name, Lastname etc.
This is my route: Route::get('/adminpanel', 'AdminController#index')->name('adminpanel');
And this is the controller:
public function index()
{
Carbon::setLocale('nl');
$tijd = Carbon::today();
$posts = Post::count();
$users = User::count();
$replies = Reply::count();
return view('admin.dashboard', compact('posts', 'users', 'replies', 'tijd'));
}
They need to be displayed in a table like this:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Rainier</td>
<td>Laan</td>
<td>rainier.laan#home.nl</td>
</tr>
</tbody>
</table>
Thanks in advance
You can use take() to get specific number of records.
$users = User::orderBy('id', 'desc')->take(5)->get();
And on your view-
#foreach($users as $user)
{{$user->name}}
#endforeach
Hope it helps :)
This one will take 5 latest users.
$users = User::latest()->take(5)->get();

View in laravel error undefined variable

I am a newbie in laravel and php. I cannot show the results of query in laravel. I wrote this in my program.
routes.php
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController#absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return Redirect::to('books/see');
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
And error is Undefined variable: results (View: ...\absen.blade.php) I very tired with this. Help please
Instead of redirecting, you should render the view in the absen() action. When doing the redirect the data you just selected from the db is all gone.
Try this:
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books/see')->with('results', $results);
}
Also you need to check if $results exists in your view, since you also want to display it when no results are available
#if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
Attention
André Daniel is very right with his comment. Your code is prone to SQL injection. You should really take a look at Laravels ORM Eloquent or the query builder. At the very minimum, use bindings for parameters:
DB::select(DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = ?"), array($ruang));
Here's an example with the query builder (Thanks #AndréDaniel)
DB::table("book")->where("ta", $ruang)->get()
You have to make your view with your variable.
Here is what you should do :
routes.php
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController#absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books.absen')->with(array('results' => $results));
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
#if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
By the way, you should use Laravel form functions Forms & HTML

Categories