Raw select using jenssegers Laravel-MongoDB - mongodb

I add jenssegers Laravel-MongoDB package into my Laravel 4.2 project, and have a one problem. Maybe can help me. I cann't use DB::raw in select or get method. I need to change name of select column and add columns into one column, but Laravel return me an error.
I try to do something like this:
$arr = StudentMark::join('students', 'students.id','=','student_marks.student_id')
->select(array('student_marks.id',DB::raw('CONCAT(students.name, " ",
students.surname, " (", students.index,") ") AS student')))->get();
But Laravel return me an error:
{"error":{"type":"ErrorException","message":"Illegal offset type","file":".....vendor\\jenssegers\\mongodb\\src\\Jenssegers\\Mongodb\\Query\\Builder.php","line":240}}
Can anybody help me?

I use select with columns, and concating columns do in foreach of results and then return to client, and that solve me a problem. :)

Related

Select Data using date between operator and two unconnected tables

I am tryin to resolve this situation. The following piece of code works ok .
SELECT FISCAL_START, DESCRIPTION,
CASE WHEN FISCAL_START BETWEEN DATE'2014-03-31' AND DATE'2014-09-29' THEN DESCRIPTION
ELSE 'Test Failed'
END
FROM GL_PERIOD
what I am trying to achieve is as follows instead of the hard coded dates of 2014-03-31 and 2014-09-29..need to replace those with column names FISCAL_START and FISCAL_END dates from an unconnected view call WORK.
Psuedo code is as follow s...
Show Description from GL_PERIOD where ENTER_DATE (which is in WORK view) is between FISCAL_START and FISCAL_END. Any help will be greatly appreciated.
I think you need this, I just continue your query to add the missing piece:
Here is sqlfiddle that will show you live example: fiddle example
SELECT "FisStart", "Descripton",
CASE WHEN "Entry_Date"
BETWEEN "FisStart"
AND "FisEnd"
THEN "Descripton"
ELSE 'Test Failed'
END
FROM GL_PERIOD
INNER JOIN WORK USING USING ("SrNo")

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

How can I display count of imagefield images in views?

I want to display the number of images uploaded to an imagefield, in a views field or views tpl.php. How can I do this? My imagefield is called boatimages. I tried this but it comes out as 0, not the correct number: < ? php print count($fields->field_boatimages) ?>
Ack. I do not think count() works like that.
Why not just do this using Views? Take a look at Arguments > Settings and you'll see 'display record count' which seems like all you would need for this.
My suggestion is install the devel module and use the function dpm to print the variable if you wanna know the structure (print_r() may work too). If count isn't working it's because, you are probably using it with the wrong data.
OR, you could just query the database for the field. I'm gonna provide you instructions for drupal 7 but drupal 6 should be similar.
Check the table field_data_field_boatimages. See how there's a list of your images related with a single entity_id
Then execute this query
SELECT COUNT(*) FROM `field_data_field_boatimages` WHERE entity_id = ###
Where ### is the entity_id you want to know. You can get it by looking for arg(1) if arg(0) == node in your page.
Now you just have to use php power to print thar result
$query = SELECT COUNT(*) FROM `field_data_field_boatimages` WHERE entity_id = :eid
$result = db_query($query, array(':eid', $nid))->fetchField();
echo $result;
Drupal 6 would be very similar. Just a little difference in the table names and the query syntax. For example using db_result instead of fetchField()
Anyway good luck!

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?