laravel-5.7:数据未保存到数据库中,未找到对象 [英] laravel-5.7: data is not saving into database, Object not found
问题描述
我正在尝试将数据保存到db中,但是没有保存,并说找不到该对象,任何人都可以向我建议解决方案,我正在关注本教程:
I'm trying to save data into db but its not saving and says that object not found, can anyone suggest me solution, i am following this tutorial: https://laracasts.com/series/laravel-from-scratch-2018/episodes/10
控制器:
public function index()
{
$projects = Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
路线:
Route::get('/projects','ProjectsController@index');
Route::post('/projects','ProjectsController@store');
Route::get('/projects/create','ProjectsController@create');
create.blade.php:
create.blade.php:
<form method="POST" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Project description"></textarea>
</div>
<div>
<button type="submit">Create Project</button>
</div>
</form>
index.blade.php:
index.blade.php:
@foreach($projects as $project)
<li>{{ $project->title }}</li>
@endforeach
推荐答案
您发布的Laravel代码在正确配置的网站下是正确的.您评论中的错误:
The Laravel code you've posted is correct under a properly configured website. The error from your comments:
找不到对象!在此服务器上找不到请求的URL.这 引荐页上的链接似乎有误或已过时.请 将该错误通知该页面的作者.如果您认为这是一个 服务器错误,请与网站管理员联系.错误404本地主机 Apache/2.4.33(Win32)OpenSSL/1.1.0h PHP/7.2.7
Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
是 Apache错误页面 ,这意味着它根本没有从laravel项目中请求页面.数据可能保存在数据库中,但是随后您重定向到项目外部的页面,而Apache找不到它.
is an Apache error page, which means it's not requesting a page from your laravel project at all. The data is probably saving in your database, but then you redirect away to a page that is outside your project, and Apache can't find it.
您的网站位于http://localhost/laravel/public
,这意味着您需要访问http://localhost/laravel/public/projects
的项目页面.但是,redirect('/projects')
为您提供了绝对路径,而不是 relative 路径,将您带到不存在的http://localhost/projects
.
Your website is located at http://localhost/laravel/public
, which means you need to access the projects page at http://localhost/laravel/public/projects
. However, redirect('/projects')
gives you an absolute path instead of a relative path, sending you to http://localhost/projects
, which does not exist.
由于这是一个本地开发项目,所以我将跳过Apache配置不正确的问题,而将重点放在其他避免错误的方法上.
Since this is a local development project, I'm going to skip the issues with the improper Apache configuration and focus on other ways to avoid the error.
使用命名路由:
Route::get('/projects','ProjectsController@index')->name('projects.index');
,并使用路由名称进行重定向:
and use the name of the route for the redirect:
return redirect()->route('projects.index');
此应该在您的项目中生成正确的网址.
This should generate correct urls within your project.
使用serve
代替Apache进行开发.
Use serve
for development instead of Apache.
在您的Laravel项目目录中打开一个终端,然后运行以下命令:
Open a terminal in your Laravel project directory and run this command:
php artisan serve
这将在 http://localhost:8000 处启动PHP的内置Web服务器,从而完全跳过Apache.在开发过程中,这非常好.
This will start PHP's built-in webserver at http://localhost:8000, skipping Apache entirely. During development this is perfectly fine.
这篇关于laravel-5.7:数据未保存到数据库中,未找到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!