Laravel 5开发API教程第3节3分42秒,ErrorException in LessonsController.php line 108: Undefined index: title

错误信息:

Undefined index: title

代码如下

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Lesson;
use Illuminate\Support\Facades\Response;

class LessonsController extends Controller
{
    /**
     * Display a listing of the resource
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // all();
        // 没有提示信息
        // 直接展示我们的数据结构
        // 没有错误信息
        $lessons = Lesson::all();
        return \Response::json([
            'status' => 'success',
            'status_code' => 200,
            'data' => $this->transform($lessons)
        ]);
    }

    /**
     * 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)
    {
        //
    }

    public function transform($lessons)
    {
        return array_map(function($lesson){
            return [
                'title' => $lesson['title'],
                'content' => $lesson['body'],
                'is_free' => $lesson['free']
            ];
        }, $lessons->toArray());
    }
}
JellyBool
修改的评论也不能少于六个字哦!
fourtwothree
修改的评论也不能少于六个字哦!
JellyBool
修改的评论也不能少于六个字哦!
fourtwothree
修改的评论也不能少于六个字哦!
fourtwothree
修改的评论也不能少于六个字哦!
JellyBool
修改的评论也不能少于六个字哦!
fourtwothree
修改的评论也不能少于六个字哦!
JellyBool
修改的评论也不能少于六个字哦!
fourtwothree
修改的评论也不能少于六个字哦!