I'm working on my first ever laravel project a crm. In my crm you can create appointments and packages. Now for the appointments I need to be able to select packages and set a relationship however I'm not sure I'm doing it correctly.
In my appointments model I have this in a function return $this->hasMany('App\Package');
and in my package model I have this in a function return $this->belongsTo('App\Appointment');
I'm having difficulty in understanding is when I'm creating a new appointment and I select a package how do I save that packages data(array) to the DB so that if I want a collection of appointments I can see what package was assigned to that appointment?
Should I be able to see the relationship from the DB ?
Eloquent provides convenient methods for adding new models to relationships.
you may use the associate method. This method will set the foreign key on the child model:
//...
$appointment = new App\Appointment(['column' => 'value']);
$package = App\Package::find(1);
$appointment->packages()->associate($package);
$appointment->save();
The way you describe it it looks like you have one-to-one relationship between Appointment and Package.
You can attach Package to Appointment using associate method on the relationship.
$appointment = Appointment::find(1);
$package = Package::find(1);
$appointment->package()->associate($package);
Belongs To Relationships
You Can Save Array Data in Relationship
$appointment->package()->createMany($package);
Related
As you see on the UML diagram, one or more users can be in charge of one project thats why i had to create user_projects table.
my situation i need create a controller that get a request that contain the project_id and return all the users that are in charge of that project.
My controller:
public function getUsers(Request $request){
$users_project = UserProject::where('project_id', $request->project_id)->get();
dd($users_project);
}
i managed to send the request with the project_id and get the rows from user_projects table that has the same project_id on the variant $users_poject, i am stuck now xD...
is it possible to do the task using laravel relationships?
hope someone can help finish this controller.
Assume we have one to many relationship like post and comment, each post hasMany comments and each comment belongsTo one post.
The question is how we could call associate method?
in Laravel document the associate method is called on relation method, something like:
$comment = new Comment(['title' => 'something']);
$post->comments()->associate($comment);
but in some cases, I saw the associate method is called on relation attribute like:
$comment = new Comment(['title' => 'something']);
$post->comments->associate($comment);
Are they the same? is there any difference between them?
Thanks
I don't think calling associate on a loaded relationship property works as it's either a database collection or a model (depends on the type of the relation). The method associate is only available on BelongsTo and MorphTo relationships.
is there any way to get the defined relationships in eloquent model. I have a situation where I need to get the model relationships so I can update all other eloquent models that relies on a specific id before delete it
There's no unified method to iterate over all registered relationships of a class. You can, however, access all the currently loaded relationships of a model instance (via the ->relations attribute or the getRelations() method), but that's not what you're up to. I'd suggest you take a look at laravel's documentation on inserting and updating relationships. So far that's the best laravel provides out of the box, the rest is developing approaches.
Try this function:
public function getRelations()
You can use
$model->getRelations()
function to get all relations
Also refer below link for details https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_getRelations
Which relation to use in Laravel to bind two table through third?
When Doctors can be assigned to some Centers. The intermediate table will be as:
doctor_id | center_id
How to create model in Laravel for this case?
You don't need a model for the intermediate table, simply use attach
Example:
$center = Center::create();
$doctor = Doctor::find(1);
$doctor->centers()->attach($doctor->id);
This is a very simple example but should give you the idea, of how to approach it.
All of it of course requires you have set up your Center and Doctor model with the correct many to many relations
Doctor.php model:
public function centers()
{
return $this->belongsToMany(Doctor::class);
}
See the documentation, for more information.
You could obviously create a model called DoctorsCenter and create it manually by doing this, whenever you want to attach a relation.
DoctorsCenter::create(['center_id' => $center->id, 'doctor_id' => $doctor->id]);
I don't see any good reason for doing this, and would not recommend it.
You can use hasMany or belongsTo relationship of Laravel.
See the laravel documentation, for more information
Laravel Version: 5.5
PHP Version: 7+
Database Driver & Version: mysql 5.7+
Scenario:
I have a SaaS application that has flexible database structure, so its fields are bound to change, especially given it has a Json field (for any extra database structure to be created from client side of the application), including relationship based fields. so Account Table can have dynamically created employee_id field, and thus the need to access relationships dynamically
Problem:
I need to EagerLoad models based on this dynamic relationship. If I had something like this:
// Account Model
public function employee(){
return $this->belongsTo(App\Employee);
}
it would be easy. But what I have is this:
public function modelBelongsTo(){
return $this->belongsTo($dynamicClassName, $dynamicForeignKey);
}
Now if I eager load this, I'll get Account Model instance with related Employee on key modelBelongsTo. This is how Eloquent Names based on the function of eagerload. But after this I cannot use this function again to eagerload a second model because it'll just overwrite results on modelBelongsTo key.
Possible Solution Directions:
1) Can I Somehow change laravel's process to use a name I provide?
or
2) Can I write functions on the fly to overcome this so I'll write employee function on the fly?
or
3) Worst Case Scenario: I iterate over all records to rename their keys individually because I am using a pagination, it wouldn't that big of a deal to loop over 10 records.
Us a morph relationship
define the various dynamic classnames say
Employee
Boss
Morph works by having the related key and the table name stored in the parent table, it means to relate them you have to use a join or an orm and you cant have foreign key constraint on it as it links to different tables.
then have your account have morphs where
we have
Account
as top class
then we have
EmployeeAccount, BossAccount
which have their relation to boss and employee
then in Account have morphto relation call it specificAccount()
to which in its child morphs have the morph relation to Account
then add it to $with so to eager load them so when fetching account you could simply do
$account ->specificAccount
to get its morph child. which is nullable
This is totally dynamic such that if you have other classes in future you can just add and add the morph relationship. This may be applied to any reflection or runtime evaluated and loaded classes/code though it is not advisable to do this, as you can always edit code to create new functionality without affecting previous.