Select List From Database With Parameter - php

Hi there i have some problem getting list from database using laravel 4.1
I have session that stored some value about companyid. Then i want to print the list to table based on the companyid itself. Let say i can access the session using {{ Session::get('name')}}
This is my controller index()
public function index($cid) {
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}
then i want to print the data to table in view
#if($pengguna->count())
<table class="table table-striped table-bordered" id="table_pengguna_list">
<thead>
<tr>
<th>No</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
<th>Last Login</th>
<th class="td-actions">Action</th>
</tr>
</thead>
<tbody>
#foreach ($pengguna as $pgn)
<tr>
<td>{{ $index++ }}</td>
<td>{{ $pgn->firstname }}</td>
<td>{{ $pgn->lastname }}</td>
<td>{{ $pgn->username }}</td>
<td>{{ $pgn->email }}</td>
<td>{{ $pgn->level }}</td>
<td>{{ $pgn->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
....
how to pass the parameter from session to the controller then? so basically pass the {{ Session::get('name')}} to $cid in index controller.
thank you.

hi there i have found the solution, just place refactor the index controller to;
public function index() {
$cid = Session::get('name');
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}

Related

Undefined variable $consultation [duplicate]

This question already has answers here:
Variable undefined error in laravel blade view
(4 answers)
Closed last month.
I want display my data to my blade file but it show this error
Undefined variable $consultation
This is my controller
public function p_consultation()
{
$consultation = Consultation::all();
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
}
This is my Route
Route::get('dashboard/admin_panel/p_consultation', [PenconsultationController::class, 'Consultation'])
And this is my Blade File
<tr>
<th>#</th>
<th>Item Name</th>
<th>Dscriptiom</th>
<th>Material</th>
<th>Quantity</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
</thead>
<tbody>
#foreach($consultation as $consultation)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $consultation->item_name }}</td>
<td>{{ $consultation->description }}</td>
<td>{{ $consultation->material }}</td>
<td>{{ $consultation->quantity }}</td>
<td>{{ $consultation->start_date }}</td>
<td>{{ $consultation->end_date }}</td>
</tr>
#endforeach
change from
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
to
return view ('dashboard/admin_panel/p_consultation', ['consultation' => $consultation]);
If you're passing data to the controller using with(), you need to bind it with an accessor.
->with('consultation', $consultation)
return view('dashboard/admin_panel/p_consultation')->with('consultation', $consultation);
You can use compact() as well
Example.
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');

Class "App\Http\Controllers\User" not found [duplicate]

This question already has answers here:
Class "App\Http\Controllers\Auth\User" not found
(2 answers)
Closed 1 year ago.
So I was fetching data from my database to print in a table however, it says that
Class "App\Http\Controllers\User" not found.
Here is the controller and here is how I will print the data
public function directory()
{
$dashboardTitle = "Directory";
$isCurrent = "Directory";
$users = User::all();
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent,
'users' => $users
]);
}
table to print at
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> Picture </th>
<th>Name</th>
<th>Email</th>
<th>User type</th>
<th>Birthdate</th>
<th>Contact number</th>
<th>Created time</th>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->profile_image }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->user_type }}</td>
<td>{{ $user->bdate }}</td>
<td>{{ $user->contact_no}}</td>
<td>{{ $user->created_at }}</td>
</tr>
#endforeach
</thead>
<tfoot>
</tbody>
</table>
At the top off your controller add
Laravel 8+
use App\Models\User;
Laravel <=7
use App\User;

Database sql server with results but view is empty

something really strange is happening. So I have a table named Lactinfo_News with 1 row. I've created also a view called LACTINFO_VW_LatestNews which have "SELECT * FROM Lactinfo_News" and is returning the same 1 row.
I'm using Eloquent and In my news Manager I have,
public function GetLatestNews($rowsPerPage) {
$list = DB::table('LACTINFO_VW_LatestNews')
->orderBy('RegistedDate', 'DESC')
->paginate($rowsPerPage);
return $list;
}
where $rowsPerPage = 30.
In my controller I have,
// >> current page
$page = '1';
if (! empty ( $request->query ( 'page' ) )) {
$page = $request->query ( 'page' );
}
// >> search
$nM = new NewsManager();
$list = $nM->GetLatestNews($page, $this->nbOfRowsPage);
return view ('admin.news.index', [
'results' => compact($list),
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
}
And in my view,
<table id="news-results" class="hover responsive" style="margin-top: 20px;">
<thead>
<tr>
<th scope="column">Título</th>
<th scope="column">Descrição</th>
<th scope="column">Data Início</th>
<th scope="column">Ficheiro</th>
<th scope="column">Registada em</th>
<th scope="column">Criada por</th>
</tr>
</thead>
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->title }}</td>
<td>{{ $r->description }}</td>
<td>{{ Carbon\Carbon::parse($r->startDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ $r->registedDate }}</td>
<td>{{ $r->createdBy }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>
</table>
If I dump the $results var is empty..
What's happening? This is really strange :S I don't think I need to execute any artisan command because I've add a new function to my manager but I'm stuck with this problem for a few days...
You are using compact on an object of type LengthAwarePaginator, which is kind of weird IMHO.
Why don't you pass $list in your view?
My problem was in the Controller and in my View,
So I've changed my controller return to
return view ('admin.news.index', [
'results' => $list,
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
And in my view I wasnt calling in a wrong way my parameters...
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->Title }}</td>
<td>{{ $r->Description }}</td>
<td>{{ Carbon\Carbon::parse($r->StartDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ Carbon\Carbon::parse($r->RegistedDate)->format('d/m/Y') }}</td>
<td>{{ $r->refCredential }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>

How to us timestamp in query builder in laravel 5.3 to tack users registered in last 5 days

I want to list down the users who are registered in last 5 day.. To list down all the users 1 use following query..
$users = User::select('*')->orderby('created_at', 'desc')->get();
and fetch it in view like this.
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td>{{ \App\Points::getUserPoints($u->id) }}</td>
</tr>
<?php }?>
</tbody></table>
but what will be the query to fetch users who have registered in last 5 days..?? any one please tell me a good query..
You can use Carbon (docs) to do this:
$now = Carbon::now();
$users = User::where('created_at', '>=', $now->subDays(5))->get();
Make sure to "use Carbon\Carbon;" at the top of the file.

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories