Categories: php, laravel, eloquent

Laravel Model creation override

1 answer

In my Laravel project, I have different user types that all have a base User model. Here's a brief idea of the database topology:

users:  - id  - name  - email  students:  - age  - user_id  teachers:  - budget  - user_id  employee:  - is_admin  - user_id 

In my case, each of Student, Teacher, Employee has their own User. But if I want to make a new Student for example, I have to make both a Student and User model. I'm aware of the Observers pattern in Laravel which can make this job easier, but I'd like to be able to programatically make Student models like the following:

$student = App\Student::create(['name' => 'Joe', 'email' => 'joe@example.net', 'age' => '20']); 

The problem gets even more complex from there, because Teacher models are also required to have both an Employee model and a User model. Is there a way to override the create method on my model so that I can pass in User creation parameters, but also Student parameters?

All answers to this question, which has the identifier 61070392

The best answer:

You can override the create method and do this yourself. In your Student model class add this:

public static function create($arr) {     $user = User::create([         'name' => $arr['name'],         'email' => $arr['email']     ]);      $student = parent::create([         'age' => $arr['age']         'user_id' => $user->id     ]);      return $student; } 

You can do other methods in a similar way.

If you Laravel version is above 5.4.* do this instead:

public static function create($arr) {     $user = User::create([         'name' => $arr['name'],         'email' => $arr['email']     ]);      $student = static::query()->create([          'age' => $arr['age']          'user_id' => $user->id     ]);      return $student; } 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data