Laravel- How to check difference between two time stamps using Carbon - laravel

I'm currently working in laravel 5.4 and I need to calculate difference between two available timestamps using Carbon Class. But I'm getting this error Call to a member function diffInHours() on string any insights from you people would be helpful thank you :)
User Controller
class UserController extends Controller
{
use EncryptDecrypt;
public function resetPassword($token)
{
$decryptTS = trim($this->decryptText($token));
$split = explode('-', $decryptTS, 2);
$userId = $split[0];
$timeStamp = $split[1];
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp)->toDateTimeString();
$now = Carbon::now();
if($timeStamp1->diffInHours($now) <=24)
{
echo "valid URL";
}
else
{
echo "Invalid URL";
}
}
}

Try to replace this line :
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp)->toDateTimeString();
with :
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp);
this first one will return string not a carbon object

Related

Laravel 5 date Format value

I would like to know why my code is not working.
I have my model Organigramme i want to calculate with a function the age between two dates:
public function getAncienneteAttribute()
{
$now = Carbon::now();
$date_dentree = Carbon::createFromFormat('d/m/Y', $this->date_dentree);
return $now->diffInYears($date_dentree);
}
But I'm faced this error: Data missing
Someone can help me please.
Your $this->date_dentree format is not matching the format you passed to Carbon::createFromFormat, it is probably the default format so try the code below:
public function getAncienneteAttribute()
{
$now = Carbon::now();
return $now->diffInYears(new Carbon($this->date_dentree));
}
Try this
$date_dentree = \Carbon\Carbon::createFromFormat('Y-m-d', $this->date_dentree);
$diffYears = \Carbon\Carbon::now()->diffInYears($date_dentree );
//\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $request->date)->format('Y-m-d')
$dbDate = \Carbon\Carbon::parse('2020-05-10');
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);

how can i get language value randomly in laravel controller?

class DynamicDependent extends Controller
{
function fetch(Request $request)
{
$value = "home";
$value2 = Lang::get('home.'.$value.'');
}
}
output :'home.home'.
But i need value from language file.
please guide me to get this.
It seems like you are trying to get a translation. For that you can use the trans helper method like this:
//In your resources/lang/{some_lang_code}/home.php
return [
'home' => 'My translation',
];
//In your controller
$value = "home";
$value2 = trans('home.'.$value); //My translation

Laravel Mutators and Accessors

I have created mutator date function in model to convert the created_at date into human readable time using diffForHumans().
I have done the following
public function setDateAttribute($value){
return \Carbon\Carbon::parse($value)->diffForHumans();
}
It works fine but it affects in all. it is possible to apply this mutator only on the specified function of the controller rather than all function
A small logic in the mutator would do the job:-
public function setDateAttribute($value){
if ( request()->path() === "/yourURL/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
the idea is to check by the url.
getting request path helper
EDIT
Comparison using route name is better, as exact path can contain id/slugs.
public function setDateAttribute($value){
if ( \Route::currentRouteName() === "/yourRouteName/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
getting route name

How to achieve this on laravel 5 eloquent

How can i achieve something like this?
public function getInformation($model) {
$result = $model::with(['province', 'city']);
if($model == 'App\Models\Business') {
$result->with(['businessProvince', 'businessCity']);
}
$result->get();
}
// call the function
$information->getInformation(\App\Models\Business::class);
i'm getting error
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
on the sample code above. Any suggestion is really appreciated.
After taking a fourth look $model should be a string, and $result is an Eloquent Builder instance and never an instance of the model class (since a query was started when with was called).
So the $model == 'App\Models\Business' I would change to $model === \App\Models\Business::class but that should not change the outcome.
Are you sure this error comes from this part of the application? Which line specifically?
Original wrong answer.
You are trying to compare the model instance with a string (since $model::with() created a instance of the model class you passed in the $model argument).
You can use the instanceof keyword for comparing an instance with a class name (http://php.net/manual/en/language.operators.type.php).
if($model instanceof \App\Models\Business) {
$result->with(['businessProvince', 'businessCity']);
}
This solved my problem, thank you guys.
public function getInformation($model) {
$result = $model::with(['province', 'city']);
if($model == 'App\Models\Business') {
// my mistake
//$result->with(['businessProvince', 'businessCity']);
$result = $result->with(['businessProvince', 'businessCity']);
}
$result->get();
}

Laravel Eloquent belongsTo: "Trying to get property of non-object" when using without echo/die/var_dump

I've just started with Laravel and Eloquent.
In my "Receipt"-model, I try to get information about a relationship.
As long as I'm using var_dump, I get my information.
As soon as I'm just returning the value to use it in a blade view, I get my "Trying to get property of non-object".
<?php
class Receipt extends Eloquent {
public function businesspartner() {
return $this->belongsTo('Businesspartner', 'f_businesspartner_id');
}
public function brand() {
return $this->belongsTo('Brand', 'f_brand_id');
}
public function invoice() {
return $this->belongsTo('Invoice', 'f_invoice_id');
}
public function name() {
$name = '';
$bp = $this->businesspartner()->first();
$name = $bp->salutation; // line 21
$name .= ' ';
$name .= $bp->lastname_company;
return $name;
}
}
The error occurs in line 21.
When I now put a
var_dump($name);die();
before returning, it prints me my name.
What's going wrong here? :(
Regards,
Timo
SOLUTION
As #Jarek Tkaczyk wrote, I was calling the name() method in a loop.
When doing my debug outputs, I got data from my first row.
When getting the error, the code was already processing the second row, where the foreign key was null.
Wrapping the problematic part in if(count($this->businesspartner)) helped.
For more information about that, Jarek linked that post: https://stackoverflow.com/a/23911985/784588

Resources