Adding repetitive actions to the create method - laravel

How can I make repetitive actions available each time I call the create method on a model. It so happens like each time I write into the database, where each table has a column of createdby - so I just want to write in one place that this column should take on the username in the session!
Thanks in advance

There are several events in Eloquent you can hook into.
It sounds like you're after the creatingevent.
User::creating(function($user) {
$user->created_by = Auth::user()->name;
});

Related

Run a method or command when a row is expired Laravel

I have a table which has user publications, when the user publish a publications he choose a date that this publication will remains visible in the platform, i want a way to flip the state of a publication that time is come to now be shown anymore (date of publication is equal or greater then date system), i know about the cronjob way but i wanted to ask here if there's a better way to do this because i think the cronjob way is not good for this i will have to run a command every minute which i prefer not to do, i only need ideas not code.
Thank you.
Do not store an active flag on the database. In your case active is a derived attribute and derived attributes should not really be stored as columns, not to mention that since active will be based on a date it is not functionally dependant on the primary key directly so is also in violation of the 2nd normal form. These two reasons will generally lead to data anomalies and what you're trying to do with a scheduled task is basically hack your way around those anomalies.
My suggested approach is to use an eloquent model attribute and attribute mutators like below:
class Publication extends Model {
protected $appends = [ 'active' ];
public function getActiveAttribute() {
return $this->activeUntil < Carbon::now(); //Still active
}
}
This way your model includes the active attribute which is computed by eloquent and not stored in the database.

servicenow getting value from extended table

Good Evening, I am sure this is a very simple question. But I am having no luck finding a correct answer.
I have onLoad client script on a request form. The form has a variable that I need to auto populate, the value is on a sys_user extended table.
I am not sure how to pull that value from the sys_user extended table.
I know to use a GlideRecord to get the current user on the sys_user, but after that is were I am having the issue.
Thanks for any help or suggestions you may have.
James
As you mentioned, GlideRecord should be what you need to use. The exact script will vary based off of what field you are trying to query on your extended table.
var gr = new GlideRecord("extended_table_name");
gr.addQuery("field_name", "query");
gr.query();
if (gr.next()) {//action you want to take}
You can add as many addQuery() methods as needed to build the query as specific as you require. The query() method then runs the query and return a new GlideRecord object. The .next() method checks to see if there is another record in the GlideRecord object and advances to the next record if so. After running the script above, you can access any properties on the GlideRecord you may need by simply dotwalking to them. i.e:
sys_id = gr.sys_id;
name = gr.getDisplayValue();

Is it possible and how to perform a join like operation which relies on the results of another observer

Sorry but I am very new to RxJS but I am getting the hang of it slowly but one question I just cant seem to get an answer for this could be the way I am wording the question but here goes...
Lets say I have a table called users, this table is keyed via the users unique pushed key (so it unique!). I create a ref to it i.e '/users/' + id, and as expected I get my record for the user which contains a field called 'nurseryId', great just what I wanted!
The problem is and this is where it may be my understanding of RxJS, but I want to use the 'nurseryId' from within the 'users' record but to do this I need to create another ref to the '/nurseries/' + nurseryId table, how can I do this from within the users observable? Is it even possible?
I dont want to combine the two pieces of information and I want to create a route resolver but I need the results from the first fetch before I can start the second fetch???
Many thanks in advance!
You have to subscribe to the 'user' ObjectObservable.
userObject.subscribe(userSnapshot=>{
//query here with userSnapshot.nurseryId as parameter
}

Laravel - minor data processing after getting Eloquent model instance

I'm just beginning to learn Laravel and the MVC way of thinking, so please bear with me. How can I approach this, and is there a standard in where to put the processed data?
I would fetch for example, a department_id (e.g. ABC) and personnel_number (e.g. 1234) from the database and I want to piece them together as one (e.g. ABC1234). So I want to prepare an object property code after an Eloquent Personnel is prepared.
Should I just alter the Eloquent object after it is created like:
$personnel = App\Personnel::find(1);
$personnel->code = $personnel->department_id . $personnel->personnel_number;
//or
$personnel->code(); //returns ABC1234
Or should I process the data in the Personnel model and fit everything into a new object? Like:
$personnel_data = new PersonnelData(App\Personnel::find(1));
//so I can access the personnel code using this, which is processed in constructor
$personnel_data->code;
//and access the model using this:
$personnel_data->model;
Or some other way?
There must be some general practice I can follow, because there are times when this is needed, e.g.:
site URL when you only store a part of it, e.g. Google Drive file URL when you only have the file ids
human readable time when you only store the timestamps
person's full name when you store their first name and last name separately
...
Is there a common/standard way to prepare these beforehand and not process them only when you need them?
If you just want a property that is a concatenated string of other properties on the same model, then overload the attribute in the model:
public function getCodeAttribute() {
return $this->department_id . $personnel->personnel_number;
}
Then you can just call $personnel->code

Laravel : Delete all records that are not in array

I have a datepicker calendar in my views, and basically I need to synchronize the selected dates (that I send in ajax) with a BoxDeliveryDate model (which only has one column named "date").
So in my Controller I was able to write a pretty nice method to only create a new record if one the selected dates is not yet stored in the database, like this :
foreach (Request::get('dates') as $date) {
$date_formated = date('Y-m-d', strtotime($date));
BoxDeliveryDate::firstOrCreate(['date'=>$date_formated]);
}
Now, if the user de-select one the dates in the datepicker, later, and synchronize, I need to delete it from the database.
Is there a nice way to do that in Laravel ? In other words, to delete every record of the table that are NOT in my Request::get('dates') ?
Also, I searched for a simple way to synchronize everything with only one method, but couldn't find anything.
Thanks for helping !
You can use whereNotIn() for that:
BoxDeliveryDate::whereNotIn('date', Request::get('dates'))->delete();
Note that this will not trigger any model events nor will it work with soft delete. Also, depending on the format of dates you might have to format the array before passing it to whereNotIn().
Also I believe it should be Request::input('dates') but it's possible that both actually works...
I would highly recommend using the soft delete trait (built into Laravel) if your doing mass deletes!
http://laravel.com/docs/4.2/eloquent#soft-deleting
$this->model->whereNotIn('id', $ids)->delete();

Resources