Laravel Api更新和删除功能

时间:2017-01-03 09:40:43

标签: php laravel rest api laravel-5.3

我正在构建API,我在更新和删除表格时遇到以下错误我使用邮递员来测试我的api

    //update error
QueryException in Connection.php line 770:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: update `lessons` set `title` = , `body` = , `completed` = , `updated_at` = 2017-01-03 09:14:10 where `id` = 11)

//delete error
FatalErrorException in LessonsController.php line 80:
Call to a member function delete() on null

我的控制器LessonsController

<?php

namespace App\Http\Controllers;

use Response;
use App\lesson;
use Illuminate\Http\Request;
use App\Acme\Transformers\LessonTransformer;
use Illuminate\Support\Facades\Input;

class LessonsController extends ApiController {

    protected $lessonTransformer;

    function __construct(LessonTransformer $lessonTransformer) {

        $this->lessonTransformer = $lessonTransformer;

    }

    //fetch all and pass a metadata 'data' 
    public function index() {

        $lessons = Lesson::all();

        return $this->respond([

            'data' => $this->lessonTransformer->transformCollection($lessons->all())

        ]);
    }


    //fetch by id
    public function show($id) {

        $lesson = Lesson::find($id);

        if(! $lesson) {

            return $this->respondNotFound();

        }

        return $this->respond([

            'data' => $this->lessonTransformer->transform($lesson)

        ]);
    }


    public function store() {

        if (! input::get('title') or ! input::get('body')) {
            return $this->respondBadRequest();
        }

        Lesson::create(input::all());

        return $this->respondCreated();

    }


    public function update(Request $request, $id) {

        $ulesson = Lesson::find($id);

        $ulesson->title = $request->input('title');
        $ulesson->body = $request->input('body');
        $ulesson->completed = $request->input('completed');
        $ulesson->save();

        return "Sucess updating user #" . $ulesson->id;
    }   


    public function destroy(Request $request) {
        $dlesson = Lesson::find($request->input('id'));

        $dlesson->delete();

        return "Employee record successfully deleted #" . $request->input('id');
    }

}

我的模特课程

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    protected $fillable = ['title', 'body', 'completed',];

    //protected $hidden =['title'];
}

商店和其他功能正常运作

谢谢

4 个答案:

答案 0 :(得分:1)

在更新中 你可以在第69行dd($ request-&gt; input('title')) 我想你没有发送标题值

并删除 我认为你在id字段中没有价值

答案 1 :(得分:1)

The image of postman

请检查邮递员并将其设置为

答案 2 :(得分:1)

我刚刚下载了Insomnia,并测试了每件事情都按预期工作正常

我不知道为什么它不在邮递员工作

答案 3 :(得分:-1)

Laravel APi更新功能...

  

根据此查询,无需定义键

控制器功能

`

public function update(Request $request){

       $reqdata = $request->all(); 

        $reqdata['date_created'] = date('Y-m-d');

         $lesson= Lesson::where('id',$request->id)->update($reqdata);

        if ($lesson) {

            return 'true';

        }else{

            return 'false';

        }

    }  `

Your data type as attached image