Good morning guys, i need to have a custom date that always has the current date but a certain time to be able to compare two dates and get the remaining time. At the moment something like this occurred to me:
$day=date("d");
$dayy= now();
$datelim= Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-',$day, '22:00:02');
return view('fen.index', [
'works' => Period::select('paperiod.descriptionp','pamatrixinfoperio.description', 'pamatrixinfoperio.codpar')
->join('pamatrixinfoperio', 'pamatrixinfoperio.cod_paperiod', '=', 'paperiod.cod')
->where('pamatrixinfoperio.read_at', '=', 0, 'AND')
->where('pamatrixinfoperio.cod_paperiod', '=', 1)
->get(),
'Periodres' => $fechalim
]);
but I don't know how to concatenate that variable day in the carbon '2020-05-',$day, '22:00:02' , How could I use it because my objective is that the variable always has the current day, Monday, Tuesday, Wednesday, but that the time is always the same.
On the other hand, I don't know if they could help me. How can I compare dayy and datelim? So in the view I can have for example: "The work is about to end in 3 days, 3 hours and 20 seconds" I hope you can help me, because I find more complex examples in carbon but not basic and I don't know how to do this :(
my view is this:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">WorksF</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
Welcome {{Auth::user()->name}}, Lasis informs you that the following files are about to end in:
#foreach($works as $work)
<li class="list-group-item">
{{$work->description}}
you have the next time to deliver the report:
<b>{{$Periodres}}</b>
</li>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
As you can see the view is not finished but I am looking for something similar, Thanks in advance for the help.
Concatenation in PHP use . not , and you need a space between $day and the hour.
Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-' . $day . ' 22:00:02');
Of course Carbon::create() as suggested by Nacho is appropriate, but still Carbon::createFromFormat() works also fine if the given input is given as 1 single string and matches the format.
I think in your case you can use:
Carbon::create($year, $month, $day, $hour, $minute, $second)
Related
When I try to echo and display the output of a query in my dashboard it is appearing like this
[{"job_type":"Sales Manager"}]
The query is this:
->where('id',$userId)
->select('job_type')
->get();
To display it on the dashboard I am using this code
<div class="col-md-6 col-lg-2 col-xlg-3">
<div class="card card-hover">
<div class="box bg-cyan text-center">
<h1 class="font-light text-white"><i class="mdi mdi-view-dashboard"></i></h1>
<h5 class="m-b-0 m-t-5 text-white">{{ $jobType }}</h5>
<h6 class="text-white">Designation</h6>
</div>
</div>
</div>
How do I just get the result instead of the variable name along with the brackets?
You can use this query
->where('id',$userId)
->pluck('job_type')
->first();
You should print it like {{$jobType->job_type}} or {{$jobType['job_type']}} based on your return type.
->where('id',$userId)
->select('job_type')
->get();
This code will return Collection instance so you should either use first(); or you iterate over the variable. You probably try to get the user so you should use it like this:
$user = User::where('id', $userId)->first();
and use below code in your view
{{ $user->job_type }}
Or you can use
$user = User::find($userId);
I've got index page on which I'm displaying events.
#foreach($events as $event)
<div class="row">
<div class="d-flex justify-content-center" style="margin: auto">
<a href="{!! route('eventView', ['id' => $event->id]) !!}" style="text-decoration: none;">
<div class="row" style="margin-top: 10 px">
<p style="display:block">{{$event->name}}</p>
</div>
#if($event->photo_patch)
<img src="{{ asset('storage/' . $event->photo_patch) }}">
#else
<img src="placeholder" alt="...">
#endif
</a>
</div>
</div>
</div>
#endforeach
But the problem is that its displaying all events, even those which took place in the past.
I would like to display events only with today's or future date
So I'm stuck now and I dont know what to do next.
I tried to do loop which shows me events that are today/in the future:
public function index()
{
$date = Carbon::today();
$events = Event::where('date_of_event' >= $date)->get();
foreach($events as $event){
if($event->date_of_event >= $date){
dump('Valid');
}
else{
dump('Invalid');
}
}
dump($events);
dump($date);
return view('frontend/index',['events'=>$events]);
}
But I dont know how to display them later inside of my view.
My second shot was to do it with this line:
$events = Event::where('date_of_event' >= $date)->get();
But then I've got error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from `events` where `1` is null)
which isnt really accurate since column name should be valid.
How can I implement that properly?
The default operator for where is =. If you want to change this you'll use the second argument for the operator and the third for your value.
Event::where('date_of_event', '>=', $date)->get();
See Where Clauses for more details.
To make that code short you may use this code
Event::whereDateOfEvent('>=', $date)->get();
#foreach($documentos as $documento)
<ul class="mail-list">
<li class="mail-item unread">
<div class="m-chck"><label class="px-single"><input type="checkbox" name="" value="" class="px"><span class="lbl"></span></label></div>
<div class="m-star"> </div>
<div class="m-from">{{$documento->assunto}} </div>
<div class="m-subject"><span class="label label-danger">Social</span> Reset your account password</div>
<div class="m-date">3:25 PM</div>
</li>
</ul>
#endforeach
I need show the date from BD... I need show the date from BD, cause iam using pixelAdmin
Carbon::now()->format('d-m-Y') or you can change the format to however you need
I would suggest you do this in the controller and pass it to the view
Laravel has a Carbon dependency attached to it but you can find the Carbon doc and see how it works
you can use Carbon for formatting timestamp. In laravel migration file you will find 2 default timestamp columns. one is created_at and another is updated_at. If you want to show the time when the document is created you can use $documento->created_at with Carbon for readable format.
Here is an example,
{{ \Carbon\Carbon::parse($documento->created_at)->toFormattedDateString() }}
The output will be similar to Dec 25, 1975.
I am brand new to Laravel and I'm running Version 6.
I want my view to display a button if one of my MySQL tables has rows that meet a specific condition but I'm having trouble figuring out how to code it - and even WHERE to code it - within my Laravel application.
My MySQL table is called diary_entries and various users of the system will contribute zero to n rows to it. Each row of the table contains a user id called client. When a given user goes to the Welcome view, I want the view to determine if that user currently has any rows in the diary_entries table. If he does, I want to display a button that will take him to another page where the entries can be displayed or edited or deleted.
I think I want to construct an #if that counts the number of records for that user; if the count is greater than zero, I want to display the button, otherwise the button is not displayed.
The problem is that I can't figure out how to code this. I've looked at the examples in the Eloquent section of the manual but they aren't particularly clear to me. I found a note near the top that said the count() function expects a Collection as an argument and that the result of an Eloquent statement is always a Collection so I guessed that I just have to execute an Eloquent query, then apply count() to the resulting Collection. But every variation of that idea which I've tried has thrown exceptions.
Here was the guess that seemed most logical to me:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1> Sleep Diary </h1>
</div>
<div>
<h3>{{Auth::user()->name }}</h3>
</div>
<div>
#if (count(App\DiaryEntry::select('*')->where('client', Auth::user()->name) > 0))
<p>
<a class="btn btn-primary"> View / edit existing sleep diary entries </a>
</p>
#endif
</div>
<div>
<p>
<a class="btn btn-primary" href="/diaryEntries"> Create a new sleep diary entry </a>
</div>
</div>
#endsection
This is obviously wrong because it throws an exception so how do I make it right? Does the building of the collection have to move into the Controller? If so, how do I invoke the method and see its result? Or can I do something like I have already done but just adjust the syntax a bit?
EDIT
I've imitated Sehdev's suggestion but I get this error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
Here is my welcome view:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1>Sleep Diary</h1>
</div>
<div>
<h3>{{ Auth::user()->name }}</h3>
</div>
<div>
#if ($count) > 0))
<p>
<a class="btn btn-primary">View/edit existing sleep diary entries</a>
</p>
#endif
</div>
<div>
<p><a class="btn btn-primary" href="/diaryEntries">Create a new sleep diary entry</a>
</div>
</div>
#endsection
And this is the relevant function from DiaryEntryController:
public function countEntriesOneUser()
{
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count();
view("welcome", compact("count"));
}
Should the compact function be returning $count instead of count? I can't find the compact function in the manual with the search function so I'm not clear what it does or what the proper syntax is. I just tried changing the last line of the function to
view("welcome", $count);
but that produced the same error.
Try this,
#php
$count=\App\DiaryEntry::where('client', Auth::user()->name)->count();
#endphp
#if($count>1)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
Using App\DiaryEntry::select('*')->where('client', Auth::user()->name) directly on your blade template is a bad practise.
You can execute your question in your controllers method and then pass the result on your view file
Your function
function test(){
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count(); // get the total no of records using count
view("index", compact("count")) // pass your count variable here
}
then you can directly use $count in your #if condition
Your blade template
<div>
#if ($count > 0)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
</div>
How to get all articles for current year?
Articles::all()->where('created_at',$current_year);
You can do this:
Articles::whereBetween('created_at', [
Carbon::now()->startOfYear(),
Carbon::now()->endOfYear(),
]);
Another way is to use whereYear() as in #xhulio answer. But I'd recommend you to use this code instead as it's more readable:
Articles::whereYear('created_at', date('Y'))->get();
Try the following query
Articles::whereYear('created_at', '=', Carbon::parse($date)->format('Y'))->get();
Laravel has the helper functions whereDate, whereDay, whereMonth and whereYear to better deal with datetime queries.
Get Data via carbon in single line
Articles::whereYear('created_at', Carbon::now()->year)->get();
i found this way to collect all years only is good
#if(isset($years) && $years != '')
#foreach($years as $yrs)
#php($year[] = $yrs->created_at)
#endforeach
#if(isset($year))
#php( $collection = collect($year)->unique()->values()->all())
#foreach($collection as $created)
#php($year_only[] = $created->format('Y'))
#endforeach
#php( $collections = collect($year_only)->unique()->values()->all())
#foreach($collections as $created_year)
#if($created != '')
<div class="lab-item">
<div class="lab-inner" style="border: #ee3725 2px solid;border-radius: 25px;background: bisque;">
<div class="lab-content">
<h4> {{ $created_year }} </h4>
</div>
</div>
</div>
#endif
#endforeach
#endif
#endif