From Laravel Query Builder to Eloquent with request: where()->orWhere()->get() - eloquent

Is it possible to write this query with Eloquent Builder ?
$transactions = Transaction::where('client_id', $this->id)
->orWhere('supplier_id', $this->id)
->get();

Related

Query using query builder to make an inner join and select with condition in typeORM

I am new to query builder. I need to apply a query. I did it on mysql and it's working fine. But I couldn't make where condition in the query.
This is my SQL query:
select users_roles.user_id
from users_roles
inner join roles on roles.id = users_roles.role_id
where roles.role = 'ADMIN_ROLE'
or roles.role = 'SUPER_ADMIN_ROLE'
I tried to do this using query builder and did this without using any where condition. How can I filter according to that mysql code? Here I am adding my query builder query:
const query = this.userRoleRepository.createQueryBuilder('usersRoles');
const users = await query
.innerJoinAndSelect('usersRoles.user', 'user')
.getMany();
Actually it's returning whole user list. But I need only specified two type of user. That should be filtered with where condition. which is in mysql query.
const users = await query
.select(userRoles.user_id)
.innerJoin('userRoles.roles', 'roles', 'roles.id = users_roles.role_id')
.where("roles.role = :adminRole OR roles.role =: superRole", { adminRole: 'ADMIN_ROLE', superRole: 'SUPER_ADMIN_ROLE' })
.getMany();
I hope this works.

MongoDb Laravel where clause

In MongoDb i have documents with a field named "common_id". I just want to fetch all documents of a particular common_id.
This is how the common_id field looks like.
"common_id" : ObjectId("5911af8209ed4456d069b1d1"),
I have tried this,
$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', '=', 'true')
->get();
this,
$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', '=', 'ObjectId("5911af8209ed4456d069b1d1")')
->get();
and this,
MasterAffiliateProductMappingMongo::where('common_id', '=', ObjectId("5911af8209ed4456d069b1d1"))
->get();
also tried this,
$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', new MongoDB\BSON\ObjectID('5911af8209ed4456d069b1d1'));
I think the problem is that - ObjectId("5911af8209ed4456d069b1d1") is not a string thats why the above codes are not working. Anyway am not sure.
But nothing works. How can i do this.
::where('common_id', '5911af8209ed4456d069b1d1')->get()
should work if you are using Jenssegers Laravel MongoDB.
Checks if it string and string is ctype_xdigit then converts as object in the query builder

Laravel 5.3 Eloquent Relationship 1-1 Query

Hi guys how could I query a data from my database with one-to-one relationship on eloquent model?
I want to query the menus with specific category id.
For example I only want to query the meals with "Breakfast Category".
Menu Model:
public function menucat()
{
return $this->hasOne('App\MenuCat', 'menu_cat_id', 'id');
}
Menu_Cat Model
public function menus()
{
return $this->belongsTo('App\Menu', 'menu_cat_id', 'menu_cat_id');
}
Database Table:
menus Table
id | menu_cat_id | menuName
menu_cat Table
menu_cat_id | menuCatName
I find it easy using the Query builder but I would like to use the eloquent to query out the data I need.
Thanks in Advance!
in the documentation you have that
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
you need to inverse menu_cat_id and id like that
return $this->hasOne('App\MenuCat', 'id', 'menu_cat_id');

how to write query for mongodb to pass in knp paginator in symfony2

i am using knp paginator bundle for pagination.
i know to use knp paginator for doctrine. but i don't know in mongodb.
below code which show for doctrine ORM query which pass to knp paginator.
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM AdwindBannerBundle:Banner a";
$query = $em->createQuery($dql);
$pagination = $this->get('knp_paginator')->paginate($query,$page,10);
same way i want to use for ODM (mongoDB).
but i have confusion that what kind of query i have to use for ODM(mongodb) to pass in knp paginator?
Maybe
$dm = $this->get('doctrine_mongodb')->getManager();
$qb = $dm->createQueryBuilder('AcmeDemoBundle:User');
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$qb,
$this->get('request')->query->get('page', 1)/*page number*/,
10/*limit per page*/
);

Doctrine 2 + Zend Form - Populate Dynamic Select Menus

I'm building a Zend form that has a dropdown/select menu populated with data from a Doctrine 2 query.
In my repository class, I have the following query in a method named selectUser():
$query = $em->createQuery('SELECT u.id, u.name FROM XX\Entity\Users u ORDER BY u.name ASC');
$users = $query->getResult();
This returns a multidimensional array, which I'm trying to loop through like this (within the same method):
$options = array();
foreach ($users as $key => $value) {
$options[$value['id']] = $value['name'];
}
return $options;
Then in my Zend form class, I try to populate the Select element like this:
$id = new Zend_Form_Element_Select('id');
$options = $this->usersRepository->selectUser();
$id->AddMultiOptions($options);
The result is an error for each user row that states "Undefined index: [name] in ...UsersRepository.php..." where [name] is the value of the 'name' column in each row.
Does anyone see what I'm doing wrong or how to populate a dynamic select menu using Doctrine 2 and Zend Framework?
(By the way, in order to run the repository method, the form class has protected properties representing the Doctrine container, entity manager, and Users repository. If this isn't considered best practice, I'd welcome any suggestions on improving my technique.)
I think your problem is here
$options[$value['id'] = $value['name']];
this would be better
$options[$value['id']] = $value['name'];