我有一个控制器文件,该文件负责检索数据并将数据插入数据库:
namespace App\Http\Controllers;
use App\Dream;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class DreamsController extends Controller
{
public function submit(Request $request) {
$dreamEntry = new Dream();
$dreamEntry->dream = $request->input('dream');
$dreamEntry->save();
return redirect('/')->with('status', 'INSERTED');
}
}
我上这节课是为了梦想:
class Dream extends Model
{
use Notifiable;
protected $fillable = [
'dream'
];
}
我的web.php文件中的一条路由:
Route::post('upload/submit', array('dream'=>'DreamController@submit'));
一种非常简单的形式:
<form action="/upload/submit" method="post">
<textarea></textarea>
<button type="submit">SUBMIT</button>
</form>
从数据库中获取数据工作正常,我可以看到我的submit
函数正在运行,因为在和处,我看到根(/)处的URL,但是我得到了error 419
先前的代码有什么问题?
答案 0 :(得分:1)
您的csrf
中需要一个<form>
字段,而您的<textarea>
没有name=
属性:
<form action="/upload/submit" method="post">
{{ csrf_field() }}
<textarea name="dream"></textarea>
<button type="submit">SUBMIT</button>
</form>
此外,我不确定array('dream'=>'DreamController@submit')
是否可行,可能很简单:
Route::post('upload/submit', 'DreamController@submit');
// 2nd parameter is the Controller/Function you want to use.