I have relationship between User and Attendance, where in Attendance table I have field start and end which are date fields. I'm able to retrieve the data through relationship. I'm getting User Absences for current month and afterwards I want to add "additional" field/variable/attribute with array of this dates(parsed to Carbon) to User model. Is it possible?
I was trying to use booted() method with static::retrieved function, where I call another.
When I need to check those absences in view to display a Calendar I iterate too many times (previously I've created a method inside Users model to retrieve and parse those data).
I need to achieve something like this:
if(in_array($someDates, $user->additional_array)) [...]
Previously I've got:
if(in_array($someDates, $user->methodToReturnAdditionalArray())) [...]
Any idea?
You can use the Laravel Attributes and define the new method for getting the data you want
Example:
public function getAdditionalArrayAttribute()
{
// return the list of array items you want;
}
And then you can use it like here:
$model->additional_array;
Related
I have two tables:
Cards
Notes
Each Card has multiple Notes. So there is a relation between them like this:
class Card extends Model {
public function notes ()
{
return $this->hasMany(Note::class);
}
}
Ok well, all fine.
Now I need to understand the concept of these two lines:
$card()->$notes()->first();
and
$card()->$notes->first();
What's the difference between them? As you see in the first one $note() is a function and in the second one $note isn't a function. How will they be translated in PHP?
The first one points out to the card table and the second one points out to the notes table, right? or what? Anyway I've stuck to understand the concept of tham.
I don't know about $ before the $notes in your code but if you trying to say something like this.
1- $card->notes()->first();
2- $card->notes->first();
In the code in line 1, first you have a $card model and then you wanted to access all notes() related to that $card, and because of adding () after notes you simply call query builder on notes, show you can perform any other database query function after that, something like where, orderBy, groupBy, ... and any other complicated query on database.
But in the second one you actually get access to a collection of notes related to that $card, we can say that you get all related notes from database and set it into laravel collections and you are no more able to perform database query on notes.
Note: because laravel collections have some methods like where(), groupBy(), whereIn(), sort(), ... you can use them on the second one, but in that case you perform those methods on collections and not database, you already get all results from database
I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.
this has been driving me nuts...
I'm in my view I'm calling
Auth::user()->userlists
which brings me back an array of values like
{"id":"1","user_id":"1","name":"list1".....}
all I want to do is bring back the names I've tried all these;
Auth::user()->userlists->name
Auth::user()->userlists()->name
Auth::user()->userlists->name->get()
Auth::user()->userlists.name
But I always get an error such as "Undefined property"
How do I return this single property, it's in my array for all the items but I'm clearly just getting the syntax incorrect...?
The reason i'm trying to do this is that I need the values placed in a drop down box, it's finding the correct rows in the table but displaying all the data instead of just the name
Form::select('userlist_id', Auth::user()->userlists);
Many thanks.
You say a User has many Userlist models, so it's a collection not single model, thus you could work with it in a loop, BUT there is better way:
$lists = Auth::user()->userlists()->lists('name','id');
// To make it available in all the views, place this for example in a controller:
View::share('userlists', $lists);
This will fetch id and name for the related models collection and return it as an array, so it's just the one you will use in a Form builder:
Form::select('userlist_id', $lists)
My Symfony 1.4 application "Edit" menu very slow. When I click on edit link it takes almost 2 minutes to response.
I am using the following function in my module's action.class.php file.
public function executeNew(sfWebRequest $request) {
$this->form = $this->configuration->getForm();
$this->employee_attendance = $this->form->getObject();
}
There is a common pitfall when using automatically generated forms in Symfony. When your form has a field which is a foreign key of a related model then a <select> element is created for this element. All the possible values of the related values are fetched from the database and populated as objects. Then the __toString() method is used on each object to display a user friendly value on the list. If the implementation of this function uses another related object then the relation is read from the database for each object separately.
For example, if you have in your form a field for related object Shift and the __toString method in the Shift class refers to another model, let's say:
function __toString()
{
return sprintf(
'%s - %s',
$this->getShiftType()->getName(),
$this->getName()
);
}
Then the ShiftType will be fetched from the database one by one for each Shift. If your select lists several thousands shifts you will have the same amount of database queries run each time you open the form (not to mention resources needed to hydrate objects).
There are two things that can be done to solve the problem:
usually the related object is set in some other way than being chosen by the user so you can just skip the widget altogether. Something like unset($this['shift_id']); in your form's setup function.
If you do need the select use a specific table method where you will limit the number of elements retrieved from the DB and/or join with any relevant tables (the ShiftType in our example). You can add an option to the widget in your form:
$this->widgetSchema['shift_id']->addOption(
'tableMethod',
'yourFunctionRetrievingJoinedTables'
);
I'm not sure if I'm using the MVC architecture correctly so go easy on me if I'm going wrong.
I have a model Account that grabs a load of valuations from a Holding model. These 2 models are linked as Account hasMany Holdings.
In my Account model I have a function that retrieves the sum of the holdings from the account and arranges it in a $date=>$value type array.
I have another, separate, model called FxRate whose table is a load of FX rates and dates. In my Account model I want to retrieve an array of fx rates by running a find on the FxRate table so I can convert the valuations array I retrieved earlier.
FxRate is not linked to either of the other models and I can't seem to find any data from it from within my Account model. What's the correct way to achieve this?
If you are in your Model file you can use this:
App::uses('FxRate', 'Model');
$FxRate = ClassRegistry::init('FxRate') // and not $FxRate = new FxRate(), see comments;
$FxRate->find('all');
in your controller instead you can use loadModel
$this->loadModel('FxRate');
$this->FxRate->find('all');