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

你的数据库的表有title这个字段么?把你的数据库表结构给我看看。

fourtwothree

@JellyBool

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLessonsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('lessons', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->boolean('free');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('lessons');
    }
}
JellyBool

Lesson这个model文件有没设置 $hidden 这个属性?不然不科学啊

fourtwothree

@JellyBool

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    // protected $hidden = ['title'];
}
fourtwothree

@JellyBool
可以了,虽然注释了,但是还是出问题了,我把它(// protected $hidden = [‘title’])删掉就好了,不明所以…

JellyBool

你注释掉也还报错?如果这样的你清除一下缓存php artisan cache:clear 或者重启一下homestead,如果你用homestead的话。

fourtwothree

@JellyBool
觉得应该是缓存的问题, 我正常显示之后,又把注释(// protected $hidden = [‘title’];)重新加了回来,也是可以正常显示的。我之前有试过清除浏览器的缓存,但是没用。php artisan cache:clear这个命令没试过,php artisan cache:clear 清除应用程序缓存,这个和清除浏览器缓存有何不同?

JellyBool

我个人的理解是:浏览器的缓存是更侧重于静态资源的缓存,比如css,图片等,很多时候我们修改了css而刷新网页的时候不起作用就是浏览器缓存做的怪。而php artisan cache:clear是laravel官方提供的一种针对框架缓存的清除命令,这些缓存包括编译后的文件和视图的缓存,所以,简单的可以理解为:浏览器缓存是对静态资源的缓存,cache:clear的缓存是laravel框架的缓存。

fourtwothree

@JellyBool
非常感谢你的耐心回答,学到很多,祝laravist越办越好!