fourtwothree

922 经验值

希望能出一个测试系列视频,以laravel单元测试为主。

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

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

@JellyBool

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

@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');
    }
}

错误信息:

ErrorException in LessonsController.php line 108:
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());
    }
}

@世界的小宇宙 感谢你的回答。试了很多种办法都不行,最后我把所有网页关了。再把所有的缓存都清了一遍,再次访问,竟然好了,我只想说my gold.....

另外,刚才我试了把表单提交指向/discussions改为/discussions/store,出现错误:MethodNotAllowedHttpException in RouteCollection.php line 219:

@JellyBool 我的php artisan命令查看是laravel-v5.1.31,但我下载的是www.golaravel.com的v.5.1.11的一键安装包