how to use between with laravel query builder - laravel

I am trying to use BETWEEN with laravel query builder. How do I do it. I have tried this below
Eventcalender::where(['between', 'acceptance_date', $final, $time])->count();

Check hist out: whereBetween()
The whereBetween method verifies that a column's value is between two values,
For Eloquent:
Eventcalender::whereBetween('acceptance_date', [$final, $time])->count();
For Query Builder:
DB::table('eventcalenders')->whereBetween('acceptance_date', [$final, $time])->count();

Related

Laravel retrieve only specific fields from each item of collection

I may be missing something extremely trivial, but is it possible to retrieve specific columns/fields from models when grabbing a collection rather then returning the entire item's fields?
Here is my query:
$items = Items::where('visible', true)->take(10)->get();
This obviously returns each item in there entirety, including unique id's, and other fields i dont want to be fetched... how can i refine this query to just select specific fields from the models?
Laravel Query Builder get() function receives array of columns which you need to fetch.
$items = Items::where('visible', true)->take(10)->get(['column_1', 'column_2']);
Use select() method to do this:
$items = Items::select(['column_1', 'column_2']'])->where('visible', true)->take(10)->get();
Source: Latavel Database Query Builder
Laravel Query Builder gives a huge flexibility to write this types of query.
You can use select(), get(), all() methods.
Items::where('visible', true)->take(10)->get('col_1', 'col_2');
OR
Items::select('col_1', 'col_2')->where('visible', true)->take(10)->get();
Items::select(['col_1', 'col_2'])->where('visible', true)->take(10)->get();

Laravel 5.5 Call to undefined method stdClass::update()

Laravel 5.5 show me the error message:
Call to undefined method stdClass::update()
for the following codes in the Model
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)->first()
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
Try using ->limit(1) instead of ->first() in the chain.
In Laravel if you wish to update only one record then you can use ->first() method of Eloquent ORM to get eloquent object and then you can simply update the object by ->save().
Laravel query builder directly not supporting update only record as Update with limit of 1 is not directly supported by mysql MySQL - UPDATE query with LIMIT
In the example you have given here is some modification to be work,
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
This will result in updating the records where specified conditions. To update specific record pass id column in where clause of that particular record.

Query_while in Laravel

How would I do such a similar query with laravel?
$GLOBALS['db']->query_while("SELECT * FROM games
WHERE id_gioco='".$id_gioco."' AND id_utente='".$USER['id']."'");
In the documentation of laravel and Eloquent, I found nothing on query_while.
Not sure what a while query is , but the following should work. If you chain 2 where clauses it will make it an AND where()
$games = Game::where('id_gioco', $id_gioco)->where('id_utente', $user->id)->get()

Select one column with where clause Eloquent

Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;

How to combine and & or operators in grocery crud

I am using codeigniter and grocerycrud, I want to set a table with grocerycrud in this way:
SELECT * FROM `dashboard`.`medidas_ludlum`
where FK_ludlum='190.26.88.131'
and date>= '2014-03-04 09:40:00'
and date<= '2014-03-05 09:40:00'
and (error_code=1 or audio_status=1);
I tried to do that in this way:
$uno='1';
$crud2 = new grocery_CRUD();
$crud2->set_theme('datatables');
$crud2->where('FK_ludlum',$ludlum_id);
$crud2->where('date>=',$fechainicio);$crud2->where('date<=',$fechafin);
$crud2->where('error_code =',$uno);
$crud2->or_where('audio_status =',$uno);
$crud2->set_table('medidas_ludlum');
$crud2->columns('measurement', 'fecha', 'audio_status', 'high_alarm_status', 'low_alarm_status','over_range_status','monitor_status','error_code');
$crud2->set_language("spanish");
$crud2->unset_add();$crud2->unset_delete();$crud2->unset_edit();$crud2->unset_read();
$data['crud2'] = $crud2->render();
However it's not giving the right results, for example I am getting rows that have a date out of the range, is there a way to get that CRUD set up?
Grocery curd is also using codeigniter's active record so you can write your where() with grouped conditions as below,no need to use or_where() function
$crud2->where('(error_code ='.$uno.' OR audio_status ='.$uno.')',null,FALSE);
or
$crud2->where('(error_code =1 OR audio_status =1)',null,FALSE);
The problem in your query is when you use or_where() function it produces where clause as and error_code=1 or audio_status=1 and using approach that i have provide will give you the where clause as and (error_code=1 or audio_status=1) a grouped condition
API and Functions list

Resources