Laravel保存到数据库的问题



我有一个问题与Laravel CRUD当我试图添加一些东西到数据库没有发生。/note/store

F12结果

我认为$note->save();在存储功能。同样重要的是要提一下,重定向在存储函数(NoteController)也不起作用。

控制器:

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsNote;
class NoteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$notes = Note::all();
// var_dump($cele);
return view('note.index')->with('notes', $notes);
}

public function create()
{
return view('note.create');
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'subtitle' => 'required',
'content' => 'required',
'color' => 'required',
'tag' => 'required'
]);

$current_date = date('Y-m-d H:i:s');

$note = new Note;
$note->name = $request->input('name');
$note->subtitle = $request->input('subtitle');
$note->content = $request->input('content');
$note->color = $request->input('color');
$note->tag = $request->input('tag');
//$note->created_at = $current_date;
//$note->updated_at = $current_date;
$note->save();
return redirect()->route('/index')->with('success','dodano');
}
/**
* Display the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}

模型:

<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Note extends Model
{
use HasFactory;
public $table = "notes";
public $timestamps = true;
protected $fillable = [
'name',
'subtitle',
'content',
'color',
'tag'
];
}

web.php

<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersNoteController;

//Route::get('/', function () {
// return view('welcome');
//});
Auth::routes();
//Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');

//Route::get('/', 'NoteController@index');
Route::resource('note', NoteController::class);

Route::post('/note/store', function () {
return back()->withInput();
});
Auth::routes();
Route::get('/note', [AppHttpControllersNoteController::class, 'index'])->name('note');
Route::get('/note/create', [AppHttpControllersNoteController::class, 'create'])->name('create');
Route::post('/note/store', [AppHttpControllersNoteController::class, 'store'])->name('store');
//Route::get('/note/store', [AppHttpControllersNoteController::class, 'store'])->name('store');

//Route::get('note/create', array('uses' => 'NoteController@index', 'as' => 'note/create'));

迁移:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateNotesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('subtitle');
$table->mediumText('content');
$table->string('color');
$table->string('tag');
$table->timestamps();
});
// Uzupełnienie tabeli rekordami
DB::table('notes')->insert([
[
'name' => 'Notatka1',
'subtitle' => 'Podtytuł',
'content' => 'h3213',
'color' => 'black',
'tag' => 'notatka',
],
[
'name' => 'Notatka2',
'subtitle' => 'Podtytuł',
'content' => 'text',
'color' => 'red',
'tag' => 'notatka',
]
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
}

请…帮助;(

在你的web.php文件中,没有路由/索引。你确定你有这个名字的路吗?

好吧,路由有问题,我添加了log on start of store功能,但是没有显示。

我的表单在create.blade.php

<form
class="container"
action= "/note/store"
method="POST">
{{ method_field('PUT') }}
{{csrf_field()}}
<div class="form-group">
<label>Nazwa</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label>Subtitle</label>
<input type="text" name="subtitle" class="form-control">
</div>
<div class="form-group">
<label>Content</label>
<input type="text" name="content" class="form-control">
</div>
<div class="form-group">
<label>Color</label>
<input type="text" name="color" class="form-control">
</div>
<div class="form-group">
<label>Tag</label>
<input type="text" name="tag" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Zapisz</button>
</div>
</form>
@endsection

我必须添加{{method_field('PUT')}},否则我会得到其他错误此路由不支持POST方法。支持的方法:GET、HEAD、PUT、PATCH、DELETE。

我应该如何改变我的路由使它与PUT一起工作?

已解决,

<form
class="container"
action= "/note"
method="POST">
{{csrf_field()}}

<div class="form-group">
<label>Nazwa</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label>Subtitle</label>
<input type="text" name="subtitle" class="form-control">
</div>
<div class="form-group">
<label>Content</label>
<input type="text" name="content" class="form-control">
</div>
<div class="form-group">
<label>Color</label>
<input type="text" name="color" class="form-control">
</div>
<div class="form-group">
<label>Tag</label>
<input type="text" name="tag" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Zapisz</button>
</div>
</form>
@endsection

对于重定向问题,尝试在route()中使用路由名称,例如

redirect()->route('home')

最新更新