I'm using Doctrine DBAL and in the ORM docs it says, in order to execute a querybuilder query I first have to get a Query object using getQuery method. Unfortunately it looks like in DBAL there's no getQuery method to get the job done, so what's the procedure? get the SQL from getSQL method and execute it separately?
If you want to retrieve an array you could do
$qb->execute()->fetchAll();
Related
I am trying to query a table using where like :
Auth::user()
->friends
->where('friend.first_name','LIKE','%'.$firstName.'%')
->all()
The query does work properly without using the LIKE statement only when i add the LIKE keyword it doesn't return any results.
Using all() will work without the builder where() statement.
When grabbing data using the query, use get() instead:
Auth::user()->friends->where('something','LIKE','%'.$something.'%')->get()
I think you are confused between a laravel relation and a eloquent collection. First let me go straight to answer, you would have to do
Auth::user()
->friends()
->where('friend.first_name','LIKE','%'.$firstName.'%')
->get()
To explain what's wrong with your code, ->where('name', 'Like', '%something%') This is a query builder method. You have to use it on the query builder, or Before you actually retrieve the model / model collection.
Auth::user()->friends()
This would give you a relation Illuminate\Database\Eloquent\Relations\HasMany that you can apply where like on.
Auth::user()->friends
This will return you a Illuminate\Database\Eloquent\Collection. And yes, it is a collection, so you will have to use collection method. Take a look at the where method for collection. You can see it only support key => value pair. It does not support "LIKE"
The where method filters the collection by a given key / value pair:
If you want to use achieve a "Like" on collection, you can probably use the filter method, which support custom callback.
The filter method filters the collection using the given callback, keeping only those items that pass a given truth test:
To conclude, when you define a friends "HasMany" relation on User, you have two way to retrieve it.
If you want to apply additional query builder method, you need to use user->friends() which do not return you data, it return you a relation that you can use query builder method. Once you do user->friends, it get you a collection. You cannot apply query builder on that anymore. So basically, user->friends is act the same way as user->friends()->get()
Don't Use all() and use get().
Auth::user()->friends()->where('something','LIKE','%'.$something.'%')->get()
I want to know the difference between laravel eloquent conisgnment->runsheet->name and $consignment->runsheet()->name
i notice that there is performance difference but why? also I notice that when I try to get relationship property like $consignment->runsheet->name it work fine but $consignment->runsheet()->name doesn't work
The first one
$consignment->runsheet
retrieve a collection (so it's already made a query to DB)
while the second
$consignment->runsheet()
is a query builder (hasn't made a database query) you can chain it like any query builder instance
so if you want to get a property from the second one you can do it like:
$consignment->runsheet()->first()->name;
OR
$consignment->runsheet()->value('name');
$collections = Collection::selectRaw("DATE(created_at) as collection_date, sum(gross_amount) as amount")
->groupBy('collection_date')
->paginate(10);
When I do that it displays that collection_date does not exist in the table, so I wonder how can I create a group by with just DATE() in eloquent?
Thanks
Based on Laravel docs, pagination doesn't work with the groupBy clause properly.
https://laravel.com/docs/7.x/pagination#paginating-query-builder-results
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.
I've created a materialized view: accounts_index that has a couple left joins, so I need to do a group by with the eloquent model. I also need to paginate the collection and am having trouble nailing the order of things. The materialized view:
CREATE materialized VIEW accounts_index
AS SELECT a.account_uuid,
a.account_no,
a.person_owner_uuid,
a.company_owner_uuid,
a.account_group_uuid,
a.account_scope_uuid,
a.created_at,
a.deleted_at,
s.service_uuid,
s.status,
st.service_type
FROM accounts a
LEFT JOIN services s
ON a.account_uuid = s.account_uuid
LEFT JOIN service_types st
ON s.service_type_uuid = st.service_type_uuid
The eloquent model can grab that table like so: AccountIndex::all();.
I could paginate that: AccountIndex::paginate(); or do a groupBy: AccountIndex::all()->groupBy('account_uuid');, but lost on how to combine the two. Some attempts:
AccountIndex::all()->groupBy('account_uuid')->paginate(): Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
AccountIndex::paginate()->groupBy('account_uuid');: returns the collection without paginating.
$accountsIndex = collect(\DB::table('accounts_index')->get());
$accountsIndex->groupBy('account_uuid')->paginate();: Same Method Illuminate\Support\Collection::paginate does not exist. exception.
The main problem I'm trying to solve is that these joins will be returning accounts that have multiple services (so multiple rows) and I need to then group by the account_uuid. Thanks in advance for any insights!
paginate method is related to Illuminate\Database\Query\Builder or Illuminate\Database\Eloquent\Builder classes, it is not exists in Collection class,
the corresponding method for it in Collection class is forPage method, you can get more information from the documentation
almost
AccountIndex::groupBy('account_uuid')->paginate()
paginate actually DOES something
groupBy() is just writing SQL just like ->where() ->join() etc that all prepares an SQL statement you can try by running ->toSql() returns an sql string.
get() all() paginate() all actually do something e.g. execute the SQL.
I am trying to get SQL query as a string from query builder. toSql() method works fine without laravel scout. I am using this package for Fulltext search. Here is my code.
Post::toSql(); // Working without scout => select * form post
But I am getting an error when try to search
Post::search('test')->toSql();
Method Laravel\Scout\Builder::toSql does not exist
How Laravel Scout can be extended to add a method toSql to it.
Try this
DB::enableQueryLog();
Post::search('test');
dd(DB::getQueryLog());