Ludwig Supraphonic Ludwig Supraphonic - 6 months ago 42
JSON Question

Json Laravel Model

How to write model in Laravel without creating Database and with function getData that returns Json? Something like:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Table extends Model
{
protected $table = 'table';
protected $fillable = [
'atribute1',
'atribute2',
'atribute3',
'atribute4'
];
}


But without database.

I would be grateful for help

Answer

You need to extend Model only if you're using Eloquent.

If you just need to get json data from somewhere, create usual class which gets data from somewhere and returns JSON:

class MyModel
{
    public function getJsonData()
    {
        $jsonData = // get json data from somewhere

        return $jsonData;
    }
}

Register this class in composer.json file, run composer dumpauto and use your class:

$model = new MyModel();
$data = $model->getJsonData();