I losted trilion hours google this but none of the solutions were good.
I have this querybuilder:
$qb2=$this->createQueryBuilder('s')
->addSelect('u')
->innerJoin('s.user','u')
->where("u.id IN(:followeeIds)")
->andWhere('s.admin_status = false')
->setParameter('user', $user)
->setParameter('followeeIds', $arrayFolloweeIds)
->orderBy('s.id','DESC')
->setMaxResults(15)
;
I could do a second query and then do like $qb->getDQL() but have would i cache the query ?
Error:
Invalid parameter number: number of bound variables does not match number of tokens
You are setting the user parameter but I do not see it being used in the query anywhere?
Also I had issues with WHERE IN and Doctrine QueryBuilder with arrays would give me a similar error, and oddly enough running array_values before binding the parameter seemed to solve those issues as well.
Try:
$qb2=$this->createQueryBuilder('s')
->addSelect('u')
->innerJoin('s.user','u')
->where("u.id IN(:followeeIds)")
->andWhere('s.admin_status = false')
->setParameter('followeeIds', array_values($arrayFolloweeIds))
->orderBy('s.id','DESC')
->setMaxResults(15)
;
In Symfony2.8 the following example helps me
...
$qb2->where(
$qb2->expr()->in('u.id', ':ids')
)
->setParameter('ids', $ids_array)
...
Related
$posts = Post::all()->orderby('created_at','desc')->where('usr_id','=',session('LoggedUser'))->get();
return view('admin.profile',compact('userInfo' , 'posts'));
i am making a custom auth for a journal activity but i cant sort the content i shows this error
"Method Illuminate\Database\Eloquent\Collection::orderby does not exist. "
$posts = Post::where('usr_id','=',session('LoggedUser'))->orderby('created_at','desc')->get();
True query like that. When you take all() already query done.
Change it to:
$posts = Post::where('usr_id','=',session('LoggedUser'))->orderby('created_at','desc')->get();
you cant use all() and orderBy because all() does not allow the modification of the query.
I believe this might be because you typed orderby instead of orderBy (notice the uppercase). See laravel orderBy documentation if needed.
Plus, as mentionned by other, don't use all() if you need to do other thing (where clause, order by, etc) in you query.
Change the orderby to orderBy. This could be the reason you are getting the error.
$posts = Post::all()->orderBy('created_at', 'DESC')->where('usr_id','=',session('LoggedUser'))->get();
return view('admin.profile',compact('userInfo' , 'posts'));
Or...
If you want to get specific number of posts you can do it this way to avoid using the Post::all
$posts = Post::orderBy('created_at', 'DESC')->where('usr_id','=',session('LoggedUser'))->paginate(5);
return view('admin.profile',compact('userInfo' , 'posts'));
Yeah this is pretty confusing and just got me as well.
The actual problem isn't the capitilization typo (orderby versus orderBy) but rather the fact that you're using ->all() instead of just Model::orderBy()->...
The moment you use ->all() the object is transformed to another type of collection object and the normal methods one would expect do not exist.
In this case you should rather use sortBy().
See here.
I am using try in Laravel 5.5 like this...
try {
$fruit = Fruit::findOrFail($id);
}
But I would like to check that not only that it finds the Fruit with the supplied ID but that it also has a fruit_color of 'red'
Do I need to do this with a 'with' statement?
I know I can run another check afterwards but wondered if I could do this all in one statement?
A few things. First, throwing an exception here is the wrong way to handle the ‘if’ situation. If you plan on a situation where a value isn’t return, then this isn’t the proper use of an exception. To answer your question:
$fruit = Fruit::where(‘id’, $id)->where(‘color’, ‘red’)->get();
This returns a collection of items meeting your criteria. Next to test if the collection is empty (no fruit) you can do the following:
if($fruit->isEmpty()) {
//handle empty collection
}
Hope this helps! The Laravel documents for collections kicks a**. I’d recommend reading further there.
You just need to add your extra conditions in before you call the find:
try {
$fruit = Fruit::where('fruit_color', 'red')->findOrFail($id);
}
Try this code
$fruit = Fruit::where(‘id’, $id)->where(‘color’, ‘red’)->first();
I'm trying to count result from my query which is using multiple where query. But it doesn't seem to be working.
My syntax is:
$partialpaidquery=['month' => $maina];
$partialpaid=Bill::where($partialpaidquery)->where('paid','!=',0)->where('fee_status','<','amount')->count();
where clause upto where('paid','!=',0) seems to work but the third one is not working. What is the problem here? It actually should have returned 1. But it is returning 0.
Seems you are using wrong query :
You are comparing < string 'amount' instead of use variable $amount
like below:
$partialpaidquery=['month' => $maina];
$partialpaid=Bill::where($partialpaidquery)->where('paid','!=',0)->where('fee_status','<',$amount)->count();
Use your third where as
->whereRaw('fee_status < amount')
because the way you are using it , the amount column is being interpreted as string not column.
This portion of a Doctrine query is only returning results for "validated = 1". How do I modify this query so it will also include results for validated = 3? This is my first "IN".
$query
->andWhere($query->expr()->in('m.validated', ':validated'))
->setParameter('validated', '1,3');
Try this code instead,
$query
->andWhere('m.validated IN (:validated)')
->setParameter('validated', array('1','2'));
Or with the same code giving values in an array.
Hope this helps.
Cheers!
trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....