laravel query builder remove where clause - laravel-5

I have whereIn in Query Builder (Laravel 5.6)
$data->whereIn('field1', [1,2,3])->whereIn('field2', [4,5,6])->whereIn('field3', [8,9,10]);
....
$some_count = clone $data;
$result = $data->get();
$count_result = $some_count->count(); <-- need without one of whereIn
And now, I need to do count() without one of whereIn, for example "whereIn('field2', [4,5,6])". How to remove this clause from $some_count?

Related

I want to extract the first five data that match the where clause from Laravel relationships

I'd like to do something like this with Laravel's Eloquent:relationship, but it doesn't work.
$playlist->setRelation('tags', $playlist->tags->where('privacySetting', 'public')->take(5));
It works without where clause, but I want to retrieve the first 5 data in the tags relationship table that match the where clause.
How can I do this?
Laravel version is 7.28.1.
this will select top 5 based on criteria from model
$playlist = Playlist::with(['tags' => function($filter){
return $filter->where('privacySetting', 'public')
->take(5);
}])
->get();
//try dd($playlist);

why when i select all data is showing but when i use find or where data not showing even if i check with dd

some make me confusing in laravel when i use all() data is showing but when i select find or where data not showing when i use dd function in laravel
//$regency = Regency::find(1101);
//$regency = Regency::where('number','=',$prov_id);
$regency = DB::table('regencies')->where('province_id', $prov_id);
dd($regency);
i try use eloquent and query builder but still same result.
those is result in dd if i using query builder, but why when i use select all data is showing
What you are showing is the Query Builder you need to execute it with ->get() to get the Collection.
You have to get the data from the query builder in order to receive the results as a collection of objects
// $regency = Regency::findOrFail(1101);
// $regency = Regency::where('number', $prov_id)->get();
$regency = DB::table('regencies')->where('province_id', $prov_id)->get();
dd($regency);
Writing a query builder just produces the SQL query but doesn't actually execute/run it
Hope this helps

when to use get() in query laravel 5

I have a basic query set up in the show method of a laravel resource
public function show($id){
$results = Student::find($id);
$drives= Drive:: where('student_id', $id);
}
The query for $results works perfectly. The query for $drives does not work unless I do ->get() at the end of it. Why is this? what's the difference between the two queries so that one requires the ->get() and the other does not? Solving this problem took me like 5 hrs and i'm just curious as to the functionality behind it so i can avoid this headache in the future.
Some eloquent expressions have a get implicitly. Those ones who are made by a Query Builder will need a ->get() call, find(), findOne()... won't need a get().
https://laravel.com/docs/5.6/eloquent#retrieving-models
https://laravel.com/docs/5.6/queries
use get to execute a builder query. unless you run the get() query wont be executed. get will return a collection.
1 - Use query builder to build queries however you want.
$drives= Drive:: where('student_id', $id);
dd($drives); // will return a query builder, you can use it to build query by chaining
2 - when you are ready to execute the query call get()
$drives= Drive:: where('student_id', $id);
$result = $drives->get()
dd($result); // will return a database query result set as a collection object
If you want to get a single object by id use find, to get a single object
$results = Student::find($id);
dd($result); will return a single model
Using the function find() on a model gets a query result based on the primary key of the model, id in this case.
When using where(), it gets a collection (an object of all query results), so if you only want the first result you must call $drives=Drive::where('student_id', $id)->first();
Here is a more in-depth explanation: the difference of find and get in Eloquent

Laravel where statement

I am new in Laravel. I want to create a search filter using eloquent model.
I want to check some columns are equal to some enteries and return the results, but the problem is that if any of the enteries is empty it returns nothing.
How to make it to search if other enteries matche and return values even one of the entery is empty.
$result = person::Where('age','>=',$age)
->Where('hairColor','=',$hairColor)
->Where('height','>=',$height)
->get();
Just use orWhere
$result = person::Where('age','>=',$age)
->orWhere('hairColor','=',$hairColor)
->orWhere('height','>=',$height)
->get();
Also, check this documentation
Laravel where clauses

Pluck together with first using Query builder

If I want to get get name of first user in database using eloquent I can do something like that:
$user = User::select('name')->first()->pluck('name');
// or $user = User::first()->pluck('name');
echo $user;
to get only name of this user as string.
However If I try the same using only query builder:
$user = DB::table('users')->select('name')->first()->pluck('name');
echo $user;
I get exception:
Call to undefined method stdClass::pluck()
But without using first it will work:
$user = DB::table('users')->select('name')->where('id',1)->pluck('name');
echo $user;
Is it not possible to use pluck with first using query builder or am I doing something wrong?
PS. Of course I know that I can display any property using $user->name without using pluck but I'm just curious why using Eloquent it works and using Query Builder it works only when not having both first and pluck
You don't want to use pluck with first, because it's redundant:
$query->first() // fetch first row
->pluck('name'); // fetch first row again and return only name
So use only pluck:
$query->pluck('name');
It does all you need.
But there's more.
Under the hood first and pluck run 2 separate queries, so:
$query->where(..)->orderBy(..)->first() // apply where and orderBy
->pluck('name'); // no where and orderBy applied here!
So it's not only redundant to use first before pluck, but also it causes unexpected results.
You can chain those methods on Eloquent query, since it returns a collection (get) or model (first), but Query\Builder returns just an array (get) or stdObject (first). That's why you couldn't do the same oon the query builder.
In Laravel 5.1+, you can use the value method.
From the docs:
If you don't even need an entire row, you may extract a single value
from a record using the value method. This method will return the
value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
This is how it works behind the scenes in the Builder class:
/**
* Get a single column's value from the first result of a query.
*
* #param string $column
* #return mixed
*/
public function value($column)
{
$result = (array) $this->first([$column]);
return count($result) > 0 ? reset($result) : null;
}
Try this one:
$user = User::pluck('name')->first();
echo $user;
Current alternative for pluck() is value().
To get first occurence, You can either use
DB::table('users')->value('name');
or use,
DB::table('users')->pluck('name')->first();

Resources