I have two tables:
users
id nickname
1 Mike
2 John
report
id user_id target_id
1 1 2
2 2 1
MyController
public function violationView(User $user)
{
$vio = Report::all(); //this should be query, but I don't know how.
return view('admin.violation.vio_listpage')
->with('vio', $vio);
}
When I displayed the $vio into my blade, it only displays the user_id and target_id. These should be the nickname of the users and not ID number.
Expected Output
ID user_name(user_id) username(target_id)
1 Mike John
2 John Mike
Does anybody know how to display the nickname instead if user_id and target_id?
To get user name by user id you need to add join with in your query. Below is the example for that:-
$vio = Violation::join('users', function($q) {
$q->on('users.id', '=', 'report.user_id');
})
->join('users as target', function($q) {
$q->on('target.id', '=', 'report.target_id');
})
->selectRaw("report.*, users.nickname as user_name, target.nickname as target_user_name")
->get();
return view('admin.violation.vio_listpage')
->with('vio', $vio);
Try this. I think this will help you to achieve your goal.
Related
I have three tables like this:
user
id | name | department_id
user_verify
id | code | user_id | department_id
department
id | name
I want to query on table user_verify and join with other tables to get username and department name.
I tried this in my model:
class ListUserVerify extends Model
{
protected $table = 'user_verify';
public function getUserVerify()
{
return $this->select("{$this->table}.code",'user.fullname','department.name')
->join('user', 'user.id', '=', "{$this->table}.user_id")
->join('department', 'department.id', '=', "{$this->table}.department_id")
->get();
}
}
But this function return null, I don't know what's wrong with my query.
Can you show me what's wrong I did?
Thank you very much!
Try it.
return \DB::table("{$this->table}")->select("{$this->table}.code",'user.fullname','department.name')
->join('user', 'user.id', '=', "{$this->table}.user_id")
->join('department', 'department.id', '=', "{$this->table}.department_id")
->get();
Try this
return DB::table('user_verify')->leftjoin('user','user.id','=','user_verify.user_id')
->leftjoin('department','department.id','=','user_verify.department_id')
->select('user.name as username','department.name as departmentname')
->get();
I have 2 tables:
user: id, name
score: id, user_id, point
Now I want to get 5 users name who have the best score but seem like it was wrong.
Here's my code:
public function getTop(){
$top = DB::table('score')
->select('user_id', DB::raw('COUNT(point)'))
->groupBy('user_id')
->orderBy(DB::raw('COUNT(point)'), 'DESC')
->take(5)
->get();
return view('home',compact('top'));
}
In your case Database query makes more senses.
Database query to get top 5 user_id with total score.
Join users table with that result.
$topResult = DB::table('users'
)->join(DB::raw('(SELECT user_id, SUM(point) as score FROM score GROUP BY user_id ORDER BY SUM(point) LIMIT 5) as top_scorer'), function($join) {
$join->on('top_scorer.user_id', '=','users.id');
})
->select(['users.*', 'top_scorer.score']) //Select fields you need.
->orderBy('top_scorer.score')
->get();
Try this.
DB::table('users')
->select(['users.id', DB::raw('MAX(sc.point) AS score')])
->join('score AS sc', 'sc.id', '=', 'users.id')
->groupBy('users.id')
->take(5)->get();
I have these 2 tables:
**users**
id | name | email | ...
-----------
**rooms**
user_id | title | status | ...
---------------------------
How can i select all users's email with rooms where rooms status is Listed in Laravel 5 ?
What you have to do, you have to use "with" and "has"
$user = User::with(['room'])->whereHas('room,function($query){
$q->where('status',1);
})->get();
create model of User and Room & a relation in user name room.
May be the above one solve your problem
If you have already define a relation between App\User and App\Room Models like this.
class User {
/* other codes in your model goes here */
public function rooms(){
return $this->hasMany(App\Room::class);
}
}
class Room {
/* other codes in your model goes here */
public function user(){
return $this->belongsTo(App\User::class);
}
}
You can retrieve all users's email with rooms where rooms status is Listed like this
$users= \App\User::with(['rooms' => function($query){
$query->where('status', 'listed');
}])->pluck('email');
I got two tables. I want to get all users and their details who are active/banned.
What is laravel eloquent equivalent to this query.
'select * from client_details where user_id IN (select user_id from client_logins where is_active='.$active.')'
Table: client_logins Model clientLogin
+---------+---------+--------------+-------------+-----------+
| id | user_id | username | password | is_active |
+---------+---------+--------------+-------------+-----------+
Table: client_details Model clientDetail
+---------+------+-------+------------------+
| user_id | name | email | mobile | address|
+---------+------+-------+------------------+
Using query builder you could write it as
$clients=DB::table('client_details as c')
->select('c.*')
->join('client_logins as l', 'c.user_id', '=', 'l.user_id')
->where('l.is_active', '=', $active)
->get()
;
Or if have defined has relation for ClientDetails model you can use whereHas filter
class ClientDetails extends Model
{
public function client_logins()
{
return $this->hasMany('App\ClientLogins', 'user_id', 'user_id');
}
}
$clients = ClientDetails::whereHas('client_logins', function ($query) use ($active) {
$query->where('is_active', '=', $active);
})->get();
My tables
Category
id_category
name
Post
id_post
category_id
title
My query:
Post::find()
->select('post.*, c.name AS catname')
->leftJoin('category c', 'c.id_category = category_id')
->all();
The output just shown the table fields Post, is not the field catname.
1) Define a relation in Post model named 'category', so:
public function getCategory() {
return $this->hasOne(Category::className(), ['id_category' => 'category_id']);
}
2) Then when you query the posts, use 'with' if you need to get category name for each post:
$posts = Post::find()
->with('category')
->all();
3) You can access to category name with:
$post->category->name