Difficulty in writing join query in laravel5.3 controller - php

I am developing a project in laravel 5.3. where I am using a users table and a points table in database. users are associated with transactions of points. these transections are recorded in points table. my points table looks like in my previous question who's link is this
-- I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their --
. Now I am fetching users' all data in a page whare I want to show the net balance of there points too. you can see the result in image.image is here
so currently I am using the following code in controller.
public function users_list()
{
$ptitle = "Website Users";
$pdesc = "";
//$total_users = User::count();
$users = User::select('*')->orderby('created_at', 'desc')->get();
return view('admin.all-users-list',
['ptitle' => $ptitle,
'pdesc' => $pdesc,
'users' => $users,
'total_users' => $this->total_users,
]);
}
And getting result in view like following
<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></td>
</tr>
<?php }?>
</tbody></table>
But I dont know how can i create a join in query builder to fetch net points for every user in list

Related

Not getting all entries of an attribute in Laravel

In my controller I've this method:
public function code($code)
{
$user = Auth::user();
$instituteCodes = DB::table('institute_codes')->where(['code' => $code, 'institute_id' => Auth::id()]);
if($instituteCodes->exists()){
$claim = DB::table('code_claims')->where('institute_code_id', $instituteCodes->value('id'))->get();
$allusers = collect($claim);
$allusers->values()->all();
$course = Course::findOrFail($instituteCodes->value('course_id'));
return view('institute.code', compact(['allusers', 'course']));
}
}
institute_codes table
id
code
course_id
1
ABC
1
2
ZXC
5
course table
id
name
1
Python
2
Laravel
I've a route which passes $code parameter to public function code() I'm getting same course data on using different codes in blade view but when I use return $course or dd($course) in controller it returns the the expected result i.e. different courses for different codes. But in blade I'm getting same courses. Any help will be appreciated.
This is happening!
Why both codes are returning same course in blade and different courses(this is what I want) on using dd or return.
Edit 1
Blade View
<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th class="long-table-row">Email</th>
<th>Code</th>
<th>Course</th>
<th>Language</th>
<th>Enrolled On</th>
<th>Progress</th>
<th>Certificate</th>
<th>Action</th>
</tr>
</thead>
#foreach ($allusers as $item)
<tr>
<td class="long-table-row">
{{ $item->user_email }}
</td>
<td class="uppercase">{{ $code }}</td>
<td>{{ $course->c_name }}</td>
<td>{{ $course->language->name }}</td>
<td>29/07/2022</td>
<td>100%</td>
<td><i class="bi bi-check-circle-fill" style="font-size: 1.2vw; color: rgb(0, 200, 90);"></i></td>
<td></td>
</tr>
#endforeach
<tbody>
</table>
</div>
I'm getting same c_name and language name for different codes.
I think you are iterating through the allusers but for course you are only picking the first course in the foreach loop of allusers.

how to view different data from same row in laravel 8

Here you can see I can store two different team names in my table
.
This is my database
I want to view them in my index.blade.php so I did this in my index.blade.php but it`s showing team 1 name everytime.
<table id="example1" class="table table-bordered table-striped table-sm">
<thead>
<tr>
<th>SL</th>
<th>Match Name</th>
<th>Match Slug</th>
<th>Team 1 Name</th>
<th>Team 2 Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->match_name }}</td>
<td>{{ $row->match_slug }}</td>
<td>{{ $row->team->team_name }}</td>
<td>{{ $row->team->team_name }}</td>
</tr>
#endforeach
</tbody>
</table>
This is my index method
public function index()
{
$data=Matchh::all();
$team=Team::all();
return view('admin.manage.matchh.index', compact('data','team'));
}
Try this apply this code
$Matchh = DB::table('Matchh')
->join('Team', 'Matchh.team_id', '=', 'Team.team_id')
->join('Team', 'Matchh.team2_id', '=', 'Team.team2_id')
->select('Matchh.matchh_name','Matchh.matchh_slug','Team.team1_name','Team.team2_name_')
->get();
you need a self join per reference table.
Try this:
$result = DB::select('SELECT
m.match_name,
m.match_slug,
t1.team_name as team1_name,
t2.team_name as team2_name
FROM matchh as m
LEFT JOIN team as t1 ON m.team_id = t1.id
LEFT JOIN team as t2 ON m.team2_id = t2.id');
return $result;

Foreach to call values from one database table with different query in laravel

I want to retrieve values ​​from one database table but separated in two adjacent tables based on different id_produk (foreign key) values.
This is code in Controller :
public function index()
{
$item_ict = Item::where('id_produk', '1');
$item_cm = Item::where('id_produk', '2');
return view('item/index', compact('item_ict', 'item_cm'));
}
Then, I call $item_ict and $item_cm in index
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($item_ict as $itemict)
#foreach ($item_cm as $itemcm)
<tr>
<td>{{ $itemict -> nama_item }}</td>
<td>{{ $itemcm -> nama_item }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
is it the correct way when i wrote that foreach? Nothing errors, but no values exited. How the way to fix it?
Or i'm thinking about call it use query in index page, but i dont know how. is it possible? how?
Combine your items into one array so that you only need to do one loop.
Also, use ->get() to actually get the items. As it stands, your code is still just the query builder.
public function index()
{
$all = [];
$item_ict = Item::where('id_produk', '1')->get();
$item_cm = Item::where('id_produk', '2')->get();
for ($i = 0; $i < max($item_ict->count(), $item_cm->count()); $i++) {
$all[] = [
isset($item_ict[$i]) ? $item_ict[$i] : null,
isset($item_cm[$i]) ? $item_cm[$i] : null,
];
}
return view('item/index', compact('all'));
}
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($all as $items)
<tr>
<td>{{ $items[0] ? $items[0]->nama_item : null }}</td>
<td>{{ $items[1] ? $items[1]->nama_item : null }}</td>
</tr>
#endforeach
</tbody>
</table>

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.

Select List From Database With Parameter

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

Categories