Laravel eloquent 更新函数
Laravel eloquent Update Function
在模块化开发中使用Laravel eloquent。保存效果很好,但是更新功能没有像我预期的那样工作。请检查我的编码方法和错误。
use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
{
$project = Modules\Projects\Entities\Project::find($id);
$project->project_name = request('project_name');
$project->save();
}
错误抛出如下:
{
"message": "Class 'Modules\Projects\Http\Controllers\Modules\Projects\Entities\Project' not found",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "D:\XMAPP\htdocs\minidmsapi\Modules\Projects\Http\Controllers\ProjectsController.php",
"line": 69,
"trace": [
如何在模块化开发中使用“$flight = App\Flight::find(1);
”?
Laravel Official Doc
尝试在 Modules\Projects\Entities\Project::find($id);
之前添加 \
。
喜欢\Modules\Projects\Entities\Project::find($id);
或者您可以直接使用 Project::find($id);
因为您已经使用了命名空间。
您已经导入了 Modules\Projects\Entities\Project;
。现在您可以直接使用 Project
。
use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
{
$project = Project::find($id);
$project->project_name = request('project_name');
$project->save();
}
希望这对您有所帮助...
您不必在 Project
模型上使用完整命名空间,因为您已经在顶部导入了它:
use Modules\Projects\Entities\Project;
只需使用:
$project = Project::find($id);
编辑:
对于 return 一些响应消息,您可以在下面的函数末尾添加类似这样的内容:
return redirect()->back()->with('success', 'Your message');
为了在 blade 中显示您的消息,请添加如下内容:
@if (session()->has('success'))
<div class="alert alert-success">
<p>{{session('success')}}</p>
</div>
@endif
在模块化开发中使用Laravel eloquent。保存效果很好,但是更新功能没有像我预期的那样工作。请检查我的编码方法和错误。
use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
{
$project = Modules\Projects\Entities\Project::find($id);
$project->project_name = request('project_name');
$project->save();
}
错误抛出如下:
{
"message": "Class 'Modules\Projects\Http\Controllers\Modules\Projects\Entities\Project' not found",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "D:\XMAPP\htdocs\minidmsapi\Modules\Projects\Http\Controllers\ProjectsController.php",
"line": 69,
"trace": [
如何在模块化开发中使用“$flight = App\Flight::find(1);
”?
Laravel Official Doc
尝试在 Modules\Projects\Entities\Project::find($id);
之前添加 \
。
喜欢\Modules\Projects\Entities\Project::find($id);
或者您可以直接使用 Project::find($id);
因为您已经使用了命名空间。
您已经导入了 Modules\Projects\Entities\Project;
。现在您可以直接使用 Project
。
use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
{
$project = Project::find($id);
$project->project_name = request('project_name');
$project->save();
}
希望这对您有所帮助...
您不必在 Project
模型上使用完整命名空间,因为您已经在顶部导入了它:
use Modules\Projects\Entities\Project;
只需使用:
$project = Project::find($id);
编辑: 对于 return 一些响应消息,您可以在下面的函数末尾添加类似这样的内容:
return redirect()->back()->with('success', 'Your message');
为了在 blade 中显示您的消息,请添加如下内容:
@if (session()->has('success'))
<div class="alert alert-success">
<p>{{session('success')}}</p>
</div>
@endif