It's possible to write in Laravel query reverse for whereJsonContains() query, something like: whereJsonDoesntContains()?
I have problem with data filtering in Model. This code works perfectly:
$query->whereHas('attributes', function($q) use ($attribute)
{
$q->where('attribute_product.attribute_id', $attribute->id)
->whereJsonContains('attribute_product.values', $someValue);
});
Now I want to create another query that returns products whereHas attributes with ID and JSON array in values column doesn't contain my variable.
Do you have any idea to do this?
Yes, there is a function just like that: whereJsonDoesntContain
Related
Database table SITE has many columns. One of them is site_id. I need all the site_ids as an array since it has to be fed to a method which accepts only a string array.
What I tried so far is:
$sites = DB::select('select site_id from site_tab');
$sites_arr = $sites->toArray();
But this doesn't produce the result I want. I need $sites_arr to be like ['A','B','C',...]
Please suggest a way to get this done. A solution based on Eloquent is also OK for me.
Thanks
Try this:
DB::table('site_tab')->pluck('site_id')->toArray();
reference pluck
referen toArray
If you open a manual, you will see that
The select method will always return an array of results
So, there's no need to use ->toArray(), as result is already an array.
To get values as array of names you can do:
$site_ids = DB::table('site_tab')->pluck('site_id');
Using ->toArray() here is optional, as you can iterate over $site_ids (which is a Collection) with a foreach too.
I want to groupBy() task using Laravel Eloquent. I have searched Internet and didn't find anything with eloquent.
some people used groupBy() query with Query Builder like this link
But I want to create query like this style:
Task::select('id')->groupBy('category_id')->count();
This code just return only count of first category_id. But I want count of all the category_id.
Native Collection alternative (grouped by php, not mysql). It's a nice option when you have the Collection as variable already assigned. Of course is less optimal than mysql grouping in most cases.
$tasks->groupBy('category_id')->map->count();
You should add the count to the select function and use the get function to execute the query.
Task::select('id', \DB::raw("count(id)"))->groupBy('category_id')->get();
The \DB::raw() function makes sure the string is inserted in the query without Laravel modifying its value.
More simple:
Task::select('id')->groupBy('category_id')**->get()**->count();
You can do something like this:
return DB::table('offers')->select('*', DB::raw('count('.$field.') as total_count'))->groupBy($field)->get();
I overrode the count() method in my model. Could be something like:
public function count($string = '*'){
$tempmodel = new YourModel();
return $tempmodel ->where('id',$this->id)->where('category_id',$this->category_id)->count($string);
}
This gave me exactly the behavior of count() I was looking for.
Worked for me as
we need to select one column like 'category_id' or 'user_id' and then
count the selected column on get()
Task::select('category_id')
->where('user_id',Auth::user()->id)
->groupBy(['category_id'])
->get()
->count();
This works for me.
output:
return $email_trackers = EmailTracker::get()->groupBy('campaign_id')->map->count();
{
"6": 2
}
Hello I would like to know is it possible to get column value as string. Instead of array in array:
Current query: Number::limit('1000')->get(['number'])->toArray()
The result at the moment is this:
Preferable result:
Before your toArray() call, add pluck('number'):
$result = Number::limit('1000')->get(['number'])->pluck('number')->toArray();
That's it! This will pluck just the number attributes from your result collection, and give you a single-level array.
The reason this works, is because you are getting a Collection back from get():
All multi-result sets returned by Eloquent are an instance of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship.
And the pluck method:
https://laravel.com/docs/5.1/collections#method-pluck
Update
Another, even more succinct method provided by #wunch in the comments:
$result = Number::limit('1000')->lists('number')->toArray();
How can I do multiple wherein in laravel?
$devices = DB::table('foo')
->select('foo.*')
->whereIn('bar1', $request->bar1)
->whereIn('bar2', $request->bar2)
->get();
Above is my sample code but it is returning me an empty array.
It is ok to use multiple WHERE IN constraints in your query. The code you provided is also ok.
If you're getting no results, make sure that values of $request->bar1 and $request->bar2 are what you expect - they should be arrays of values that contain what you want your bar1/bar2 columns to be.
You can always get the generated SQL by calling toSql() instead of get(), you can also inspect the values of parameters by calling getBindings().
I have this query:
static function findIdOnName($pageName){
return Fanpages::select('id')
->where('url', '=', $pageName)
->get();
}
Response: (when done print_r)
[{"id":17}]
I just want the INT (in this case 17) I searched the interwebs for it, but I can't find anthing about it. Randomly tried adding ->toString() etc to the query, but so far, no good.
Your code returns a Collection with a single Model (or multiple models if there are more matching the where clause), while the method you need is pluck:
return Fanpages::where('url', '=', $pageName)->pluck('id');
// returns INT 17
as it returns value for column id of the first row matching WHERE clause.
If you do not return a view with data, then Laravel will automatically convert your data into json. In order to accomplish what you want you can simply do something like
$data = Fanpages::select('id')->where('url', '=', $pageName)->get();
die($data->id);
However, exiting the application like this isn't recommended. You should either keep the json response and work with that, or send the data to a basic blade template.