Laravel How can I configure "reserved_at" in the jobs table - laravel

I want to use Carbon::today()->addMonth() as default value of reserved_at column in jobs table
then I will use ite in the job in the way of
if(reserved_at == Carbon->today())
{
//execution
} else {
//don't
}

You can set reserved_at in model's creating event within the model boot() method, like that add following method in Job model:
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->reserved_at = Carbon::today()->addMonth();
});
}
It will append reserved_at field value in model when you storing data using eloquent methods.
Note: It'll not work with insert() query builder method.

I figured it out it's by using the function of the class InteractsWithQueue
->delay(Carbon::now()->addMinutes(5)) with the dispatch function

Related

How to set the default sorting column for lists?

I have a list of record.
Its model has this
protected static function booted()
{
static::addGlobalScope(new OrderByDescriptionScope);
}
this scope is doing
public function apply(Builder $builder, Model $model)
{
$builder->orderBy('descrizione', 'asc');
}
but when opening a list, records are never sorted by descrizione.
This is the suggested method of apply a global scope, as for laravel 8 documentation.
Why does my code not work?
How to set default sorting for laravel backpack?
using crud object, you can sort your recodes using orderBy method, for example:
public function setupListOperation()
{
$this->crud->orderBy('created_at', 'desc');
}
$collect_all = $collect_all->sortByDesc("id");
$collect_all->values()->all();
when you fetch data after order like this.

Storing Data and at the same time updating data on a different table in Laravel 8

I have two tables, 'vehicles' and 'assign_vehicles'. Whenever I assign a vehicle to a driver, I want the 'status' column on the 'vehicles' table to be updated to 'assigned'. How do I go about it?
There is some assumptions to be made. You will have to have classes that looks similar to this.
public class Vechicle extends Model {
public function assigned()
{
return $this->hasMany(AssignVehicle::class);
}
}
public class AssignVehicle {
public function vehicle()
{
return $this->belongsTo(Vehicle::class);
}
}
You can monitor the AssignVehicle event and add it to your boot method. Add it in AssignVehicle.php class. From there access the vehicle and update the status. Note there is a creating and created event, creating is before saving to the database and created is after saving to the database
public static function boot() {
parent::boot();
static::created(function (AssignVehicle $assignVehicle) {
$assignVehicle->vehicle->status = 'assigned';
$assignVehicle->vehicle->save();
});
}
For this to trigger remember to use standard Laravel approaches for events to trigger. The following will work.
AssignVehicle::create([
'vehicle_id' => $vehicle->id,
// your fields
])

Laravel: Accessing model attributes in the constructor method

How to use __construct on laravel eloquent and get attributes. I tired:
public function __construct() {
parent::__construct();
dd($this->attributes);
}
My code return null. But on abstract class Model filled all attributes:
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
It's possible get access to model attributes in the constructor method?
Try with accessors.
https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators
Define it like:
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
And use $this->first_namesomething like this.
I tested this locally by updating the constructor like this:
public function __construct(array $attributes = []) {
parent::__construct($attributes);
dd($this->getAttributes());
}
However, I've discovered that when fetching the object from the database, its attributes are not filled in the constructor, and therefore it's not possible to access them there.
What you can do is access the attributes after the object has been initialized:
$post = Post::find(1);
dump($post->getAttributes());
Not sure if that helps, but it is what it is.
Maybe Events or Observers can help you with what you need:
https://laravel.com/docs/5.8/eloquent#events
https://laravel.com/docs/5.8/eloquent#observers
It's impossible in constructor, because attributes list is empty.
But you can use Events and boot() method of model to achieve desired result:
class MyModel extends Model{
public static function boot(){
parent::boot();
self::retrieved(function ($model) {# Called after data loaded from db
dd($this->attributes); # now attributes was filled
});
}
}
Read more about another events here:
https://www.itsolutionstuff.com/post/laravel-model-events-tutorialexample.html

How to create custom model events laravel 5.1?

I want to create a custom model event in laravel 5.1.
For e.x. when an Articles category is updated i want to make an event and listen to it.
$article = Article::find($id);
$article->category_id = $request->input('category_id');
// fire an event here
You should use Eloquent Events (do not confuse with Laravel Events).
public function boot()
{
Article::updated(function ($user) {
// do some stuff here
});
}
You would want to look into Observers to make this more reusable and single-responsible, though a starting point would be something alike:
public function boot()
{
self::updated(function ($model) {
if (array_key_exists('category_id', $model->getDirty())) {
// Log things here
}
});
}
Laravel will populate a 'dirty' array which contains modified fields. You can detect when a certain field has changed using this.
You also have:
$model->getOriginal('field_name') // for this field value (originally)
$model->getOriginal() // for all original field values
You can use Attribute Events to fire an event when the category_id attribute changes:
class Article extends Model
{
protected $dispatchesEvents = [
'category_id:*' => ArticleCategoryChanged::class,
];
}

How to define a relationship that touches grandparents in eloquent

In Laravel, I would like to define a grandparent type eloquent relationship so that I can have the grandchild touch the grandparent when it is added or updated (for instance if it brings the grandparent a very nice birthday card). Say I have a hierarchy like Collection > post > comment.
--Comment--
protected $touches = array('post', 'collection');
public function post()
{
return $this->belongsTo('Post');
}
public function order()
{
// not working
// return $this->post->collection();
// not working
// return $this->belongsTo('Post')->belongsTo('Collection');
}
--Post--
public function collection()
{
return $this->belongsTo('Collection');
}
I want the update function in the Collection to run when a Comment is added. How can I set that up?
I'm using Laravel 4.1
I want the update function in the Collection to run when a Comment is
added.
I'm a bit confused about the update function but...
You can register an event handler for that, for example, add this in your Comment model:
class Comment extends Eloquent {
public static function boot()
{
parent::boot();
static::created(function($comment)
{
// $comment is the current Comment instance
});
}
}
So, whenever any Comment will be created the event handler (anonymous function) will run so you may do something in that function. Read more on Laravel Website.

Resources