I am bit struggling to convert below given SQL query to eloquent.
Here is an example of what I am trying to do -
SELECT u.wallet_address AS wallet_address, p.id AS project_id
FROM users u, projects p, investment_investor i
WHERE u.id = i.user_id AND
p.id=i.project_id AND
u.wallet_address <> '' AND
p.wallet_address <> '' AND
p.contract_address <> ''
GROUP BY u.id, p.id;
The below SQL query gives me the expected output, which I am able to build with the Laravel DB query builder. But want to standardise it to Laravel framework.
Your help is much appreciated!
First thing you need to have to be able to write queries in the laravel way is something called Model. Example of a model would be like so
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
//
}
You can read more about it here
Once you create it, you can start writing queries. For example
$results = Users::selectRaw('users.wallet_address as `wallet_address`,'.
'id as `project_id`')
->get();
You can read more about joins and other things here
$result=DB::table('investment_investor')
->select('users.wallet_address as wallet_address','projects.id AS project_id')
->join('users','users.id','=','investment_investor.user_id')
->join('projects','projects.id','=','investment_investor.project_id')
->where('users.wallet_address','!=','')
->where('projects.wallet_address','!=','')
->where('projects.contract_address','!=,'')
->groupBy('users.id', 'projects.id')
->get();
Related
I have a problem with a query that I am doing in Laravel 5.8 using Eloquent, that I really don't know what happens.
I will explain:
My Eloquent query is this:
$naviones=DB::connection('naviones')->table('ps_customer')
->select('ps_orders.id_order','ps_customer.firstname','ps_customer.lastname','ps_customer.email','ps_address.address1',
'ps_address.postcode','ps_address.city','ps_address.phone_mobile','ps_orders.id_order','ps_orders.reference',
'ps_order_detail.product_name','ps_order_detail.product_quantity','ps_shop.name as tienda','ps_carrier.name as transportista')
->join('ps_address','ps_customer.id_customer','=','ps_address.id_customer')
->join('ps_orders', function($join){
$join->on('ps_address.id_customer','=','ps_orders.id_customer')
->where('ps_address.id_address','=','ps_orders.id_address_delivery');
})
->join('ps_order_detail','ps_orders.id_order','=','ps_order_detail.id_order')
->join('ps_shop','ps_order_detail.id_shop','=','ps_shop.id_shop')
->join('ps_carrier','ps_orders.id_carrier','=','ps_carrier.id_carrier')
->where('ps_orders.id_order','=',11389)
->get();
As you can see, I am making this query to a database, which is not necessarily the one used by the application. That eloquent query translates to this:
SELECT
`ps_orders`.`id_order`,
`ps_customer`.`firstname`,
`ps_customer`.`lastname`,
`ps_customer`.`email`,
`ps_address`.`address1`,
`ps_address`.`postcode`,
`ps_address`.`city`,
`ps_address`.`phone_mobile`,
`ps_orders`.`id_order`,
`ps_orders`.`reference`,
`ps_order_detail`.`product_name`,
`ps_order_detail`.`product_quantity`,
`ps_shop`.`name` AS `tienda`,
`ps_carrier`.`name` AS `transportista`
FROM
`ps_customer`
INNER JOIN `ps_address` ON `ps_customer`.`id_customer` =
`ps_address`.`id_customer`
INNER JOIN `ps_orders` ON `ps_address`.`id_customer` =
`ps_orders`.`id_customer`
AND `ps_address`.`id_address` = ps_orders.id_address_delivery
INNER JOIN `ps_order_detail` ON `ps_orders`.`id_order` =
`ps_order_detail`.`id_order`
INNER JOIN `ps_shop` ON `ps_order_detail`.`id_shop` =
`ps_shop`.`id_shop`
INNER JOIN `ps_carrier` ON `ps_orders`.`id_carrier` =
`ps_carrier`.`id_carrier`
WHERE
`ps_orders`.`id_order` = 11389
Well, the point is that if I take that query and execute it in Mysql, it gives me the information that I am requiring, but, in eloquent the collection, it is empty. That is what drives me crazy that the collection is empty. That query is being delivered to me by eloquent, how am I ensuring that it is delivered by eloquent? I generated an error on purpose in one of the fields for Laravel to inform me and show me the complete query to examine it and that is the query:
At the end of the red circle I show the error created on purpose to see the complete query and see if it is well armed.
I do not understand what is happening, I do not know what to do, I have two days and I have no idea of what is happening.
I hope you can give me some idea, thanks.
If you remove this:
->join('ps_orders', function($join){
$join->on('ps_address.id_customer','=','ps_orders.id_customer')
->where('ps_address.id_address','=','ps_orders.id_address_delivery');
})
is there any result? maybe the problim in this function.
also, can you change this:
DB::connection('naviones')->table('ps_customer')
to
DB::table('ps_customer')
How am using laravel 5.4,
to write sql query to get data from multiple tables for below query
select l.party_id,l.container_id,p.party_name from
`tbl_container_lease` l, `tbl_partys` p
where l.`party_id` = p.`id` and l.`user_id` = 5
Now am using this
Containerlease::whereHas('getpartys',function($q){})
->where('user_id','=',$user_id)
->get();
but it is getting too confuse to use
is there any better alternative to use this query by using model..
You would have to join the tables to do this.
Containerlease::join('tbl_partys', 'tbl_partys.id', '=', 'tbl_container_lease.party_id')
->where('tbl_container_lease.user_id', 5)
->select('tbl_partys.*', 'tbl_container_lease.*');
->get();
However, an even better way would be to create relations if you are using Eloquent. The documentation for this can be found here: https://laravel.com/docs/5.4/eloquent-relationships
It would be better, since you're in laravel after all, to make use of laravel relationships.
Make sure you have a party model:
php artisan make:model Party
Assuming one lease has one party, In your container lease model add:
public function party(){
return $this->hasOne('App\Party', 'id', 'party_id');
}
Then access your model
ContainerLease::with('party')->where('user_id', '=', 5)->get();
For example:
Query
SELECT book.\*,author.* FROM book
INNER JOIN author ON author.id = book.author_id
WHERE book.id=1
Get model
$modelBook = Book::find()->innerJoin('author','author.id = book.author_id')->where(['book.id'=>1])->one();
$modelAuthor = Author::findOne(['id'=>$modelBook->author_id]);
The problem:
How can I get 2 activerecord model Book and Author with just only one mysql execute?
I know that we can use with() function, but it spend another query SELECT ...IN(...) to get second model, although we have sufficient data from join query
Is there a more effective solution ?
ActiveRecord are for model extendinng the ActiveRecord classe
You seems are using a query and not a "model" so i suggest you of use activeDataProvider.
You can refer the models managed by dataProvider using getModels() function
$provider = new SqlDataProvider([
......
]);
// returns an array of data rows
$models = $provider->getModels();
see this guide and reference for detail
http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html
http://www.yiiframework.com/doc-2.0/yii-data-sqldataprovider.html
Try this :
With approach is not slower in case of performance.
$modelBook = Book::find()->with('YOUR_RELATION_NAME_HERE')->findByPK(id);
I'm quite a beginner at Symfony 2 and Doctrine 2. I have two models a Blog post and Comment. They are related to each other by blog_id FK inside comment table.
I just want to create a simple method that takes a blog id, Retrieves that blog and related comments. Instead of doing another query in lazy-loading the related comments.
Here's what i tried :
<?php
namespace Blogger\BlogBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class BlogRepository extends EntityRepository
{
public function getBlogWithComments($id)
{
$query = $this->getEntityManager()->createQuery('
SELECT b, c
FROM BloggerBlogBundle:Blog b
WHERE b.id = :id
JOIN b.id c
WHERE b.id = c.blog');
$query->setParameter("id", $id);
return $query->getResult();
}
}
I'm really a beginner at Doctrine 2. I usually use Query Builder, But i have no idea how to make a join using it. So Tried it in DQL, Couldn't figure it out either.
It gives me this syntax error whenever i try to call getBlogWithComments method:
[Syntax Error] line 0, col 80: Error: Expected end of string, got
'JOIN'
All what i want to know is that, How to write JOIN statements using Query Builder and DQL ? Knowing that documentation is not that helpful unfortunately.
A JOIN with DQL is done by referencing the relationship between the entities and it only needs to go one way. What I mean is b.comment c is all that is needed, there is no need for referencing c.blog.
public function getBlogWithComments($id)
{
return $this->getEntityManager()
->createQuery('
SELECT b, c
FROM BloggerBlogBundle:Blog b
JOIN b.comment c
WHERE b.id = :id
')
->setParameter("id", $id)
->getResult();
}
I have read the documentation but I can't quite figure out how to run the following query in Laravel 4
SELECT
COUNT(*)
FROM
acl a,
routes r
WHERE
(a.user_id = 1 OR
a.group_id IN(SELECT group_id FROM user_group_junction WHERE user_id = 1)) AND
r.route = 'protected' AND
a.routes_id = r.id;
So how would I run the query in Laravel 4 using eloquent?
Yes each table has a model and relationships are defined
Based on my selected answer the following is what I came up with (And works)
Acls::join('routes','routes.id','=','acl.routes_id')
->where('routes.route','=','protected')
->Where(function($in_parenthesis) use($user_id){
$in_parenthesis->whereIn('acl.group_id',function($where_in) use($user_id){
$where_in->select('group_id')
->from('user_group_junction')
->where('user_id','=',$user_id);
})
->orWhere('acl.user_id','=',$user_id);
})
->count();
Methods called on Eloquent models pass through to the Illuminate\Database\Eloquent\Builder class, which itself extends from the Illuminate\Database\Query\Builder class. This means that all the things you can do with the query builder, you can also do with Eloquent models. The exception being that you don't need to define the table.
So for example if you wanted to perform a join and a where like you've done above, you would just do:
$query = Acl::join('routes', 'acl.routes_id', '=', 'routes.id')
->where('routes.route', '=', 'protected');
$results = $query->get();
Obviously this isn't your whole query, but you can figure out the rest.