【问题标题】:Laravel Api update and delete functionLaravel Api 更新和删除功能
【发布时间】:2017-05-17 08:45:23
【问题描述】:

我正在构建一个 API,我在使用 postman 测试我的 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

我的控制器课程控制器

<?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'];
}

商店和其他功能运行良好

谢谢

【问题讨论】:

  • 您的 $ulesson-&gt;title 似乎没有在您的更新函数中设置,并且可能需要所有这些值来执行您的更新请求
  • 您可能还想查看$ulesson-&gt;fill($request-&gt;all()); :)
  • 我很抱歉,我没有得到我应该做什么
  • 似乎在销毁函数中 $dlesson = Lesson::find($request->input('id'));找不到要删除的内容。你可以检查 if($dlesson) 然后 $dlesson->delete();
  • 查看Laravel form request validation。我发现这是验证的最干净的解决方案之一。您可以请求验证检查是否设置了所有必填字段,这样如果验证未通过,控制器将永远不会执行。

标签: php laravel rest api laravel-5.3


【解决方案1】:

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

在删除中 我认为您在 id 字段中没有任何价值

【讨论】:

  • 我得到了输出`null`
  • 是的,对于这个空值它没有找到要删除的东西,请检查您从表单输入传递的输入值
  • 在你的邮递员中添加参数 title body 并在 body form-data 中完成?
  • 如果可以的话,可以给我看一下邮递员屏幕的截图或者你提交的表格吗
  • 你应该做 dd($request->all())
【解决方案2】:

请检查你的邮递员并设置它

【讨论】:

  • 先生,这是一个 POST 请求,它工作正常,数据正在插入数据库,但如何执行更新
  • 好的,您只需将邮递员中的方法从 POST 更改为 PUT 或 PATCH
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2017-03-18
      • 2020-01-02
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多