Laravel query for showing records starting soonest but not records where date has passed - laravel

I'm a little new so I only know the basics. I have a course table where it has the start_date of a course (timestamp) and I would like to show the courses starting closes to the current date but not courses that have passed (e.g yesterdays courses).
I also need to not do this with a scope.
My model is App\Course. My current code is
$course = Course::latest()->take(6)->get());
Could anyone help? Thanks!

Try this:
$course = Course::where('start_date', '>', Carbon::now())->orderBy('start_date', 'desc')->take(6)->get());
It will work if you'll add start_date to a $dates variable.

Related

Show list of registered users base on current month using laravel

Im new to laravel and im tryin to learn the fundamentals of it. My question is how can I display all the registered users that is registered only on the current month.
$from = now()->startOfMonth(); // first date of the current month
$to = now();
$usersRegisteredThisMonth = User::whereBetween('created_at', [$from, $to])->get();
There is a simple way of doing it.
Just use this code
User::whereMonth('created_at', now()->month) // checking if the month of created_at is current month
->whereYear('created_at', now()->year) // checking if the year of created_at is current year
->get();
This line will give you Users from current month.
Nobody explained that these queries might be put in user model with local scope scopeWith magic method. I assume you read docs from top to bottom.
Simple example:
public function scopeRegisteredLastMonth($query){ return $query->whereBetween... }
Laravel Local Scope
You can specyficy additional arguments for this method.
The final call in controller will look like this:
$lastMonthUsers = User::registeredLastMonth()->get();
This function should be set to something like 'public function withRegisteredBetween($query, $date_start, $date_end) and return query based on date range.
PS: Don't use DB:: when you can use Model::method() or Model::query()->method()
PS2: for date management I advise you to install carbon, it's an additional addon - sometimes it's easy, sometimes not, overall not bad.
You could use the users table with a whereBetween clause like this:
$from = date('2022-01-05 00:00:00');
$to = date('2022-31-05 00:00:00');
$usersRegisteredThisMonth = DB::table('users')->
whereBetween('created_at', [$from, $to])->get();

Laravel Jobs to get data from API

I have this database table which is going to be updated continuously.
game_id | end_time
Now how can I create a job that will run on this end_time, a job will get some data from third-Party API which is gonna be depended from this game_id.
Note: game_id and end_time changes from every record.
I have looked-up on task scheduler but I cant find information I think that is only for a specific time.
Yes, you can use laravel scheduler for this task. You can look at the time variable inside the console command class.
For example put in your kernel class:
$schedule->command('ApiConnection:get')->everyFiveMinutes();
And in your ApiConnection class (handle() function) you can get your DB records:
Model::whereDate('end_time', '>=', Carbon::today())->get(); //format of Carbon function should be equal to your end_time column format, I mean I don't know if you use just date or date with time, etc
And now you should be able to use game_id data by time.
I also suggest to add status column in your DB table, so you can control what records is already handled, like this:
$models = Model::whereDate('end_time', '>=', Carbon::today())->where('status', 'awaiting')->get();
foreach ($models as $model) {
$model->status = 'in progress';
...your api work here...
$model->status = 'completed';
}

Any reason why a model's fields not updating despite an update call?

