我在laravel中处理表单,我已经编写了代码,但是一旦我单击按钮提交表单,它就重置了页面
这是我的视图create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<form action="/p" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row">
<h1>Add New Post</h1>
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Post Caption</label>
<input id="caption"
type="text"
class="form-control{{ $errors->has('caption') ? ' is-invalid' : '' }}"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption" autofocus>
@if ($errors->has('caption'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('caption') }}</strong>
</span>
@endif
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@if ($errors->has('image'))
<strong>{{ $errors->first('image') }}</strong>
@endif
</div>
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</div>
</div>
</form>
</div>
@endsection
还有我的PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function create(){
return view('posts.create');
}
public function store(){
$data = request()->validate([
'caption' => 'required',
'image' => ['required', 'image'],
]);
Post::create($data);
dd(request()->all());
}
}
我的路线web.php
<?php
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/create', 'PostsController@create');
Route::post('/p', 'PostsController@create');
Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');
验证不起作用,每次我单击按钮时,都会重置所有内容。小孩子帮我解决这个问题
帖子模型
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
答案 0 :(得分:2)
从{p>更改web.php
文件中的这一行
Route::post('/p', 'PostsController@create');
到
Route::post('/p/store', 'PostsController@store')->name('p.store');
如您在上面的修改中所看到的,您所指向的是错误的Controller方法。
此外,最佳做法是使用命名路由。
使用上述命名的路由,您现在可以使用路由帮助程序,而不必担心表单中的此类网址:
<form action="{{ route('p.store') }}" enctype="multipart/form-data" method="post">
</form>
更新1 :
我以前没听过。在Request
请求的定义中,您的Controller方法必须至少具有POST
对象作为参数。同时更新您的验证逻辑。
将您的store()
方法更新为此
public function store(Request $request){
$request->validate([
'caption' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
]);
Post::create($request->input());
dd($request->all());
}
观察到您之前使用的是全局请求帮助器request()
。您不再需要这样做,因为Request对象现在作为参数传递。另请注意,使用路由时,您无需传递任何实际参数。该参数由Laravel自动传递。
更新2:
另外,使用$fillable
数组更新您的Post模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable = ['caption', 'image'];
public function user(){
return $this->belongsTo(User::class);
}
}
$fillable
数组表示可以使用HTTP请求(例如,从HTML表单)分配的数据库中的字段。
从Laravel文档中:
您将需要在模型上指定
fillable
或guarded
属性,因为默认情况下,所有口才模型都可以防止大规模分配。
答案 1 :(得分:0)
问题出在您的路线上。获取和发布路径都将转到控制器的create方法。发布路线就像
public class Program
{
public static void Main()
{
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
Console.WriteLine(keyinfo.Key + " was pressed" + );
}
while (keyinfo.Key != ConsoleKey.X);
}
}