<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Lesson;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class LessonsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// all() is bad
// 没有提示信息(Yes)
// 直接展示我们的数据结构
// 没有错误信息
$lessons = Lesson::all();
// return $lessons;
// exit;
return \Response::json([
'status' => 'success',
'status_code' => 200,
'data' => $this->transform($lessons)
//'data' => $lessons->toArray()
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$lesson = Lesson::findOrFail($id);
return \Response::json([
'status' => 'success',
'status_code' => 200,
'data' => $lesson
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
private function transform($lessons)
{
return array_map(function($lesson){
return [
'title' => $lesson['title'],
'content' => $lesson['body'],
'is_free' => $lesson['free']
];
},$lessons->toArray);
}
}