In Laravel 5.3 I am, using Model events to take certain actions. For the most part, things are working fine, just one place in the program flow where data is not updating.
Here is the whole story:
I have a database structure with courses and weeks. So Course 1 with Week1, Week2, etc.
Then to repeat a given course I also have courseinstance and weekinstance which contain date info for given instances of courses and weeks.
I am using Angular in the backend to handle CRUD, and when I create a course it automatically creates a courseinstance where a start_date and end_date are null.
The code for that in the course API controller is
$thiscourse= \App\Courses::create(Input::all());
After that the course model executes this:
public static function boot() {
parent::boot();
static::created(function($course) {
// add first instance of this course - dates will be null at this point
$instance = new Coursesinstance;
$instance->course_id = $course->id;
$instance->save();
});
}
That is successful. Then in my backend, I add a week to the course (which is for descriptive data about the week's topic) and that creates a first instance of the week where there are a start and end date. In the API week controller first we have this:
$week = \App\Week::create(Input::all());
Then that triggers create event on the model and we go to:
static::created(function($week) {
// add first instance of this week
$instance = new Weekinstance;
$instance->week_id = $week->id;
$instance->weekstart = $week->weekstart;
$instance->zoomstart = $week->zoomstart;
$coursesinstance_id = DB::table('coursesinstance')
->select('id')
->where('course_id', $week->courses->id)
->first();
$instance->coursesinstance_id = $coursesinstance_id->id;
$instance->save();
});
This is a success, things looking fine in the database, all relationships proper, various id's just as they should be.
Now since a new week instance is created, we go to this in the Weekinstance model so that I can look at all weeks in the instance and update the course instance start and end dates.
static::created(function($weekinstance) {
// get all week instances corresponding to this course instance
$weekinstances = DB::table('weekinstance')
->select('*')
->where('coursesinstance_id', $weekinstance->coursesinstance_id)
->get();
$weekstarts=array();
foreach ($weekinstance as $instance)
{
$weekstart = $instance->weekstart;
$weekstarts[].=$weekstart;
}
$dates = array_map('strtotime', $weekstarts);
$startdate = min($dates);
$enddate = strtotime("+7 day", max($dates));
// update start and end date for the course instance
$result = \App\Coursesinstance::where('id', $weekinstance->coursesinstance_id)
->update([
'start_date' => $startdate,
'end_date' => $enddate
]);
});
And here is where things break down. The week is successfully created, the weekinstance is created, but the update call above just doesn't set the start_date and end_date of the courseinstance.
I have ran in a debugger, and at the moment of the update, all seems well. A $startdate and $enddate variable are created as timestamps, the where clause is indeed returning the course instance, the return result is a 1, so Laravel is not returning any errors, and in log file no issues. start_date and end_date are fillable on the model. Everything truly looks normal.
The date fields are are stored as TIMESTAMP in MySQL. It does not accept Unix timestamp's as an argument. So you must convert the timestamp's into the correct format, before updating. One way to do this:
->update([
'start_date' => Carbon\Carbon::createFromTimestamp($startdate),
'end_date' => Carbon\Carbon::createFromTimestamp($enddate)
]);
Ok, after spending so much time on this I finally realized the problem is that I was passing my timestamp values directly to the query builder. Little did I know that MYSQL expects a timestamp as a date string. I thought the data type datetime was meant for strings and that a timestamp, as name suggests, could be used for an actual timestamp. Crazy thinking, I know.
So here is how I had to create the "timestamps":
$startdate = date('Y-m-d H:i:s',min($dates));
$enddate = date('Y-m-d H:i:s',strtotime("+7 day", max($dates)));
So in other words, 180 degrees away from what I would consider logical based on definition of a timestamp.
Thanks,
Brian

how to select one month back records from data base in laravel

how select one month back records from current date from database in laravel. I am trying this code.
This is controller code.
class LoginHistoryController extends Controller {
public function index()
{
$login_history = LoginHistory::where('login_date','BETWEEN', '(CURDATE() -
INTERVAL 10 DAY) AND CURDATE()' )->get();
}
}
but i am getting error.
I will have a approach something like this. First I will calculate the date like
$today = date('Y-m-d');
$date = date_create($today);
date_sub($date, date_interval_create_from_date_string("30 days"));
$beforeOneMonth = date_format($date, "Y-m-d");
You should have the intended value in $beforeOneMonth by now. Now you can compare it in anyway you like whether you use IN operator or >=. For eg.
$login_history = LoginHistory::where('login_date','>=', $beforeOneMonth)->get();
Give it a try. If you are storing date in some other format, you can do your own tricks to format the date and do the thing
Another way to do it would be with whereRaw:
$login_history = LoginHistory::whereRaw(
'login_date BETWEEN (CURDATE() - INTERVAL 10 DAY) AND CURDATE()'
)->get();
Note that whereRaw has the side effect of making your code less portable since you're using SQL that might be specific to your database server. But sometimes you just can't do what you would like using the query builder.

How to use whereBetween for dates in Laravel

I am trying to get the number of new users for each week using the created_at column. I am trying to use the whereBetweensyntax but it always return 0 even when it is suppose to return otherwise.
{{ DB::table('users')
->whereBetween('created_at', array(date("Y/m/d h:i:s", strtotime('sunday last week')), date("Y/m/d h:i:s", strtotime('saturday this week'))))->count(); }}
Any suggestions?
The query itself should work as written, though you might want to verify that created_at is a column of either timestamp, date, or datetime type. When you created the users table, did you use a migration to create your users table, and if so, did you specify $table->timestamps();? Or did you manually define the created_at column, and perhaps set it to a string?
A couple other (unrelated) suggestions:
• It appears that you're running this query in a view, echoing the count using blade. This logic would be better handled elsewhere, perhaps in your User model, and the result passed to the view by the controller.
• You can simplify your query using PHPs DateTime object, replacing your date(...strtotime...) with date_create(...):
$usersThisWeek = User::whereBetween(
'created_at', array(
date_create('sunday last week'),
date_create('saturday this week')
))->count();

Resources