getting the relation data from where clause in view - eloquent

I have the following query
$partner = DeliveryPartner::with(['deliveryPartnerImage' => function($q) {
$q->where('image_type', '=', 'logo');
}])
->find($id);
this works. for a partner with a logo it shows a relation with 1 item like so
and when i change the where clause to something non existent for example $q->where('image_type', '=', 'testtesttest');
it shows a empty array like this
so i know my query works but how to get this data in the view?
i did $partner->deliveryPartnerImage()->first() But for some reason this always shows data? also when i change the query... so im guessing im doing this wrong but i can not find another way to do this?

Use this:
$partner->deliveryPartnerImage
$partner->deliveryPartnerImage()->first() executes a new query without the where() constraint.

Related

Can't use changeable field in Mongoose Sort

Hello i want to use Mongoose sort but i have a problem when i want to change my sorting with arguments it doesn't work. For example
Users.find().sort({sortBy: 1})
Its not working at all. Whats the problem??
You have 2 problems in your code:
1. You are not specifying what field to sort by:
You forgot to change the sortBy field in your sort object to the wanted field to sort by.
For example, if you want to sort your users by name it will look like:
User.find().sort({name: 1})
2. You are not executing your query
You need to execute the query using the .exec(callback) function.
Your code will look like:
Users.find().sort({sortBy: 1}).exec((err, documents) => {
// Your logic
})
You can also use the await keyword to get your data without a callback function.
const users = await Users.find().sort({sortBy: 1}).exec();
Just note that if you decide to use the await option it needs to be in an async function.

Not able to pass complete datetime(Y-m-d H:i:s) in DB::raw in eloquent

$shop=DB::table('shops')
->leftJoin('orderbookings',function($join)
{
$join->on('shops.id','=','orderbookings.shop_id');
$join->on('orderbookings.created_at','>=',DB::raw(date("Y-m-d",strtotime("now"))));
})
->select('shops.*')
->selectRaw('COUNT(orderbookings.id) as totalorder, SUM(orderbookings.grand_total) as gtotal')
->orderBy('shops.shop_name', 'asc')
->groupby('shops.id')
->paginate(10);
Above code working fine(But not giving total order and amount correct) and also gives result almost close to what I want,
But I am not able to give date format (Y-m-d H:i:s), it shows syntax error. I am using Laravel 5.2 version
Note: I want to give time as well with date to rectify result,
On giving [example: 2017-03-08 11:15:00] shows syntax error
working query in mysql
SELECT COUNT(orderbookings.id), SUM(orderbookings.grand_total), shops.shop_name FROMshopsLEFT JOIN orderbookings on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02" GROUP BY shops.id
But not able to to convert in eloquent
You should be able to do the following:
$join->on('orderbookings.created_at', '>=', date("Y-m-d"));
You don't need to use DB::raw, and leaving the second parameter null for date, assumes "now".
If this doesn't solve your issue, please post the exact error you're seeing.

Laravel Eloquent query similar to findorfail

I would like to look up and return an object referenced by a different column to that of id.
As far as I know there isn't another method similar to Task::findOrFail($id) but that can reference another field. For example:
Task::findOrFail('column_name' = 'column_data');
I'm currently using
Task::where('tid' , '=', $tid)->first();
Is this possible?
Maybe you can use the firstOrFail() function of Laravel.
Task::where('column_name', '=' ,'column_data')->firstOrFail();

wordpress 3.2.1 database query

i am trying to make a simple selection in a wordpress table (created by a plugin). the table is named reduceri , and has the following columns: id, post, category.
so, i am trying to take all the category values, when the post is equal to the current post id.
the way i am doing the query is:
$the_query = "
SELECT $wpdb->reduceri.category
FROM $wpdb->reduceri
WHERE $wpdb->reduceri.post = ".$post_id."
";
$my_reduceri = $wpdb->get_results($the_query);
but when i var_dump the $my_reduceri all i get is an empty array: array(0) { } even though there should actually be some results... any idea where i am wrong (in the query)?
thank you
Did you declared global $wpdb; before using this query?

How to get whole index in ZEND Lucene?

Hi I am looking for a way to get the whole index if my query is nothing.
My lucene is a picture of my database without some unwanted content, like expired annonces...
So I would like to use only lucene to get annonces, and for that I need a way to get the whole index.
Any ideas?
thanks!
This is not the answer but something wich works for me:
Using an indexKey like is_indexed, always true.
I am adding "is_indexed:1" to my query and it works...
If you have something else, let me know!
I tend to use a field which is named after the index such as:
$oDoc->addField(
Zend_Search_Lucene_Field::keyword(
'index',
'availability'
)
);
Then the term query will return all fields. It's not pretty but it works fine.