我尝试使用fractal转换eloquent模型中的日期,然后通过ajax将其发送到网页。但是,我没有得到预期的转换数据对象。这是我的代码:
变压器:
<?php
namespace App\Transformers\Models;
use App\Models\Student;
use League\Fractal;
class StudentTransformer extends Fractal\TransformerAbstract
{
public function transform(Student $student)
{
return [
'name' => $student->name,
'birth_date' => date('d-m-Y', strtotime($student->birth_date)),
'start_date' => date('d-m-Y', strtotime($student->start_date)),
'is_active' => $student->is_active ? 'Yes' : 'No',
'course_title' => $student->course_title,
'university_id' => $student->university_id,
'institution_id' => $student->institution_id,
'course_id' => $student->course_id,
];
}
}
路由文件:
Route::get('/', function () {
$student = App\Models\Student::first();
$response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);
return response()->json([$response]);
});
返回:
[
{ }
]
如果我dd()
,就像这样:
Route::get('/', function () {
$student = App\Models\Student::first();
$response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);
dd($response);
});
我得到以下内容,没有明显的转换
Item {#299 ▼
#data: Student {#305 ▼
#fillable: array:10 [▼
0 => "name"
1 => "birth_date"
2 => "start_date"
3 => "is_active"
4 => "course_title"
5 => "university_id"
6 => "institution_id"
7 => "course_id"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:13 [▼
"id" => 1
"name" => "Michel Jast"
"birth_date" => "1979-12-22"
"start_date" => "2015-08-02"
"is_active" => 1
"course_title" => "quia"
"university_id" => "10954"
"institution_id" => 1044
"course_id" => 1
"created_at" => "2015-11-23 09:40:35"
"updated_at" => "2015-11-26 10:24:14"
]
#original: array:13 [▼
"id" => 1
"name" => "Michel Jast"
"birth_date" => "1979-12-22"
"start_date" => "2015-08-02"
"is_active" => 1
"course_title" => "quia"
"university_id" => "10954"
"institution_id" => 1044
"course_id" => 1
"created_at" => "2015-11-23 09:40:35"
"updated_at" => "2015-11-26 10:24:14"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
#meta: []
#resourceKey: null
#transformer: StudentTransformer {#301 ▼
#availableIncludes: []
#defaultIncludes: []
#currentScope: null
}
}
我在这里缺少什么想法?
答案 0 :(得分:1)
我的实施如下:
Route::get('/', function () {
$student = App\Models\Student::first();
$response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);
$manager = new \League\Fractal\Manager();
$manager->setSerializer(new \League\Fractal\Serializer\ArraySerializer());
return response()->json($manager->createData($response)->toArray());
});
您必须创建一个\League\Fractal\Manager
实例并为其分配序列化程序。管理员会将数据返回到数组或其他数据中。
在fractal page中,有更详细的内容。
答案 1 :(得分:0)
我没有在项目中使用过Factral,但我认为你需要做类似的事情。
Route::get('/', function () {
$student = App\Models\Student::first();
$response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);
return response()->json($response->toArray());
});