Doctrine Where 'IN' operator - doctrine

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!

Related

Method Illuminate\Database\Eloquent\Collection::orderby does not exist

$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.

Laravel Offset not offsetting and/or Skip not skipping

i have a database with data and i want to skip/offset the first 3 row.
$data = Data::orderBy('created_at','desc')->skip(3)->paginate(1);
$data = Data::orderBy('created_at','desc')->offset(3)->paginate(1);
both query is returning all result from the start. can anyone help me with this?
Thanks.
skip doesn't seem to work with paginate. What you can do is exclude the row by using whereNotIn.
$data = Data::orderBy('created_at','desc')->whereNotIn('id', [1,2,3])->paginate(1);
If you don't know the id you can query and use the result.
$id = Data::orderBy('created_at','desc')->take(3)->pluck('id');
$data = Data::orderBy('created_at','desc')->whereNotIn('id', $id)->paginate(1);
You can not use paginate() and skip() together. You can do is :
$data = Data::orderBy('created_at','desc')->skip(3)->take(10)->get(); and update these values skip and take values as per your custom implementation.
If you literally want to skip first 3 rows and never ever use them in pagination, you can do :
$dataToEliminate = Data::orderBy('created_at','desc')->take(3)->select('id')->pluck('id');
$data = Data::whereNotIn('id', $dataToEliminate)->orderBy('created_at','desc')->skip(3)->paginate(1);
See documentation for reference.
I have explored the paginate method from the code and came to know
$data = Data::orderBy('created_at','desc')->paginate(1, '*', null, 2);
The 4th parameter, you need to provide the page number (not the offset).

laravel return '?' instead of '1' in WHERE clause

$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->toSql();
dd($datas);
I wrote query in "laravel 5.2". I displayed it as sql query with function toSql()
and the result is:
"select * from `els` where `status` = ? order by `created_at` desc limit 4"
you can see that there is ? mark. this is why my query doesn't work. why does it return me ? instead of one. I also tried it like this '1' same result.
You may want to read about bindings if you want to understand what does ? mean.
https://laravel.com/docs/5.3/database#running-queries
If you want to debug and watch queries, you can install this debugbar: https://github.com/barryvdh/laravel-debugbar
? is used for Parameter binding which prevent against SQL injection.
If you want to see the raw SQL query which is executed with parameters values, try the below code:
DB::enableQueryLog();
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->get();
dd(DB::getQueryLog());
Hope this helps!

Assistance with Doctrine ODM query

I have the following query which is providing undesired results.
$query = $dm->createQueryBuilder('MainClassifiedBundle:Discussion')
->field('id')->equals($discussionId)
->field('discussion_id')->equals($discussionId);
What I am trying to do is to find any documents where id = $discussionId OR discussion_id =$discussionId.
Many thanks for your help
Well, you have the methods add() and addOr of query builder:
$query = $dm->createQueryBuilder('MainClassifiedBundle:Discussion');
->add($query->field('id')->equals($discussionId))
->addOr($query->field('discussion_id')->equals($discussionId));
Also you can replace the add and addOr methods by where and orWhere

Doctrine query not executing

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() ....

Resources