How can I make that query in Laravel Eloquent or Fluent?
DUELS
|id|userId1 | UserId2
-------------------------
|1| 1 | 4 |
|2| 3 | 2 |
|3| 2 | 1 |
-------------------------
USERS
|id| firstName |
----------------
|1| Bob |
|2| Hans |
|3| Jerome |
|3| Katy |
----------------
Query: get the names of the users in the duels where the user with $userId is participating:
SELECT u1.firstName AS user1FirstName, u2.firstName AS user2FirstName
FROM duels
LEFT JOIN users AS u1
ON userId1 = u1.id
LEFT JOIN users AS u2
ON userId2 = u2.id
WHERE userId1 = $userId || userId2 = $userId
Using Laravel's Fluent, to perform that query you need to do the following:
DB::table('users')->join('duels', function($join) use ($userId){
$join->on('users.id', '=', 'duels.userId1')
->orOn('users.id', '=', 'duels.userId2')
->where('users.id', '=', $userId);
})->get();
Using Laravel's Eloquent, however, you may consider the user as either first player, or as second player.
In your User model, define the above two relationships as such:
class User extends Eloquent{
/**
* ...
*/
public function duelsAsFirstPlayer(){
return $this->belongsToMany( 'App\User', 'duels', 'user_id_1', 'user_id_2');
}
public function duelsAsSecondPlayer(){
return $this->belongsToMany( 'App\User', 'duels', 'user_id_2', 'user_id_1');
}
}
Now, in order to get all the users the user with $userId has duels with, query the User model as such:
as first player:
$users = App\User::find($userId)->duelsAsFirstPlayer()->get();
or as second player:
$users = App\User::find($userId)->duelsAsSecondPlayer()->get();
Related
I've found a couple of questions online similar to mine, but I can't seem to find a working answer.
I have 2 tables
USER
ID | FIRSTNAME | EMAIL_ADDRESS
1 | Joe Bloggs | Joe#bloggs.com
STATUS
ID | USER_ID | STATUS | DATE
1 | 1 | 'In' | 2018-06-04 09:01:00
2 | 1 | 'Out' | 2018-06-04 09:00:00
I need to be able to Join the 2 tables together but only get the most recent status column by date, like this
ID | FIRSTNAME | EMAIL_ADDRESS | STATUS_ID | STATUS | DATE
1 | Joe Bloggs | Joe#bloggs.com | 1 | 'In' | 2018-06-04 09:01:00
I need to be able to run extra query builder arguments like where, because the user has the ability to pass in filters and search parameters if they require, so to be able to use the status table columns in my query builder, i'm doing a join like this
$users = Users::join('status', 'status.user_id', '=', 'user.id')->distinct('user.id');
Which then allows me to pass in any search parameters if I need them
if(!empty($request->search)){
$param = $request->search;
$users = $users->where(function($query) use ($param){
$query->where('users.firstname', 'like', '%'.$param.'%')
->orWhere('users.email_address', 'like', '%'.$param.'%');
});
}
if(!empty($request->dateFrom)){
if(!empty($request->dateTo)){
$users = $users->whereRaw('DATE(status.date) BETWEEN ? AND ?', [$request->dateFrom, $request->dateTo]);
} else {
$users = $users->whereRaw('DATE(status.date) BETWEEN ? AND DATE(NOW())', [$request->dateFrom]);
}
}
Then run my get() at the end
if($method == 'paginate'){
$users = $users->paginate(10);
} else {
$users = $users->get();
}
This returns the following
ID | FIRSTNAME | EMAIL_ADDRESS | STATUS_ID | STATUS | DATE
1 | Joe Bloggs | Joe#bloggs.com | 1 | 'In' | 2018-06-04 09:01:00
2 | Joe Bloggs | Joe#bloggs.com | 1 | 'Out' | 2018-06-04 09:00:00
I need to be able to use the foreign table columns as arguments in my Where functions, but I need to only return 1 row per user. How do I run a join, but only return 1 row for each of my users?
To get latest record for each user you can use a self join for status table
select u.*,s.*
from user u
join status s on u.id = s.user_id
left join status s1 on s.user_id = s1.user_id
and s.date < s1.date
where s1.user_id is null
Using query builder you might rewrite it as
DB::table('user as u')
->select('u.*', 's.*')
->join('status as s', 'u.id ', '=', 's.user_id')
->leftJoin('status as s1', function ($join) {
$join->on('s.user_id', '=', 's1.user_id')
->whereRaw(DB::raw('s.date < s1.date'));
})
->whereNull('s1.user_id')
->get();
Laravel Eloquent select all rows with max created_at
Laravel - Get the last entry of each UID type
Laravel Eloquent group by most recent record
Since you are using Laravel lets make the solution a bit more understandable and easier:
First Lets create the models for each table:
class User extends Model
{
public $timestamps = false;
protected $fillable = [
'FIRSTNAME',
'EMAIL_ADDRESS'
];
public function status(){
return $this->hasMany(\App\Status::class);
}
}
Now lets create the Status Model:
class Status extends Model
{
public $timestamps = false;
protected $casts = [
'USER_ID' => 'int',
];
protected $fillable = [
'USER_ID',
'STATUS',
'DATE'
];
public function user()
{
return $this->belongsTo(\App\User::class);
}
}
Now you can use Eloquents like this:
$result=User::whereHas('Status', function($q){
$q->where('date', $current_date);
})->distinct('id')->get();
Hope it Helps!
I am dealing with Eloquent ORM collections and query builders. I am trying to figure out how to join and use "where" in a collection, like in query builder.
For example, I have the following tables:
Users:
ID | Name | Last name
-------------------------
1 | Martin | Fernandez
2 | Some | User
Persons:
ID | Nick | User_ID | Active
----------------------------------
1 | Tincho | 1 | 1
Companies:
ID | Name | User_ID | Active
----------------------------------
1 | Maramal| 1 | 0
2 | Some | 2 | 1
This is an example, the tables I am working on have more than 30 columns each one. I want to select all the user that are active.
Usually I would do a query like:
SELECT *
FROM users
LEFT JOIN persons ON users.id = persons.user_id
LEFT join companies ON users.id = companies.user_id
WHERE persons.active = 1
OR companies.active = 1
That can be translated to Laravel Query Builder like:
DB::table('users')
->leftJoin('persons', 'users.id', '=', 'persons.user_id')
->leftJoin('companies', 'users.id', '=', 'companies.user_id')
->where('persons.active', 1)
->orWhere('companies.active', 1)
->get();
But what I want to use is a Laravel Eloquent ORM Collection, until now I am doing the following:
$users= User::orderBy('id',' desc')->get();
foreach($users as $k => $user) {
if($user->company && !$user->company->active || $user->person && !$user->person->active) {
unset($users[$k]);
}
... and here a lot of validations and unsets ...
}
But I know that at this point, I already grabbed all the users instead those who are active.
How would I achieve what I did with query builder within a collection? Thanks in advance.
This should do it:
$users = User::whereHas('companies', function($q) {
$q->where('active', true);
})->orWhereHas('persons', function($q) {
$q->where('active', true);
})->with(['companies', 'persons'])->orderBy('id', 'desc')->get();
I have 2 tables in my Laravel application namely customers and stores. Customers can belong to many stores and stores can have many customers. There's a pivot table between them to store that relation.
Question is, how do I extract the list of customers for a given store using Eloquent? Is it possible? I am currently able to extract that using Laravel's Query Builder. Here is my code:
| customers | stores | customer_store |
-------------------------------------------
| id | id | customer_id |
| name | name | store_id |
| created_at| created_at | created_at |
| updated_at| updated_at | updated_at |
Customer Model:
public function stores(){
return $this->belongsToMany(Store::class)
->withPivot('customer_store', 'store_id')
->withTimestamps();
}
Store Model:
public function customers(){
return $this->belongsToMany(Customer::class)
->withPivot('customer_store', 'customer_id')
->withTimestamps();
}
DB Query (using Query Builder):
$customer = DB::select(SELECT customers.id, customers.name, customers.phone, customers.email, customers.location FROM customers LEFT JOIN customer_store on customers.id = customer_store.customer_id WHERE customer_store.store_id = $storeID);
Try this one:
public function result(Request $request) {
$storeId = $request->get('storeId');
$customers = Customer::whereHas('stores', function($query) use($storeId) {
$query->where('stores.id', $storeId);
})->get();
}
Try executing this...
$result = Customer::with('stores')->get();
Hope this helps.
To know more about Eloquent Relationship refer: https://laravel.com/docs/5.1/eloquent-relationships
Try below:
Here Customers is your model and $storeID is your store id. $storeID is outside of the scope of your callback. So you must use the use statement to pass them.
Customers::leftJoin('customer_store', function($join) use($storeID){
$join->on('customers.id', '=', 'customer_store.customer_id')
->where('customer_store.store_id','=', $storeID);
})
->whereNotNull('customer_store.store_id')//Not Null Filter
->get();
Hope this help you!
i have two tables 'users' and 'projects'
Table users
-----------------------------------
user_id | name | email | password |
-----------------------------------
1 |name1 |email1 | password1|
-----------------------------------
2 |name2 |email2 | password2|
Table projects
project_id |user_id | name |
--------------------------------------
1 | 1 | project_name1 |
-----------------------------------
2 | 1 | project_name2 |
There is no relation between these two tables but user_id in projects are the id from user table.
Normal Query
SELECT a.user_id,a.name AS username,b.name AS project_name FROM users a LEFT JOIN projects b ON a.user_id=b.user_id;
I want the same query execution in laravel eloquent.
Have searched in google got some idea about "with" function but not able to get the output.
Thanks.
This is the way you should use Eloquent:
create User.php
<?php
class User extends Eloquent
{
public function projects()
{
return $this->hasMany('Project');
}
}
create Project.php
<?php
class Project extends Eloquent
{
}
On controller, write this to get all users, with related projects inserted inside each users:
$users = User::with('projects')->all();
foreach($users as $user)
{
$project_of_this_user = $user->projects;
}
You can simply do as following,
DB::table('users')
->join('projects =', 'users.id', '=', 'posts.user_id')
->select('*')
->get();
For more details you can refer laravel Docs
This should be what you need:
DB::table('users')
->join('projects', 'projects.user_id', '=', 'users.user_id')
->select('users.user_id', 'users.name as username', 'projects.name as project_name')
->get();
I think, this one helps you out
DB::table('users')
->leftJoin('projects', 'users.user_id', '=', 'projects.user_id')
->select('users.user_id', 'users.name as username', 'projects.name as project_name')
->get();
I'm not so good at making queries using Laravel Eloquent. I've two tables
stories table
------------------------------------------------------------------
id | title | body | author_id |...
------------------------------------------------------------------
1 | Story 1 | Once a.. | 2
2 | Story 2 | There is | 4
3 | Something | You are. | 2
activities table
------------------------------------------------------------------
id | story_id | liker_id |...
------------------------------------------------------------------
1 | 2 | 2
Here author_id & liker_id are actually user_id. I want to get the Stories authored and liked by a specific user to display these stories in his profile.
I want to use the Eloquent ORM. I tried something like this using query builder
$stories = DB::table('stories')
->join('activities', function($join)
{
$join->on('stories.author_id', '=', 'activities.liker_id')
})
->where('stories.author_id', $author_id)
->get();
return $stories;
I can get story_id for a specific liker_id by join but couldn't get the details from stories table using story_id in a Single query.
Here is simple method with query builder to get Stories authored and liked by a specific user
$author_id = 1;
$stories = DB::table('stories')
->join('activities', 'stories.author_id', '=', DB::raw('activities.liker_id AND stories.id = activities.story_id'))
->Where('stories.author_id', $author_id)
->get();
//select * from `stories` inner join `activities` on `stories`.`author_id` = activities.liker_id AND stories.id = activities.story_id where `stories`.`author_id` = 1"
with Eloquent you can do as following create 2 model file
1. Story model (Story.php)
2. Activity Model (Activity.php)
Story.php
class Story extends Eloquent {
public function activities()
{
return $this->hasMany('Activity');
}
}
Activity.php
class Activity extends Eloquent {
public function story()
{
return $this->belongsTo('Story');
}
}
than you can write function within Story to get data as your need
$stories = Story::with('activities')
->where(DB::raw('stories.id = activities.story_id'))
->Where('stories.author_id', $author_id)
->get();
// haven't tested with eloquent but it should work