logo
down
shadow

How to configure Laravel model attributes in a trait?


How to configure Laravel model attributes in a trait?

By : tbos
Date : November 17 2020, 01:00 AM
With these it helps You could override the method where $this->hidden is actually used. And that's getArrayableItems
code :
trait Multilingual {

    protected function getArrayableItems(array $values)
    {
        if(!in_array('translation_of', $this->hidden)){
            $this->hidden[] = 'translation_of';
        }
        return parent::getArrayableItems($values);
    }
}


Share : facebook icon twitter icon
LARAVEL how to change $fillable in Model from trait?

LARAVEL how to change $fillable in Model from trait?


By : Dasun
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have in model: , You should be able to use the boot method of Traits like so
code :
public static function bootSeoTrait()
{
    $this->fillable[] = 'seoMeta';
}
Scoping Queries within Trait on a Laravel Model

Scoping Queries within Trait on a Laravel Model


By : Perry Joshi-Godrez
Date : March 29 2020, 07:55 AM
I hope this helps you . A Little Background… , This is how it should be done actually
code :
trait Locatable {

  /**
   * Scope a query to include the distance from the current model.
   *
   * @param   \Illuminate\Database\Eloquent\Builder $query
   * @return \Illuminate\Database\Eloquent\Builder
   */
  public function scopeNearby($query)
  {
    if(!$this->location) {
      return $query;
    }

    return $query->whereHas('location',
      function($query) {
        $query->selectRaw('
                    //Your query for distance (Use the getTable() function wherever required...)
                  ) AS distance
                ')
              ->having('distance', '<', 25);
      }
    );
  }
}
dd($user->with('locations')->toSql());
How to use trait in Laravel model factory?

How to use trait in Laravel model factory?


By : Todd Levy
Date : March 29 2020, 07:55 AM
hop of those help? Probably not what you're looking for but here goes:
You can create an inline class that defines a function that returns the real function you want to use. That inline class can use a trait.
code :
<?php

use App\Working;
use App\Traits\Calculate;

// ...
$factory->define(App\Working::class, (new class {
     use Calculate;
     public function generatorFunction() {
         return function (Faker\Generator $faker) {
             // ...
             $getData = $this->getData();
             // ...
             return ['get_data' => $getData];

         };
     }
 })->generatorFunction());
Laravel: difference between using a Trait or extending a model

Laravel: difference between using a Trait or extending a model


By : user2792638
Date : March 29 2020, 07:55 AM
hope this fix your issue Extending a parent/abstract class is best used when the models all share common characteristics but behave (function) in a unique way. For instance, Cars and Motorcycles are both Vehicles. They share common characteristics (e.g. headlights, indicators, engines, transmissions, etc.), but how to operate() them is unique from class to class.
Traits, on the other hand, are better used when the using classes (or Eloquent models in this case) require common functionality (i.e. methods) despite having different characteristics.
Laravel: How do i fix this trait (with attributes) to also work when creating a model?

Laravel: How do i fix this trait (with attributes) to also work when creating a model?


By : lion.2010
Date : March 29 2020, 07:55 AM
I wish this helpful for you Trick was using a saved model event and also not forgetting to set the fillable attribute on the email model:
code :
/*
    * Update or create single e-mail model for model owner
    *
    * @return void
    */
    private function setEmailTypeAttribute($emailType, $email) :void
    {
        static::saved(static function($model) use($emailType, $email) {
            $model->emails()
                ->updateOrCreate(
                    [
                        'owned_by_type' => get_class($model),
                        'owned_by_id' => $model->id,
                        'type' => $emailType
                    ],
                    [
                        'email' => $email
                    ]);
            });
    }
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org