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();
Related
I have an issue with getting data of the joined table if I use query().
I did not get the data of product table, how can I solve this using query() without using Active record?
Here is my db table structure
category table
+--------------------------+
| cId | added | category |
+-----+--------+-----------+
| 1 | 1.2.20 | PC |
| 2 | 1.7.20 | electron |
+-----+--------+-----------+
product table
+--------------------------+
| id | cId | cost |
+-----+--------+-----------+
| 1 | 1 | 3000 |
| 1 | 2 | 9000 |
+-----+--------+-----------+
My Model
protected $table = 'category';
public function showProduct()
{
$sql = "SELECT
category.*, COALESCE(SUM(product.cost),0) as price
FROM category
JOIN product
ON product.cId = category.cId
GROUP BY category.cId
";
$this->db->query($sql);
return $this;
}
My Controller
public function index()
{
$result = $this->model->showProduct();
echo "<pre>";
print_r($result->asObject()->paginate(1));
//pagination
$pager = $this->model->showProduct()->pager->links();
}
Result I get
Array
(
[0] => stdClass Object
(
[cId] => 1
[added] => 1.2.20
[category] => PC
)
[1] => stdClass Object
(
[cId] => 2
[added] => 1.7.20
[category] => electron
),
)
You are requested to run this code.
SELECT category.cId,category.added,category.category,product.id,COALESCE(SUM(product.cost),0) as price
FROM category,product
WHERE category.cId=product.cId
GROUP BY category.cId;
If you are using CodeIgniter-4 then you can easily write this using Query Builder Class.
$sql="SELECT category*,product* FROM category INNER JOIN product.cid=category.id WHERE category.id";
I found solution to this by setting this table property within a function.
Model
$protected $table = array("default bd table here");
public function showProduct()
{
$this->table = array('category');
$sql = "SELECT
category.*, COALESCE(SUM(product.cost),0) as price
FROM category
JOIN product
ON product.cId = category.cId
GROUP BY category.cId
";
$this->db->query($sql);
return $this;
}
My Controller
public function index()
{
$result = $this->model->showProduct();
//pagination
$pager = $this->model->showProduct()->pager->links();
}
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 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.
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');
Database Structure is as follows:
Table: users
id | name | email | password
Table: analytics
id | user_id(fk) | article_id(fk) | revenue | date | clicks
Now I want the list of all users with the sum of their own revenue from these two Tables.
What I tried is as follows:
$users = User::select('users*', 'analytics.*', 'SUM(analytics.revenue)')
->leftJoin('analytics', 'analytics.user_id', '=', 'users.id)
->where('analytics.date', Carbon::today()->toDateString())
->get();
But it is throwing me error.
You may try this
$users = User::select('users*', 'analytics.*', DB::raw('SUM(analytics.revenue) As revenue'))
->leftJoin('analytics', 'analytics.user_id', '=', 'users.id')
->where('analytics.date', Carbon::today()->toDateString())
->get();
$users = User::select(\DB::raw('users.*, SUM(analytics.revenue) as revenue'))
->leftJoin('analytics', 'analytics.user_id', '=', 'users.id')
->groupBy('users.id')
->get();
This maybe a bit late. Maybe will help someone else.
You need to include every select columns in groupBy. In your case:
$users = User::select('users.id', 'users.name', 'users.password', 'analytics.date', 'analytics.clicks', 'SUM(analytics.revenue) as revenue')
->leftJoin('analytics', 'users.id', '=', 'analytics.user_id')
->where('analytics.date', Carbon::today()->toDateString())
->groupBy('users.id', 'users.name', 'users.password', 'analytics.date', 'analytics.clicks')
->get();