today()->format('l');
Today with Carbon
How can i view only today's day (monday) using carbon in laravel?
I have developed an app for separate collection that shows a weekly calendar with the various waste, if I want to view only today's day using Carbon, what should I do? This is the project
If I wanted to create a week with carbon and then compare today's day and view the type of waste to be differentiated
are you trying to filter your results so on a specific day you only see that specific days results?
from your notes model you are a relationship called day which will get you the id of the day you have attached to that note. That data isn't a specific date object in the context of Carbon, but just a string that maps to ['monday','tuesday','Wednesday'],...]
if you want to get the notes for Friday, you will need to filter on
$day = Day::where('name', today()->format('l'))->first();
which would get the $day where its name matches 'Friday';
and then filter.
$todaysNotes = Note::where('day_id',$day->id)->get();
if you have localised your day names to your language, you will need to localise Carbon too.
eg replace
today()->format('l')
with
Carbon::setLocale('it_IT')->now()->format('l');
would return venerdìfor Friday
I have two sections: one for the weekly calendar (Week controller) and one for today (Today controller). Here is a photo:
@piero on your day controller, get it to show all days. Post the controller method, and then I can show you show to get it to just display today.
BTW it will really help you if you watch the Laravel 8 from scratch series, here: https://laracasts.com/series/laravel-8-from-scratch
Take some time to read the documentation and that will make it easier for you to develop your application.
Today controller:
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class TodayController extends Controller
{
public function show()
{
$currentDate = Carbon::now()->locale('it_IT')->timezone('Europe/Rome');
return view('today.show',compact('currentDate'));
}
public function showToday()
{
}
}
if you want to get 'notes' for today, then the following should work:
public function show()
{
$currentDate = Carbon::now()->locale('it_IT')->timezone('Europe/Rome');
$day = Day::where('name', $currentDate->format('l'))->first();
$notes = Note::where('day_id', $day->id)->get();
return view('today.show', compact('currentDate','notes'));
}
@piero this is assuming you have a day with a name matching what's produced by $currentDate->format('l')
The above returns an array of `$notes'. if you only have one, change to
$notes = Note::where('day_id', $day->id)->first();
@michaloravec I know this could be reduced by doing a query where but I'm keeping it simple whilst we have the training wheels on..
I have an error:
Trying to get property 'id' of non-object on line
$notes = Note::where('day_id', $day->id)->get();
so you arent getting a match on the day name.
what values do you have stored in your days table?
I have the following values:
so its just not matching case. use ucwords
$day = Day::where('name', ucwords($currentDate->format('l')))->first();
I wrote how you suggested but it doesn't work, I always have the same mistake
$day = Day::where('name', ucwords($currentDate->format('l')))->first();
ok, so what value do you get for:
$currentDate->format('l')
With the CurrentDate variable I get today's day to display on the screen as shown in the photo
I know what you do with it :P
what value do you get in your controller if you
dd($currentDate->format('l'))
With
dd(...)
I have this
The following will return Venerdì
$currentDate = Carbon::now()->locale('it_IT')->timezone('Europe/Rome');
ucwords($currentDate->dayName);
I would like to compare the current day with the list of days and then extract the corresponding day.
yes
this is what we're doing here:
$currentDate = Carbon::now()->locale('it_IT')->timezone('Europe/Rome'); // get currentDate
$day = Day::where('name', $currentDate->dayName)->first(); // gets day record where day.name matches $currentDate->dayName
$notes = Note::where('day_id', $day->id)->get(); // gets notes where notes.day_id matches $day->id.
return view('today.show', compact('currentDate','notes'));
How do I also recover the other data? Type, day of collection and start and end time?
@piero you already know how to get this info from $notes. We've covered that in a previous thread.
if you only have one note for each day then you can just return this instead of an collection of 1 item.
$note = Note::where('day_id', $day->id)->first();
return view('today.show', compact('currentDate','note'));
I want to recover the other data relating to today's day. How can I do?
I have the following page:
@extends('layout')
@section('title')
Oggi
@endsection
@section('content')
<div class="row">
<div class="col-md-12"></div>
<h4 class="display-2 fw-bold fst-italic text-info">
Oggi, {{ $currentDate->isoFormat('llll')}}
</h4>
</div>
</div>
<div class="d-flex flex-row bd-highlight mb-3 mt-4">
<div class="p-1 bd-highlight">
<div class="card text-center text-dark " style="width:16rem; left:55rem; top:6rem">
<div class="card-header">
<h2>{{$day->name}}</h2>
</div>
<div class="card-body">
<h3 class="card-title">{{$notes->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$notes->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$notes->ora_inizio}}</p>
<p class="card-text">Ora fine: {{$notes->ora_fine}}</p>
</div>
</div>
</div>
@endsection
as I said, you have already done this:
https://github.com/pierre1590/recycle/blob/main/resources/views/calendar/index.blade.php
you should be passing $note not $notes if you have a single note for each day.
$note = Note::where('day_id', $day->id)->first();
return view('today.show', compact('currentDate','note'));
One last question, if I wanted to create a check to check if one day there is no collection, how should I implement it?
if you have a day with no collection, then you won't get a $note returned for it.
so add a condition:
@if($note)
<h3 class="card-title">{{$notes->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$notes->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$notes->ora_inizio}}</p>
<p class="card-text">Ora fine: {{$notes->ora_fine}}</p>
@else
<p>No Collection</p>
@endif
I have the following page :
@extends('layout')
@section('title')
Oggi
@endsection
@section('content')
<div class="row">
<div class="col-md-12"></div>
<h4 class="display-2 fw-bold fst-italic text-info">
Oggi, {{ $currentDate->isoFormat('llll')}}
</h4>
</div>
</div>
<div class="d-flex flex-row bd-highlight mb-3 mt-4">
@foreach ($notes as $n )
<div class="p-1 bd-highlight">
<div class="card text-center text-dark " style="width:16rem; left:55rem; top:6rem">
<div class="card-header">
<h2>{{$n->day->name}}</h2>
</div>
<div class="card-body">
@if ($note)
<h3 class="card-title">{{$n->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$n->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$n->ora_inizio}} </p>
<p class="card-text">Ora fine: {{$n->ora_fine}} </p>
@else
<p>NULLA DA DIFFERENZIARE</p>
@endif
</div>
</div>
@endforeach
</div>
@endsection
But it doesn't function
hopefully you can spot why...
@if ($note)
<h3 class="card-title">{{$n->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$n->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$n->ora_inizio}} </p>
<p class="card-text">Ora fine: {{$n->ora_fine}} </p>
@else
<p>NULLA DA DIFFERENZIARE</p>
@endif
change $note for $n
I tried using $n but it still doesn't work.
what do you mean 'still doesn't work'?
can you tell me the error you are getting or show a screen grab?
I have cleared the entire calendar, but it still doesn't get the message that there is nothing to recycle.
your
@if ($note)
<h3 class="card-title">{{$n->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$n->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$n->ora_inizio}} </p>
<p class="card-text">Ora fine: {{$n->ora_fine}} </p>
@else
<p>NULLA DA DIFFERENZIARE</p>
@endif
only hides the card-title and card-text if there is no note.
you may find that NULLA DA DIFFERENZIARE is displaying but as you have not assigned any classes to it it may be hidden. inspect the DOM and you will likely see it.
move the if statement up if you need to, to exclude the date if thats what you want.
That bit is really basic laravel so you shouldn't really need my assistance to help you with that.
see https://laravel.com/docs/8.x/blade#if-statements
Please also take some time to watch the free lessons to educate yourself on laravel https://laracasts.com/series/laravel-8-from-scratch
The best way to learn is to read the documentation and watch the lessons.
I changed it this way, but it still doesn't display anything.
@if($n)
<h3 class="card-title">{{$n->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$n->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$n->ora_inizio}} </p>
<p class="card-text">Ora fine: {{$n->ora_fine}} </p>
@else
<div class="card text-center text-dark ">
<div class="card-header">
<h2>{{$n->day->name}}</h2>
</div>
<div class="card-body">
<h2 class="card-title">NULLA DA DIFFERENZIARE</h2>
</div>
</div>
@endif
view source on your browser check if it's there as it may be a styling error.
I used this other method, but no message is displayed.
@if($n)
<h3 class="card-title">{{$n->category->name}}</h3>
<p class="card-text">Giorno di raccolta: {{$n->collection->name}} </p>
<p class="card-text">Ora Inizio: {{$n->ora_inizio}} </p>
<p class="card-text">Ora fine: {{$n->ora_fine}} </p>
@else
<div class="card">
<div class="card-body">
<h2 class="card-title">NULLA DA DIFFERENZIARE.</h2>
</div>
</div>
@endif
have you viewed html in browser and inspected the code?
I tried to inspect the code in the browser but it's all regular.
Can you see NULLA DA DIFFERENZIARE if you view the page source?
No
I noticed that if I write inside the foreach loop it does not show me any messages, only the collection day.
Please or to participate in this conversation.