Laravel 在数据库中保存我的信息时出现 CRUD 问题
Laravel CRUD problem with saving my info in the database
我在数据库中保存我的信息时遇到问题 Laravel CRUD,我在创建页面上写了新信息,他转到索引页面,但没有看到信息。
after I press submit
I don't see my new info
我的create.blade.php(第一个图片页):
<p>It works</p>
<form metod="post" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" value="" placeholder="title">
</div>
<div>
<textarea type="text" name="description" value="" placeholder="description"></textarea>
</div>
<div>
<button type="sumbit">Sumbit</button>
</div>
</form>
我的index.blade.php(第二张图片页):
<p>It works</p>
@foreach ($projects as $project)
<li>{{ $project->title}}</li>>
@endforeach
我的web.php
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
Route::get('/users', 'App\Http\Controllers\MainController@users');
Route::get('/business', 'App\Http\Controllers\MainController@business');
Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
我的ProjectsController.php:
<?php
namespace App\Http\Controllers;
use \App\Models\Project;
use Illuminate\Http\Request;
class ProjectsController extends Controller
{
public function index()
{
$projects = \App\Models\Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = reqeust('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
}
我的Project.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
}
您的 ProjectsController
store()
函数中的请求拼写错误,应该是
$project->title = request('title');
这可能不完全是解决方案,您可能需要这个
public function store()
{
$project = new Project();
$project->title = request()->input('title');
$project->description = request()->input('description');
$project->save();
return redirect('/projects');
}
我在数据库中保存我的信息时遇到问题 Laravel CRUD,我在创建页面上写了新信息,他转到索引页面,但没有看到信息。 after I press submit
I don't see my new info
我的create.blade.php(第一个图片页):
<p>It works</p>
<form metod="post" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" value="" placeholder="title">
</div>
<div>
<textarea type="text" name="description" value="" placeholder="description"></textarea>
</div>
<div>
<button type="sumbit">Sumbit</button>
</div>
</form>
我的index.blade.php(第二张图片页):
<p>It works</p>
@foreach ($projects as $project)
<li>{{ $project->title}}</li>>
@endforeach
我的web.php
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
Route::get('/users', 'App\Http\Controllers\MainController@users');
Route::get('/business', 'App\Http\Controllers\MainController@business');
Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
我的ProjectsController.php:
<?php
namespace App\Http\Controllers;
use \App\Models\Project;
use Illuminate\Http\Request;
class ProjectsController extends Controller
{
public function index()
{
$projects = \App\Models\Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = reqeust('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
}
我的Project.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
}
您的 ProjectsController
store()
函数中的请求拼写错误,应该是
$project->title = request('title');
这可能不完全是解决方案,您可能需要这个
public function store()
{
$project = new Project();
$project->title = request()->input('title');
$project->description = request()->input('description');
$project->save();
return redirect('/projects');
}