Laravel вставляет данные столбца типа JSON в БД через модель - PullRequest
0 голосов
/ 20 марта 2019

Привет! Я использую столбец json при переносе и пытаюсь сохранить значения в данных через модель. Вот моя миграция,

Schema::create('notifications', function (Blueprint $table) {
    $table->increments('id');
    $table->json('title');
    $table->json('message')->nullable();
    $table->timestamps();
});

А это мой код модели,

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Notification extends Model

{
    use HasTranslations;

    public $translatable = ['title','message'];

    protected $fillable = [
        'title','message'
    ];

},

А это мой код вставки,

$notification = new Notification();

$notification_title = [
    'en' => 'Request created',
    'it' => 'Richiesta creata',
];
$notification_title = json_encode($notification_title);

$notification_message = [
    'en' => 'Request created',
    'it' => 'Richiesta creata',
];
$notification_message = json_encode($notification_message);

$notification->title = $notification_title;
$notification->message = $notification_message;
$notification->save();

Похоже, что поля JSON не сохраняются должным образом.

1 Ответ

2 голосов
/ 20 марта 2019

Заменить json_encode() на JSON-броски :

class Notification extends Model
{
    protected $casts = [
        'title' => 'array',
        'message' => 'array',
    ];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...