mardi 19 mars 2019

Laravel insert json type column data to db via model

Hi I'm using a json column in migrations and trying to save values to data via model. Here is my migration,

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

And this is my model code,

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'
    ];

},

And this is my insert code,

$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();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire