eloquent

Laravel: Create HasMany Relationship Based On Condition

妖精的绣舞 提交于 2021-01-29 19:45:20
问题 I need to display reproductions based on Gender Condition Reproduction Table: $table->bigIncrements('id'); $table->unsignedInteger('father_id'); $table->unsignedInteger('mother_id'); ... I need to retrieve reproductions according to the gender User.php public function reproductions(){ if($this->gender == 'm'){ return $this->hasMany(Reproduction::class, 'father_id'); }else{ return $this->hasMany(Reproduction::class, 'mother_id'); } } Reproduction Table: id father_id mother_id 1 1 2 2 1 3 3 1 4

Duplicate Entry when using firstOrCreate on Laravel?

╄→尐↘猪︶ㄣ 提交于 2021-01-29 15:08:47
问题 This is my Controller foreach ($dataOrder['items'] as $rowJubelio) { DataOrderDetailEcommerce::firstOrCreate([ 'order_id' => DataOrderEcommerce::firstOrCreate([ 'orderid'=> $invoice, 'cust_name'=> ucwords($cust_name), 'province'=> $province, 'city'=> $city, 'district'=> $area, 'zipcode'=> $zipcode, 'phone'=>$phone, 'fleet_id'=> $fleet, 'ecommerce_id'=> $ecommerce, 'date_add'=>date('Y-m-d'), 'no_resi'=> $no_resi, 'email'=> $email, 'order_time'=> $transactionDate ])->orderid, 'sku_id'=>

Laravel Model creation override

可紊 提交于 2021-01-29 13:48:22
问题 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

Many to Many to MorphToMany Relationships

自古美人都是妖i 提交于 2021-01-29 13:42:47
问题 I have an Exam , Question and Tag (Spatie/laravel-tags package) models. An Exam consists of many Questions (Many to Many), and Question has many Tags (MorphToMany). I would like to have a method on the Exam model to get all the tags of Exam through its associated questions, such that $exams→tags() returns all the tags from associated questions belonging to the exam. Can anyone point me towards what may be the best course to take in order to achieve this? 回答1: If you have proper described

Eloquent: use a modificator in WhereIn

五迷三道 提交于 2021-01-29 11:10:48
问题 in my code i've create a modificator and generate the column chiave_composita. If i run this code i've obtain the data TransazioniOrigine::all()->pluck('chiave_composita')->toArray() but if i use this code $origine = TransazioniOrigine::all()->pluck('chiave_composita')->toArray(); $destinazione = Transazioni::all()->pluck('transazioneKey')->toArray(); $diff = array_diff($origine, $destinazione); dd(TransazioniOrigine::whereIn('chiave_composita', $diff)->get()); i have this errror: Il nome di

Laravel relationship based on custom attribute not working both directions

て烟熏妆下的殇ゞ 提交于 2021-01-29 10:53:33
问题 Models Product and Category. Product model has this custom attribute: getCategoryIdAttribute() . because the way to get category id is a bit complex. With that attribute I can define the relationship (Product -> Category): public function category() { return $this->belongsTo(Category::class, 'category_id', 'id'); } This relationship works fine! But now on the Category model: public function products() { return $this->hasMany(Product::class, 'category_id', 'id'); } If I use this relationship

How to group and count record by date using Laravel?

北慕城南 提交于 2021-01-29 10:41:53
问题 I have an issues table, it has a column created_at , and status which can be 'Open' or 'Closed' I need to get the count of issues depending on the status per month. I want to get something like this: January 2018: { open: 300, closed: 28, }, February 2018: { open: 250, closed: 80 } I don't know if there's a better approach, I'm open to suggestion in how I would retrieve the data. Basically I'm creating an admin dashboard and want to display the data in a chart. 回答1: Assuming you are using

Laravel hasone relationship

与世无争的帅哥 提交于 2021-01-29 09:16:20
问题 I am trying to use hasone relation but getting null error. below is my code User Model: function profile() { $this->hasOne('App\Profile'); } Profile Model: function User() { return $this->belongsTo('App\User'); } Register Controller protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); if($user) { $profile = new Profile(); $profile->name = 'Fake Name'; $profile->father = 'Fake Father

Laravel 5 relationship using a 'JSON' column

僤鯓⒐⒋嵵緔 提交于 2021-01-29 09:13:34
问题 Is it possible to use Eloquent to create a relationship in Laravel 5 where the foreign key exists in a field in a JSON column? If it is, how so? Example: I have a table called chats with a participantIds column, of a JSON datatype. The JSON format of the data looks like this: {"creator": "1", "recipient": "2"} I want to join the users table using those fields to get the participants of the Chats. How do I do that? 回答1: Laravel has no native support for JSON relationships. I created a package

Laravel hasone relationship

久未见 提交于 2021-01-29 09:09:36
问题 I am trying to use hasone relation but getting null error. below is my code User Model: function profile() { $this->hasOne('App\Profile'); } Profile Model: function User() { return $this->belongsTo('App\User'); } Register Controller protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); if($user) { $profile = new Profile(); $profile->name = 'Fake Name'; $profile->father = 'Fake Father