【问题标题】:Json Laravel ModelJson Laravel 模型
【发布时间】:2016-05-21 07:29:52
【问题描述】:

如何在 Laravel 中编写模型而不创建数据库并使用返回 Json 的函数 getData?比如:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

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

但没有数据库。

我会很感激你的帮助

【问题讨论】:

    标签: php json laravel model


    【解决方案1】:

    只有在使用 Eloquent 时才需要扩展 Model。

    如果您只需要从某个地方获取 json 数据,请创建从某个地方获取数据并返回 JSON 的普通类:

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

    composer.json 文件中注册这个类,运行composer dumpauto 并使用你的类:

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

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 1970-01-01
      • 2014-03-25
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多