【发布时间】:2019-04-01 12:10:57
【问题描述】:
我使用这个表架构:
Schema::create('forms', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 255)->default('');
$table->text('html')->nullable();
$table->text('json')->nullable();
$table->timestamps();
$table->softDeletes();
});
这是模型:
class Form extends Model
{
use SoftDeletes;
protected $fillable = [
'name',
'html',
'json'
];
protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
}
在控制器中,我想显示所有模型项目的列表,但只有 id 和 name 文件。现在我使用它,但它显示所有未隐藏的字段:
public function index() {
return Form::->paginate(100);
}
此功能仅适用于表格名称列表。但这里是第二个显示表单数据以进行修改:
public function show(string $id) {
$item = Form::findOrFail($id);
return response()->json($item);
}
当然,最后一个函数需要显示所有字段(id、name、html 和 json)。
是否有任何最佳做法可以在 index() 函数中使用 paginate() 仅显示我需要的字段?
【问题讨论】:
标签: laravel eloquent laravel-5.7 laravel-pagination