Laravel query builder function whereNotBetween() usage issue - laravel

Has anyone used DB::whereNotBetween() in Laravel4?
I tried using it for retrieving rows from a table where the field value is not between the given 2 dates, but failed! Here is how I tried it -
DB::table('bookings')->whereNotBetween('arrival',array("2014-01-02","2014-01-10"))->get(array('id'));
This should return all booking rows where the arrival date is not between the above given dates. But instead I am receiving the following error -
"Unknown column 'not_between' in 'where clause' "
not sure what I am doing wrong so all helps are welcomed.
thanks in advance :)

whereNotBetween is a new feature in Laravel 4.1.. Check your version with php artisan -V to be sure. If you don't have 4.1. installed, you can easily follow this guide to upgrade from 4.0.*. I just did it twice last week and it's fairly simple.
If you are on 4.0.*, and can't upgrade for some reason, I would suggest going back to the old tried-and-true: where(...,'>=', $enddate)->where(..., '<=', $startdate).

Related

Laravel returning Unknown column 'table.deleted_at'in 'where clause'

It seems there are lot of posts about this error, but before everything, I must say that I have use SoftDeletes; because I use it and I need it to manage trashed models.
Column 'deleted_at' also exists.
Solution like this saying to remove it does not suit in my case.
I am looking for help to keep use SoftDeletes; but making my request working properly.
Here is my query:
public function scopeWithRowNumber($query)
{
$sub = Model::selectRaw('*, #row:=#row+1 as row')
->orderBy('some_date', 'desc')->toSql();
$query->from(DB::raw("({$sub}) as sub"));
}
The sql exit gives me :
select *, #row:=#row+1 as row from `model_table`
where `model_table`.`deleted_at` is null order by `entry_date` desc
How to fix it?
I finally use this:
withoutGlobalScope('Illuminate\Database\Eloquent\SoftDeletingScope')
on my queries to avoid checking deleted_at column.
The issue is relative to nested queries and seems "normal". In my case, this helps me to fix/avoid error:
> Unknown column 'deleted_at' or column not found.
Maybe it will help someone else.
Also for ranking, I was inspired by this post. But I have to edit my query a little bit because of more conditions with dynamic parameters I added. But this is another subject.

Get data for Last 7 days from database for each day in laravel

Good Evening.
I am trying to get the data from database for Only for last 7 days and show the same in table.
I tried the below code, but its giving me data for 30 days. I am new to coding and a self learner,
Your help will be helpful...
controller code
$sevendays = Carbon::now()->subDays(7);
$dairymilksaleweek = customermilksale::selectraw('(saledate) as "startdate", (SUM(buffalomilk)) as "totalbmilk", (SUM(a2milk)) as "totala2milk", (SUM(jerseymilk)) as "totaljmilk", (SUM(totalmilk)) as "totalmilk"')
->whereDate('saledate', '>=', now()->subDays(7)->startOfDay())
->groupBy('saledate')
->orderBy('saledate')
->get();
Thanks in advance...
As per your query you wanted to get data for last seven days and you are using the whereDate() method of laravel eloquent which needs dates in specified format i.e format('d/m/Y') don't know what's the format for your saledate column but you need to convert second parameter of the function to one of the below.
1- today()->subDays(7)
2- now()->subDays(7)->startOfDay()->format('d/m/Y')
Hope this will help you out.

How to show DBMS name and version in laravel view?

I want to show a status bar in a laravel website. Which will show the DBMS name and version number. The output I want is like
PostgreSQL 9.2.24
Or
MySQL 5.6
I can get the name of database by using env('DB_CONNECTION'). It is giving me the name(although, it is showing 'pgsql'; not 'PostgreSQL ').
However, I don't understand how can I get the version number in view. What is the way to get the version number?
You can use DB::raw() to create database raw expression.
$version = DB::select( DB::raw("select version()"));
pass the value to your view.

Sorting by the newest comment with 3 tables users,posts, and comments in Laravel Eloquent

I'm sorting my post by the newest comment timestamp. It works but when it comes to paginating the posts I get empty paginated posts in my rendered pagination.
Sorry about my previous reply but do check this on laravel documentation https://laravel.com/docs/5.7/queries#joins > sub-query joins or
try: ->orderBy('comments.created_at','desc'), instead of using the orderByRaw(), if also you want to orderBy multiple columns you can chain the other column to the previous like this
->orderBy('comments.created_at','desc')
->orderBy('confession.created_at','desc')
also had similar problem some days back and that was how i fixed mine. hope its helps.

Laravel DB:table created_at Date Is Different from DB

Running Laravel 5.6, I found this oddity:
> php artisan tinker
> App\Users::all();
...
created_at: "2018-06-04 16:26:00",
updated_at: "2018-06-04 16:26:00",
....
Pulling in the same element within a Laravel model using:
$users = DB::table('users')->get();
$users->first()->created_at
//2018-04-06 15:59:01
This is the same row. The 35 minute delay is odd, but perhaps due to Homestead latency(?) I have no idea why the month and day are rearranged.
In my middleware, I set the locale information as such:
setlocale(LC_TIME, env('APP_LOCALE_CODE')); //de_DE.UTF-8
date_default_timezone_set(env('APP_LOCALE_TIMEZONE')); //APP_LOCALE_TIMEZONE=Europe/Berlin
Anyone have an idea here?
Turns out the query I needed required multiple joins and groupby sorting so I used DB::table instead of eloquent. When doing so, you need to be careful of the select statement, especially with common DB fields like created_at. Essentially it was pulling in another "created_at" field and by luck, happened to be a reverse of todays current date causing the initial thought it was a formatting error.

Resources