I Have 2 version app, one in local use MySQL and the other one is build in Heroku using PostgreSQL
The plan is to have search form that able to lookup data from different table
The table is books, writters, categories, and publishers
Here the controller
public function search(Request $request)
{
$keyword = $request->input('keyword');
// multiple query from different table
$query = Book::where('judul','like','%'.$keyword.'%')
->orWhere('label','like','%'.$keyword.'%')
->orWhere('isbn','like','%'.$keyword.'%')
->orWhere('status', 'like', '%'.$keyword.'%')
->orWhereHas('writter', function ($query) use ($keyword) {
$query->where('nama_penulis', 'like', '%'.$keyword.'%');
})
->orWhereHas('category', function ($query) use ($keyword) {
$query->where('nama_kategori', 'like', '%'.$keyword.'%');
})
->orWhereHas('publisher', function ($query) use ($keyword) {
$query->where('nama_penerbit', 'like', '%'.$keyword.'%');
})
;
$book_list = $query->paginate(5);
$pagination = $book_list->appends($request->except('page'));
$total_book = $book_list->total();
return view('dashboards.index', compact('book_list', 'keyword', 'pagination', 'total_book', 'nama_penulis'));
}
The form works like a charm on local (using my MySQL), but when I'm using Postgres the search form only able to fetch from books table.
I wonder what make postgres ignoring my code
someone on the Laracast explain to me that The problem is from like which is Case Sensitive for the Postgres, thats why my code didnt work.
If you have same issue, i recommend you to use ilike to make POstgres insensitive.
or you can try the alternative way by whereRaw instead.
Related
My two table Member and Deposit there has one to many relationship one member has multiple deposit in Deposit table i want to search by multiple column both table which will have to match.
This is my Member Table
1.id,
2.branch_id,
3.village_id,
4.user_id,
5.name,
6.phone,
7.email,
8........
My Deposit Table
1.meber_id,
2.user_id
3.deposit_date,
4.deposit_amount,
5.total_amount,
6..........
My Controller Code
$depo = Deposit::with(['member'=>function($query){$query->where('branch_id',$request->branch_id)->where('status','running')->get();}])->where('user_id',$request->user_id)->whereDate('deposit-date','>=',$from_date)->whereDate('deposit-date','<=',$to_date)->get();
if i do that then ....$query->where('branch_id',$request->branch_id)->get()..... section is not working please help me any one
Try this:
$depo = Deposit::whereHas('member', function($query) use ($request){
$query->where([
['branch_id' => $request->branch_id],
['status'=> 'running']
])
})
->where('user_id',$request->user_id)
->whereDate([
['deposit-date','>=',$from_date],
['deposit-date','<=',$to_date]
])->get();
Your question is quite ambiguous, but looks like you need to use the $request in the with function.
$deposits = Deposit::with(['member' => function ($query) use ($request) {
$query->where('branch_id', $request->branch_id)
->where('status', 'running')->get();
}])->where('user_id', $request->user_id)
->whereDate('deposit-date', '>=', $from_date)
->whereDate('deposit-date', '<=', $to_date)
->get();
But the with method wont filter down your query it will simply limit the number of members returned with all the deposits. It's not searching in the member table.
UPDATE 04/12/2018
Without checking the docs at all, and completely off the top of my head.
Deposit::with('member', function($query) use($request){
$query->where('branch_id', $request->branch_id)
->orWhere('village_id', $request->village_id)
})->where(function($query) use($request) {
$query->where('user_id', $request->user_id)
->whereDate('deposit-date', '>=', $from_date)
->whereDate('deposit-date', '<=', $to_date)
})->orWhereHas('member', function($query) use($request){
$query->where('branch_id', $request->branch_id)
->orWhere('village_id', $request->village_id)
})->get();
I am learning how to do simple queries using the Yii2 framework. I use PostgreSQL.
I am trying to join two tables and get the data from both tables with a where condition.
The tables are called Admins and Persons.
The join use field called idadm.
The condition is idadm = 33. This works great but the result has data only from the Admins table and I need data from the other table.
Here is my example:
$query = \app\models\Admins::find()
->select('*')
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->all();
I am following the Yii2 official guide: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
Update: Here I show the updated code that doesn't solve de problem:
You need to write all column name in select().
$query = \app\models\Admins::find()
->select('admin.*,persons.*') // make sure same column name not there in both table
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->all();
And also you need to define person table attributes in Admin model.
Second way is get records as array,so you dont need to define attributes in Admin model.
$query = \app\models\Admins::find()
->select('admin.*,persons.*') // make sure same column name not there in both table
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->asArray()
->all();
Ensure that active record has required relations, e.g. something like follows:
class Admins extends \yii\db\ActiveRecord {
public function table() {
return "admins";
}
public function getPersons()
{
return $this->hasMany(Person::className(), ['idadm' => 'idadm']);
}
}
class Person extends \yii\db\ActiveRecord {
public function table() {
return "persons";
}
}
Then use joinWith to build query:
$query = Admins::find()
->joinWith('persons')
->limit(1);
$result = $query->createCommand()->getSql();
echo $result;
Here is produced query:
SELECT `admins`.* FROM `admins`
LEFT JOIN `person` ON `admins`.`idadm` = `person`.`idadm` LIMIT 1
I'm working on super simple search across multiple fields in a document to see if any of them has a single value. (Note: some fields are using regex to search if value is contained in string). Using query builder I constructed the following.
public function search($value, $limit, $offset=0, $orderby = '', $order='' )
{
$regexVal = new \MongoRegex('/^.*(\b'.str_replace(' ', '\s', $value).'\b).*?$/i');
$query = $this->repository->createQueryBuilder();
$query->addOr($query->expr()->field('location')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.first_name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.last_name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.email')->equals($value));
$query->addOr($query->expr()->field('email')->equals($value));
$query->addOr($query->expr()->field('organization')->equals($value));
$query->limit($limit)
->skip($offset);
if( ! empty($orderby) && $order ){
$query->sort($orderby, $order);
}
return $query->getQuery()->execute();
}
If I dump out the constructed query values I get the following array in this gist. https://gist.github.com/jchamb/04a0400c989cd28b1841 The extra association field in there is being added by a Doctrine Filter.
Through Query builder I don't get any results, however if I construct the query myself and run it in an admin app like genghis, I get the expected single document result.
Actual written mongodb string looks like this. https://gist.github.com/jchamb/ce60829480576a88290d
This project is a zend2 app that was already using doctrine and mongo. I'm not much of an expert with mongo in general so I'm not sure what i'm doing wrong inside of Query Builder that i'm not getting the same result as executing the query directly. I can't find any info on stack or the query builder docs that gives any extra clues for the multiple addOrs syntax either.
Any help or direction would be really appreciated, in the most basic form I need query builder to get a document where association = x and ( field1 = val or field2 = value).
Thanks!
Really unsure what the exact issue was with the above, but after playing around, switching the order of query builder around fixes the problem.
public function search($value, $limit, $offset=0, $orderby = '', $order='' )
{
$regexVal = new \MongoRegex('/^.*(\b'.str_replace(' ', '\s', $value).'\b).*?$/i');
$query = $this->repository->createQueryBuilder()
->find()
->limit($limit)
->skip($offset);
$query->addOr($query->expr()->field('location')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.first_name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.last_name')->equals($regexVal));
$query->addOr($query->expr()->field('mappedData.email')->equals($value));
$query->addOr($query->expr()->field('email')->equals($value));
$query->addOr($query->expr()->field('organization')->equals($value));
if( ! empty($orderby) && $order ){
$query->sort($orderby, $order);
}
return $query->getQuery()->execute();
}
Would love to still hear some feedback about why this works and the above didn't if anyone know more about the internals of query builder.
I've got a problem which I don't know how to solve.
I've got companies which have a profile(profile belongs to company) and have locations(company has many locations).
I'm trying to get all companies which have a name like $request->input('search_term') and have a least one location with a zip or city matching $request->input('search_addition') and a profile with a type of 0.
My code is:
$companies = Company::with(['profile' => function ($query) {
$query->where('type', 0);
}])
->whereHas('locations', function ($query) use ($request) {
$query->where('zip', 'like', '%'.$request->input('search_addition').'%')
->orWhere('city', 'like', '%'.$request->input('search_addition').'%');
})
->where('name', 'like', '%'.$request->input('search_term').'%');
At the end I add $companies = $companies->paginate(25);
The result I get is not what I want. I get all companies which have a name like $request->input('search_term'). It ignores the type of the profile and the locations. The locations array is empty for those companies which don't match the specified search_addition but it still returns the company.
with() is used for relationship eager loading either with or without constraint, not used as a constraint to filter the parent model which you need to use where function family instead.
This is what I think the code should be (not tested).
$companies = Company::with('profile')
->with(['locations' => function ($query) use ($request) {
$query->where('zip', 'like', '%'.$request->input('search_addition').'%')
->orWhere('city', 'like', '%'.$request->input('search_addition').'%');
}])
->whereHas('profile', function ($query) {
$query->where('type', 0);
})
->whereHas('locations', function ($query) use ($request) {
$query->where('zip', 'like', '%'.$request->input('search_addition').'%')
->orWhere('city', 'like', '%'.$request->input('search_addition').'%');
})
->where('name', 'like', '%'.$request->input('search_term').'%');
I want to update an entry in the table when 2 conditions are met.
I have this statement, but it is only for one where condition
$this->dbo->update('mytable', $data, $this->dbo->quoteInto('id= ?', $id));
Instead of just checking where for just "id", i want to check for userid also..
Appreciate any help.
Something similar to this should work as the $where argument will accept and parse an array, reference the _whereExpr() method in Zend_Db_Adapter_Abstract for the code on how the $where arg is processed:
$this->dbo->update('mytable', $data, array('id= ?'=> $id, 'user_id=?'=>$userId));
I'm going to suggest that you may wish to alter you approach and use the save() method of Zend_Db_Table_Row instead of update. Here's an example.
public function saveUser($id, array $userData, $userId = null)
{
//$this->getDbAdapter is a placeholder for your choice of db adapters, I suggest a DbTable model that extends Zend_Db_Table_Abstract
$select = $this->getDbAdapter()->select();
//if userId is not null build a where clause
if (!is_null($userId)) {
$select->where('user_id = ?', $userId);
}
//build where clause for primary key, two where() in select() will be 'AND' use orWhere() for 'OR'
$select->where('id = ?', $id);
//fetch the row you wish to alter
$row = $this->getDbAdapter()->fetchRow($select);
//assign the data to the row, you can update any or all columns
$row->username = $userData[username];
$row->user_id = $userData[user_id];
//ect...
//save the new data to the row, will only update changed coluns
$row->save();
//return the whole for use in other ways, save() typically only returnbs the primary key.
return $row;
}
Yes this method is a little more verbose and maybe a touch more complicated. However as you start bumping up against the limits of 'INSERT' and 'UPDATE', save() can provide some useful functionality.