如何修复“在 null 上调用成员函数 update()”错误?

我正在尝试将输入数据从表单提取到数据库中以运行更新操作。然而,它们是一个错误,直接指向“$obj->update(request()->all());”行。错误是“在 null 上调用成员函数 update()”。我不太确定这个错误意味着什么,但我认为这意味着 request()->all() 为空。我想要的输出是在将用户输入更新到数据库后,用户将通过 return redirect('/p') 重定向到 pageManagement.blade.php; 然而,当前的输出只是一条错误消息,指出“Call to a member function update() on null”。


public function update($URI)

{

    $data = request()->validate([

        'title' => 'required',

        'URI' => 'required|min:5|max:10',

        'pageContent' => 'required'

    ]);

    $obj = \App\Page::find($URI);

    $obj->update(request()->all());

    return redirect('/p');

}

这是我的模型 Page.php。


class Page extends Model

{

    protected $fillable = ['title', 'URI', 'pageContent'];

}

这是我在 web.php 中的路线。


Route::patch('/page/{URI}','PageController@update');

这是我的表单,供用户输入输入数据。


<form action="/page/{{ $pageContent->URI }}" method="post">

@csrf

@method('PATCH')

  <label for="title">Title:</label><br>

  <input type="text" id="title" name="title" autocomplete="off" value="{{ $pageContent -> title 

}}"><br>

  @error('title') <p style="color: red">{{ $message }}</p> @enderror

  <label for="URI">URI:</label><br>

  <input type="text" id="URI" name="URI" autocomplete="off" value="{{ $pageContent -> URI }}"> 


@error('URI') {{ $message }}

@enderror 页面内容:
pageContent }}"> @error('pageContent') {{ $message }}


@enderror tinymce.init({ 选择器:'#pageContent' })

我的 GitHub 存储库附在下面,因为表单的问题格式看起来有点搞砸了......

https://github.com/xiaoheixi/wfams

感谢您阅读我的问题!:D


富国沪深
浏览 53回答 4
4回答

倚天杖

您正在使用Request ,因此您需要在函数中调用Requestupdate() ,您可以使用 For 方法where():public function update(Request $request){    $data = $request->validate([        'title' => 'required',        'URI' => 'required|min:5|max:10',        'pageContent' => 'required'    ]);    $obj = \App\Page::where('URI', $request->URI)        ->update([            'title' => $request->title,             'pageContent' => $request->pageContent        ]);    return redirect('/p');}

慕哥9229398

错误的意思是find返回了null。这是有道理的,因为find我们将尝试搜索主键字段而不是URI字段。我不会使用URI作为该记录的标识符。我会用它的id.<form action="/page/{{ $pageContent->id }}" method="post">Route::patch('/page/{page}','PageController@update');这将使事情变得相当简单。现在您可以轻松地使用路由模型绑定来获取此信息Page:use App\Page;...public function update(Page $page){&nbsp; &nbsp; $data = request()->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'URI' => 'required|min:5|max:10',&nbsp; &nbsp; &nbsp; &nbsp; 'pageContent' => 'required'&nbsp; &nbsp; ]);&nbsp; &nbsp; $page->update(request()->all());&nbsp; &nbsp; return redirect('/p');}这将确保这$page是一个现有的模型实例。既然如此,你就可以调用update它。只需确保title,URI和pageContent是“可填充的”。

呼啦一阵风

我看到你的github https://github.com/xiaoheixi/myFirstWebsite在控制器中使用模型绑定的示例:Route::patch('/page/{URI}','PageController@update');Route::get('{URI}', 'PageController@getPage');public function update($URI){&nbsp; &nbsp; $data = request()->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'URI' => 'required|min:5|max:10',&nbsp; &nbsp; &nbsp; &nbsp; 'pageContent' => 'required'&nbsp; &nbsp; ]);&nbsp; &nbsp; $obj = \App\Page::find($URI);&nbsp; &nbsp; $obj->update(request()->all());&nbsp; &nbsp; return redirect('/p');}将控制器更改为:use App\Page;public function getPage(Page $URI){&nbsp; &nbsp; return view('page_view', compact('URI'));}public function update(Request $request, Page $URI)//match $URI to {URI}{&nbsp; &nbsp; $data = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'URI' => 'required|min:5|max:10|unique:pages,URI,'.$URI->id, //you dont want a duplicate URI when you search a page by URI&nbsp; &nbsp; &nbsp; &nbsp; 'pageContent' => 'required'&nbsp; &nbsp; ]);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $URI->update($request->all());&nbsp; &nbsp; return redirect('/p');}在刀片中:<form action="/page/{{ $pageContent }}" method="post">在模型中:use Illuminate\Database\Eloquent\Model;class Page extends Model{&nbsp; &nbsp; protected $fillable = ['title', 'URI', 'pageContent'];&nbsp; &nbsp; public function getRouteKeyName()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return 'URI';&nbsp; &nbsp; }}

当年话下

我建议您使用验证规则“ exists ”验证 URI 是否存在:&nbsp; $data = request()->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'URI' => 'required|min:5|max:10|exists:pages,URI',&nbsp; &nbsp; &nbsp; &nbsp; 'pageContent' => 'required'&nbsp; &nbsp; ]);现在,如果 URI 不存在于您的页面表中,laravel 将返回一个验证错误,并显示消息,如“指定的 URI 不存在于表页面中”
打开App,查看更多内容
随时随地看视频慕课网APP