I have the following record in my database where I have to get all records containing any of the records.
Table record contains:
$record_ids = ["123","234","111"]
Trying to search for it like the following with Laravel:
$records = DB::table('records_table')
->where('id', $id)
->whereRaw('JSON_CONTAINS(record_id, ?)', $record_ids)
->pluck('records');
I've also tried with similar solutions like:
->whereIn('record_id', $record_ids)
I'm using Laravel 5.5 and Mysql.
Update:
It works when I 'manually' add it like the following:
->whereIn('record_id', ["123","234","111"])
But when fetching the array from the database it doesn't work
I think you want to use here:
->whereIn('record_id', json_decode($json, true));
The types have to match (integer vs. string) and the search value has to be JSON encoded:
$search = json_encode(array_map('intval', $record_ids));
$records = DB::table('records_table')
->where('id', $id)
->whereRaw('JSON_CONTAINS(record_id, ?)', $search)
->pluck('records');
In Laravel 5.6, you can use whereJsonContains():
->whereJsonContains('record_id', array_map('intval', $record_ids));
Related
I've been trying this query
Model::find()
->innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id')
->where(['IN', 'translation_code', $arrayOfTranslationCodes])
->asArray()
->all();
The translation table contains multiple rows with the same ID but with different translation codes.
This query only returns the first matching locale for a given ID. How would I retrieve the other translation codes for a given ID?
This is the solution that I came to:
$query = Model::find()
-> innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id');
foreach ($arrayOfTranslationCodes as $translation)
{
$query->andWhere(['OR', 'translation_code', $translation])
}
$queryResponse = $query->asArray()->all();
This allowed me to find rows with the same id, but have different translations. You need to store the $query->asArray()->all(); as $query itself just returns the active query.
I have the following table named mytable:
id SERIAL PK
namae VARCHAR
And using laravel 5.7 I need to retrieve the data using the following code (tested in a tinker session):
$yakuzaNames = DB::select('SELECT id from mytable where name like 'omaewa%');
dump($yakuzaNames);
The problem is that once data retrieved in variable $yakuzaNames is in the following format:
[ { id: 1},{id:2},...]
From the results I need to retrieve an array containing integers with the id, therefore I need to manipulate it via array_map:
$yakuzaNames = DB::select('SELECT id from mytable where name like 'omaewa%');
$yakuzaNames = array_map(function($item){ return $item->id },$yakuzaNames);
Or use a foreach loop:
$yakuzaNames = DB::select('SELECT id from mytable where name like 'omaewa%');
$names = [];
foreach($yakuzaNames as $yakuzaName){
$names[] = $yakuzaName->id;
}
But using a loop seems kinda a waste also using some sort of iteration seems waste as well. Is there a way for laravel's database layer be able to return directly the data in the format I want to?
Use pluck method for collection. https://laravel.com/docs/8.x/collections#method-pluck
I have problem with my laravel query.
Now I use query like this:
$courses = Course::whereJsonContains('schedule->day', 1)->get();
It doesn't work.
I'm using postgreSql 9.6 and my database and raw query look like this
http://sqlfiddle.com/#!17/88fd2/1/0
I want to select class where have schedule in day = 1
If you define the column as schedule->day, MySQL assumes that this is an array of integers. In your case it's an array of objects, so you have to target the parent array and add the property name you are looking for in the second argument.
Like so:
$courses = Course::whereJsonContains('schedule', ['day' => 1])->get();
I solved with
$courses = Course::whereJsonContains('schedule', [['day' => '1']])->get();
I solved with
$products=ProductShop::active()
->whereJsonContains('tag', [['value' => "tampa"]])->get();
If you're querying the value уоu don't need to use whereJsonContains, simply use a regular where query such as:
$courses = Course::where('schedule->day', 1)->get();`
If you want to check if day exists in Json, use whereJsonLength such as:
$courses = Course::whereJsonLength('schedule->day', '>', 0)->get();
The following is the get(); query :
$zero_others = DB::table('postusrs')
->where('post_id', $p_id)
->where('user_id','!=', $op_id)
->where('highted','=', 1)
->get();
Here I will fill the array (which I believe is wrong, and I need to know the correct syntax)
$zero_id = $zero_others->id;
And lastly, I will update the group of IDs received in $zero_id into the DB :
DB::table('postusrs')->where('id', $zero_id)->update(['highted' => 0]);
I'm seeing an error of: Trying to get property of non-object. Please advise how to fill the array ?
If you want to collect an array of ids, in Laravel 5.2. Try this:
$zero_ids = DB::table('postusrs')
->where('post_id', $p_id)
->where('user_id','!=', $op_id)
->where('highted','=', 1)
->pluck('id');
But you have to change the update query using whereIn:
DB::table('postusrs')->whereIn('id', $zero_ids)->update(['highted' => 0]);
When building a query to find multiple values from a model, eloquent would look something like this:
return Contact::whereIn('user_name', explode(',', $userNames)
But let's say that the value for userNames is ['foo', 'bar']; but I only have foo as a valid username, is there a way to get bar (all failed to find) out as part of the same query without having to compare the result ->get() against the request?
It's not possible to get the query to return the list of username that doesn't exist from the given condition. But you could do this.
$allUserNames = explode(',', $userNames);
$validUserNames = Contact::whereIn('user_name', $allUserNames)
->pluck('user_name')
->toArray();
$invalidUserNames = array_values(array_diff($allUserNames, $validUserNames));
You can use array diff to remove the unvalid value form the select region, and you lost the ->get() to get the results;
return Contact::whereIn('user_name', array_diff(explode(',', $userNames, ['bar']))->get()