Laravel 5.3 Method distinct does not exist - laravel

I am trying to write a query that returns the count of distinct links. However, I keep on getting
Method distinct does not exist
Here is my code
$sample = Sample::all();
$uniqueResumes = $sample->distinct()->select('link')->count();

Please try this :
$uniqueResumes = Sample::select('link')->distinct()->count();

Related

How to reverse Array of Data in Laravel when using Eloquent with Pagination?

I am fetching a list of data using Eloquent method and trying to reverse the order of array, I am getting an error.
Here is the code :(Previous Code)
$temp = Product::paginate(20);
New Code
$temp = Product::paginate(20)->orderBy('id','desc');
Error :
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
New method :
$temp = Product::paginate(20)->sortDesc();
Error :
Method Illuminate\Database\Eloquent\Collection::appends does not exist.
Can anyone help me?
use below syntax
Product::orderBy('id','desc')->paginate(20);
try to write it in this way,
use orderBy before the paginate
$temp = Product::orderBy('id','desc')->paginate(20);
->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Product::latest()->paginate(20)

How to optimize the following query Laravel?

I have the code which is working perfectly fine.
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping2 = AmenityCategoryMapping::where('company_id', $property->company_id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping3 = AmenityCategoryMapping::whereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping = array_merge($amenityCategoryMapping3, $amenityCategoryMapping2, $amenityCategoryMapping1 );
However, currently I am executing 3 different query to extract data from the same table just using different parameters. Is there any way to minimize this query requests and still receiving the same results?
Currently, while plucking if two array has the same key then I am replacing amenityCategoryMapping3 by amenityCategoryMapping2 and that by amenityCategoryMapping1. That is more priority should be given to amenityCategoryMapping1
Try using orWhere function
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orWhere('company_id', $property->company_id)
->orWhereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();

Laravel Query Result Issue: query is returning empty array even there is data exist into database

I am working on laravel 5.4. I am trying to get data from database using below query:
$obj = \Illuminate\Support\Facades\DB::table('A')
->join('B', 'A.Intake', '=', 'B.Id')
->join('C', 'B.StreamId', '=', 'C.StreamId')
->select(\Illuminate\Support\Facades\DB::raw('DISTINCT(C.StreamId) As StreamId'))
->where('A.YearId', $yearId);
$streamIds = $obj->get()->toArray();
The above query is returning empty results. I have also debug the query generated by laravel. Below is the raw query generating by laravel on the basis of above conditions:
select DISTINCT(C.StreamId) As StreamId from A
inner join B on A.Intake = B.CentreStreamId
inner join C on B.StreamId = C.StreamId
where A.YearId = '12'
When I run the above raw query directly in my database, then it returns me some records. But when I try to get records in laravel then it returns me empty results.
I am not able to get the issue with the above query. Can someone please tell me why it is returning empty results set? If there is any issue with above query builder syntax then please correct my query.
Thanks in Advance.

no results if setting more than 2 parameters to the querybuilder - laravel-doctrine/orm + Lumen + oracle

I am facing a strange problem trying to execute a simple request using laravel-doctrine/orm v1.2.5 on Lumen v5.2.7. with an Oracle database.
When I create a query with the query builder containing more than 2 parameters, I get no results, even though I am expecting a result set.
$conn = $this->getEntityManager()->getConnection();
$queryBuilder = $conn->createQueryBuilder();
$queryBuilder->select('*')
->from('SYNCHS')
->where('SYNCHS.CLIENT_ID = :clientId')
->andWhere('SYNCHS.TOP_CLIENT_ID = :topClientId')
->andWhere('SYNCHS.ID = :synchId');
$queryBuilder->setParameter('clientId', $clientId)
->setParameter('topClientId', $topClientId)
->setParameter('synchId', $synchId);
echo $queryBuilder->getSQL();
var_dump($queryBuilder->getParameters());
$stmt = $queryBuilder->execute();
var_dump($stmt);
$results = $stmt->fetchAll();
dd($results);
If I comment any one of the where/addwhere clause, I end up with the expected result set, but if I have more than 2 parameters bound in the query, it returns nothing (of course it should return something with the passed parameters).
For every scenari:
I can see all my parameters are bound in the query builder var_dump($queryBuilder->getParameters());
Copy paste of the echo $queryBuilder->getSQL(); in Toad (oracle) setting the parameters with the values displayed by the var_dump($queryBuilder->getParameters()); give me 1 row (expected result). So I am sure that my query is correct.
I've looked for hours on google but no relevant result appeared for this problem. Thanks

proper usage of queries (Aggregates) in laravel 4.2

im trying to get the total count of records that are less than a value in my table so im using this query in laravel 4.2
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft','<','ModuleCriticalLevel')
->count();
and passes it like this
return View::make('ssims.board' ,compact('title','mTotal','critical'));
//please don't mind the others
then receives it in the view page like this
<div>Modules at critical level <span><strong><?= $critical ?></strong></span></div>
unfortunately, im getting zero whereas in my database, i have 2 records where ModuleCountLeft is less than ModuleCriticalLevel
any ideas?
thanks
Hi turns out that i needed to pluck out the value of ModuleCriticalLevel first here is the code
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
-thumbs up here-

Resources