I have this Eloquent query:
User::select([
'users.name AS name',
'order.name AS name'
])
->leftJoin( 'orders', 'users.id', '=', 'users.order_id')
->get()->toArray();
I need to sort orders table by id before passing it to leftJoin
Related
Is there any way i can do this select query using Hibernate Specification API (criteria API)?
select * from employee order by (CASE
WHEN full_name IS NOT NULL
AND full_name <> '' THEN full_name
WHEN CONCAT(first_name, last_name) IS NOT NULL
AND CONCAT(first_name, last_name) <> '' THEN CONCAT(first_name, ' ', last_name)
END) nulls first
My specification method:
Specification { root: Root<Employee>, criteriaQuery: CriteriaQuery<*>, cb: CriteriaBuilder ->
...
criteriaQuery.orderBy(
cb.asc(
//TODO something?
)
)
...
}
Try this :
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Route> query = criteriaBuilder.createQuery(Employee.class);
List<Order> orders = new ArrayList();
Root<Employee> employeeRoot = query.from(Employee.class);
orders.add(criteriaBuilder.desc(employeeRoot .get("firstName")));
orders.add(criteriaBuilder.desc(employeeRoot .get("lastName")));
query.orderBy(orders);
I have a table called chat which is joined with users and messages (messages are joined with users), the query works fine just I am trying to add orderby with created_at (desc) but it has to be from the table messages. help me out here. Thanks in advance. Below is my query,
return new GroupDirectChat(
Chat::with(['messages.users', 'users' => function ($query) {
$query->where('id', '!=', Auth::id());
}])
->where('direct_chat', 1)
->whereHas('users', function ($query) {
$query->where('id', '=', Auth::id());
})
->get()
);
Combine a JOIN with a subquery:
$chats = Chat::select('chats.*')
->join('messages', 'messages.chat_id', 'chats.id')
->where('chats.direct_chat', 1)
->where('messages.id', function ($query) {
$query->select('id')
->from('messages')
->whereColumn('chat_id', 'chats.id')
->latest()
->limit(1);
})
->whereHas('users', function ($query) {
$query->where('id', '=', Auth::id());
})
->latest('messages.created_at')
->with(['messages.users', 'users' => function ($query) {
$query->where('id', '!=', Auth::id());
}])
->get();
Note that this query only selects chats with at least one message.
I have two collections one is order_master and the other one is product_master.
I need to fetch only those order which have the particular product name say 'tops'
Here is my my collection
Order Master
_id
o_type
product_data
->0
->product_id
->shipping_details
Product_master
_id
product_name
product_type
based on product name I have to select the order details
for Eg:
Select * from order_master om inner join product_master pm on pm._id = om.product_data.product_id where pm.product_name = 'top'
similar like this
How to write in mongo db
I have tried like this
$collection = $this->mongo_db->db->selectCollection('product_master');
$request = $collection->aggregate(
array(
'$match' => array(
'$and' => array(
array('product_name' => 'top')
)
)
),
array(
'$lookup' => array(
'from' => "order_master",
'localField' => "product_data.p_id",
'foreignField' => "_id",
'as' => "product_data"
)
)
);
Yes, you can "join" "tables" in mongodb using aggregate pipeline but your real problem is filtering documents on its inner array value. In my opinion, You need to unwind the product_data first. Here some reference on unwind https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
Basically unwind will deconstruct the array, if Order A has 5 items in product_data then unwind will create 5 copy of order A with only on one of 5 items in it.
After that, you apply lookup then filter them using lookup and do the final step which is grouping on the id to get unique order.
The query would be something like this
db.getCollection('order_master').aggregate([
{$unwind: '$product_data'},
{$lookup: {
from: 'product_master',
localField: 'product_data.product_id',
foreignField: '_id',
as: 'product_details'
}
},
{$match: {'product_details.product_name': 'tops'}},
{$group: {_id: '$_id'}}
])
I have two collections users and clients. I want to use join between this two collections. Use hasMany relations. My eloquent query as follows
$user_list = User::with('clientMaster')
->where('client_status', '=', 1)
->get()
->toArray()
User Model
public function clientMaster()
{
return $this->hasMany('App\Client', '_id', 'client_name');
}
Now, when I print $user_list output as below. This output comes from users collection
(
[0] => Array
(
[_id] => 5788bbe97ffbe04a467634e1
[name] => Abc Pqr
[email] => abc.pqr#xyz.in
[client_id] => 57876dcdc83aa906e81371f4
[client_master] => Array
(
)
)
)
Now I want use join between client and user for fetcing client name on behalf of client._id = users.client_id. I don't know how to use join in laravel eloquent. Please suggest me.
I've got a problem. I'm trying to left join two tables with Zend Framework using $select object. Unfortunatly my tables has common field 'name' and when I'm joining one with the other the results I get is that name field from table overwrites the name field from the other.
My code is something like that:
$select->joinLeft ( array ('users' => 'users' ), $this->_name . '.employee_id = users.user_id', array ('*' ) );
How I can join tables and avoid this issue?
Use table aliases as you would in any normal sql query!
With Zend_Db aliases are written like this:
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array() ); // empty list of columns
The non-zend query would look like this:
SELECT p.product_id, p.product_name
FROM products AS p
JOIN line_items AS l ON p.product_id = l.product_id;
I guess it's bit late but to get all fields from two tables you must alias all the fields
$select = $db->select()
->from(array('u' => 'users'),
array('u.id'=>'u.id','u.employee_id'=>'u.employee_id','u.name'=>'u.name'))
->joinLeft(array('e' => 'employees'),
'e.id = u.employee_id',
array('e.id'=>'e.id','e.name'=>'e.name') );
And your array would look like:
array(
0=>array(
'u.id'=>'1',
'u.employee_id'=>'1',
'u.name'=>'John Doe',
'e.id'=>'1',
'e.name'=>'Worker'
),
1=>array(
...
));