L5 Unable to store Jquery datepicker value to database - laravel

I am using L5 with eloquent. In my model i defined below function for necessary database table field.
public function getBuyingDateAttribute($value)
{
return Carbon::parse($value)->format('d/m/Y');
}
Datepicker
$( "#buying-date" ).datepicker({dateFormat: 'dd-mm-yy'});
I am able to display a current value with this format in my form(usingform model binding) but when i try to store or update the field the value comes null. I am a newbie with Laravel and Eloquent. How can i format and store the data properly ? Anyhelp would be appreciated.

if in your form the date column name is date then use the following in your controller
$date = date('d/m/Y',strtotime($request->date));
//here $request is the object of Request class

Related

Laravel mutators on specific situations

As I understand, mutators change your attribute on save function (create or update). Can I make mutator just for atrribute display to users, but leave default values on save function ?
In Eloquent Model when you do this:
public function getSomeNameAttribute($value)
{
return ucfirst($value);
}
That ^ will mutate the value for showing it later in views and it won't affect what is stored in DB table.
And also if you use a setter setSomeNameAttribute($value) you can pull the actual value from db table with:
$model->getOriginal('attribute_name'); // Laravel V. ^5.*
or
$model->getRawOriginal('attribute_name'); // Laravel V. ^8.*

Laravel Livewire Date Cast and Validation

I have a form with a modal and one of the fields in the modal form is a date field. It is cast in the Model as:
'date_last_contact' => 'date:m/d/Y'
In the $rules section of the livewire file it is set as:
'editing.date_last_contact' => 'date|nullable',
The issue is if I someone inputs a non-date, non-null value in the field and tries to save, it throws an error because it is not validating…
Carbon\Exceptions\InvalidFormatException
Could not parse ‘adff’: DateTime::__construct(): Failed to parse time string (adff) at position 0 (a): The timezone could not be found in the database
The Save function in the livewire file looks like this:
public function save()
{
$this->validate();
$this->editing->save();
$this->showEditModal = false;
}
What it seems is happening it is trying to CAST it to a date before the validation is happening. How can this be prevented?
Versions:
Laravel: 8.24.0
Livewire: 2.3.8
I'd use date format over date, enforce the structure on the BE, as well as add some front end validation to help hold the user's hand
https://laravel.com/docs/9.x/validation#rule-date-format
Try casting the date field before submiting the form.

How can I get the data inside my notifications from within my Controller?

I'm trying to get the numbers of records from my notifications, where the candidate_user_id column from inside the data attribute is the same as the UserId (Authenticated User).
After I dd, I was able to get the data from all of the records in the table by using the pluck method (Line 1). I then tried to use the Where clause to get the items that I need
but this just didn't work, it was still returning all of the records in the table.
DashboardController.php:
public function index()
{
$notifications = Notification::all()->pluck('data');
$notifications->where('candidate_user_id', Auth::user()->id);
dd($notifications);
}
Here is a partial screenshot of the data that is being plucked.
How can I get the data from this, in a way like this ->where('candidate_user_id', Auth::user()->id);?
If data was a JSON field on the table you could try to use a where condition to search the JSON using the -> operator:
Notification::where('data->candidate_user_id', Auth::id())->pluck('data');
Assuming you only want this data field and not the rest of the fields, you can call pluck on the builder directly. There isn't much reason to hydrate Model instances with all the fields to then just pluck a single field from them if it is just a table field, so you can ask the database for just the field you want.
The data in the data field is a json string, so you can tell Laravel to automatically cast it as an array using the $casts property on each of the models that is notifiable.
For instance, if you have a User model which uses the trait (ie has use Notifiable), add this:
protected $casts = [
'data' => 'array',
];
If you want to access all notifications for the auth user.
$user = auth()->user();
dd($user->notifications->pluck('data'));
If you really want to do in your question way, here is how.
$notifications = Notification::all()->pluck('data');
$notifications = $notifications->where('candidate_user_id', Auth::user()->id)
->all();
This assumes you that you did not modify the default laravel notifications relationship and database migration setup. If you have modified some of the default ones, you need to provide how you modify it.

What type of class of datetime fields in laravel

When I fetch data from database using Laravel, I got some timestamp field like 'created_at', 'updated_at',....
What type of class for these fields, what type of class which return true of expression $created_at instanceof DatetimeClass
Thanks
Recent Laravel uses (Carbon library v2)
for that type of data (Illuminate\Support\Carbon).
As for ... instanceof DatetimeClass part of your question, I am not sure what really you refer to as DatetimeClass class, but I assume that you meant built-in DateTime class. If that's so, you can get DateTime from Carbon object by calling toDateTime(), i.e.:
$dt = $model->created_at->toDateTime();
Alternatively, edit your ... instanceof ... uses to test against Carbon.
Please also have a look at date mutators in Laravel. Laravel will automatically cast a timestamp(created_at, updated_at) or when you need to define custom date fields in the db you can define these in the model: https://laravel.com/docs/7.x/eloquent-mutators#date-mutators
If you do not want to automatically cast you can parse a string representation of a date using $carbonObject = Carbon::parse('2020-02-03');

How to use carbon or map function to format created_at?

I make Table CRUD in admin control panel, I will change format of date(column created_at)in Controller with map function with collection OR carbon in table Admin in Database,
I need the date like this (jan 2 , 2017)
You can use toFormattedDateString in carbon:
$user->created_at->toFormattedDateString();
also you can change format from model
In your model add an accessor method like this:
public function getCreatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Ym-d');
}

Resources