#php #laravel #eloquent
#php #laravel #красноречивый
Вопрос:
У меня возникла проблема с сохранением данных в моей исходной модели, когда я хочу сохранить в (SchoolGrade) «Оригинальную модель» с помощью вспомогательной функции, данные сохраняются в модели перевода (SchoolGradeT).
Код:
- Исходный код модели:
<?php
/**
* Created by Reliese Model.
* Date: Thu, 17 Jan 2019 11:22:32 0000.
*/
namespace App;
use IlluminateDatabaseEloquentModel as Eloquent;
use DimsavTranslatableTranslatable;
use AppTranslationModelsSchoolGradeT;
/**
* Class SchoolGrade
*
* @property int $id
* @property string $name
* @property int $school_id
* @property CarbonCarbon $created_at
* @property CarbonCarbon $updated_at
* @property string $deleted_at
*
* @property AppSchool $school
*
* @package App
*/
class SchoolGrade extends Eloquent
{
use Translatable;
use BackpackCRUDCrudTrait;
use IlluminateDatabaseEloquentSoftDeletes;
public $translatedAttributes = ['name'];
protected $casts = [
'school_id' => 'int'
];
protected $fillable = [
'name',
'school_id'
];
protected $visible = [
'id',
'name',
'school_id'
];
/**
* OVERRIDES DimsavTranslatableTranslatable getAttributeOrFallback function
* Returns the attribute value from fallback translation if value of attribute
* is empty and the property fallback is enabled in the configuration.
* in model.
* @param $locale
* @param $attribute
* @return mixed
*/
private function getAttributeOrFallback($locale, $attribute)
{
$translation = $this->getTranslation($locale);
if ((!$translation instanceof Model ||
empty($translation->$attribute)) amp;amp;
$this->usePropertyFallback()) {
$translation = $this->getTranslation($this->getFallbackLocale(), false);
}
if ($translation instanceof Model) {
return $translation->$attribute;
}
if ($translation === null amp;amp; isset($this->attributes[$attribute])) {
return $this->attributes[$attribute];
}
return null;
}
public function school()
{
return $this->belongsTo(School::class);
}
public function translatables()
{
return $this->hasOne(SchoolGradeT::class, 'school_grade_id');
}
}
* Код модели перевода:
<?php
/**
* Created by Reliese Model.
* Date: Thu, 14 Feb 2019 11:11:46 0000.
*/
namespace AppTranslationModels;
use IlluminateDatabaseEloquentModel as Eloquent;
/**
* Class SchoolGradeT
*
* @property int $id
* @property int $school_grade_id
* @property int $language_id
* @property string $locale
* @property string $name
* @property CarbonCarbon $created_at
* @property CarbonCarbon $updated_at
*
* @property AppLanguage $language
* @property AppSchoolGrade $school_grade
*
* @package App
*/
class SchoolGradeT extends Eloquent
{
use BackpackCRUDCrudTrait;
protected $visible = ['school_grade_id', 'name'];
protected $fillable = ['name', 'language_id', 'locale', 'school_grade_id'];
public $timestamps = true;
public function language()
{
return $this->belongsTo(AppLanguage::class);
}
public function school_grade()
{
return $this->belongsTo(AppSchoolGrade::class);
}
}
* Код вспомогательной функции:
public static function fillGrades($schoolId){
$grades = [
'1st grade',
'2nd grade',
'3rd grade',
'4th grade',
'5th grade',
'6th grade',
'7th grade'
];
foreach ($grades as $name) {
SchoolGrade::create([
'name' => $name,
'school_id' => $schoolId,
]);
}
return true;
}
* Код вызова API:
Route::get('test/{id}', function ($id) {
return response()->json(
[
'status' => "success",
'data' => AppHelpersHelper::fillGrades($id),
],
200
);
});
Заранее спасибо, высоко ценится!