Laravel:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法有错误;

时间:2019-10-12 01:34:17

标签: php html laravel laravel-5 laravel-blade

我有一个简单的Crud应用,只有一个输入post title

这是职位表

张贴表格

id  title
1   sdss
2   pani

这是我在后控制器中的更新功能

public function update(Request $request, $id)
{
    $post = Post::find($id);
    $post->$request = $request->get('title');
    $post->save();

    return redirect("/posts")->with("success", "Data updated");
}

这是我的模特

class Post extends Model
{
    protected $fillable =["title"];
}

这是html

@section('content')
  <div class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-12">

          <form method="post" action="{{ route('posts.update', $post->id) }}" class="form-horizontal">
            @csrf
            @method('PATCH') 

            <div class="card ">
              <div class="card-header card-header-primary">
                <h4 class="card-title">{{ __('Edit Page Details') }}</h4>
                <p class="card-category"></p>
              </div>
              <div class="card-body ">
                <div class="row">
                  <div class="col-md-12 text-right">
                      <a href="{{ route('posts.index')}}" class="btn btn-sm btn-primary">{{ __('Back to list') }}</a>
                  </div>
                </div>
                <div class="row">
                  <label class="col-sm-2 col-form-label">{{ __(' Title') }}</label>
                  <div class="col-sm-7">
                        <div class="form-group">    

                        <input type="text" class="form-control" name="title" value="{{$post->title}}"/>
                        </div>
                  </div>
                </div>

              </div>
              <div class="card-footer ml-auto mr-auto">
                <button type="submit" class="btn btn-primary">{{ __('Save') }}</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
@endsection

现在,当我单击“保存”按钮以更新数据时,出现以下错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the 
manual that corresponds to your MariaDB server version for the right syntax to use near '.`8,
application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, 
' at line 1 (SQL: update `posts` set `PATCH /posts/2 HTTP/1`.`1 Accept:

laravel的新手

我该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:2)

如果您想更新标题,不是吗?

$post->title = $request->get('title');

代替

$post->$request = $request->get('title');

帖子模型没有名为$request的属性

PS:$fillable属性无关紧要,因为您没有执行批量分配

希望这会有所帮助