Laravel Api 更新和删除功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41440454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:02:46  来源:igfitidea点击:

Laravel Api update and delete function

phplaravelrestapilaravel-5.3

提问by Mr Robot

I'm building an API, i'm getting the following error while updating and deleting from table i'm using postman to test my api

我正在构建一个 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

My controller LessonsController

我的控制器 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');
    }

}

my model Lesson

我的模型课

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

the store and other functions are working fine

商店和其他功能运行良好

Thank You

谢谢你

回答by Akkapong Kajornwongwattana

In update can you dd($request->input('title')) in line 69 I think you don't sent the value of title

在更新中你可以在第 69 行 dd($request->input('title')) 我认为你没有发送 title 的值

and in delete I think you have no value in id field

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

回答by Akkapong Kajornwongwattana

The image of postman

邮递员的形象

Please check you postman and set it like this

请检查您的邮递员并将其设置为这样

回答by Mr Robot

i just downloaded Insomnia and tested every thing is working fine as expected

我刚刚下载了 Insomnia 并测试了一切都按预期工作正常

i don't know why it's not working in postman though

我不知道为什么它在邮递员中不起作用

回答by Muhammad Hamza

Laravel APi Update Function...

Laravel API 更新函数...

According to this query no need to define keys

根据此查询无需定义键

Controller Function

控制器功能

`

`

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

您的数据类型作为附加图像