我正在尝试使用当前用户的user_id和用户正在查看的事件的event_id来填充数据透视表。但是在点击提交时,页面显示由于不活动,页面已过期。请刷新并重试。这是我的代码:
EventController.php
public function index()
{
$events = DB::table('events')->get();
return view('events.index', ['events'=>$events]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('events.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$event = new Event;
$event->title = $request->eventTitle;
$event->location = $request->eventLocation;
$event->date = $request->eventDate;
$event->time = $request->eventTime;
$event->save();
Session::flash('success', 'Event created successfully');
return redirect()->route('events.create');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$event = Event::find($id);
return view('events.show', ['event'=>$event]);
}
EventsUsersController.php
public function store(Request $request)
{
$event_id = $request->id;
$user_id = $request->Auth::user()->id;
DB::table('event_user')->insert([
['event_id' => $event_id],
['user_id' => $user_id]
]);
}
以及我要将数据插入数据透视表的显示页面
@extends('layouts.app')
@section('content')
<div class="container">
<h1>{{$event->title}}</h1>
<p>Date: {{$event->date}}</p>
<p>Time: {{$event->time}}</p>
<p>Location: {{$event->location}}</p>
@if(Auth::check())
<form method="POST" action="{{ route('eventsusers.store') }}">
{{ csrf_field() }}
<input type="hidden" name="event_id" value="{{ $event->id }}">
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<button type="submit" class="btn btn-success">Register</button>
</form>
@endif
</div>
@endsection
我的user_event表的迁移文件:
public function up()
{
Schema::create('event_user', function (Blueprint $table) {
$table->integer('event_id');
$table->integer('user_id');
$table->timestamps();
$table->primary(['event_id', 'user_id']);
});
}
我是laravel的新手。所以请温柔。 :)
答案 0 :(得分:1)
csrf令牌可以创建此类问题。有几种方法可以解决它。
在表单中添加{{ csrf_field() }}
,如下面的代码&amp;检查会发生什么。
您可以添加隐藏字段,例如<input type="hidden" name="_token" value="{{ csrf_token() }}">
您可以使用此方法更新VerifyCsrfToken中间件
受保护的$除外= [ “你的/路由” ]。