dt888999 2018-06-03 04:18
浏览 50
已采纳

如何将数据从一个模型的显示页面插入到laravel中的数据透视表中

I am trying to fill a pivot table with the user_id of the current user and event_id of the event that the user is viewing. But on clicking submit the page shows The page has expired due to inactivity. Please refresh and try again. Here is my code:

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]
    ]);
}

and the show page from where I want to insert data into the pivot table

@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

My migration file for user_event table:

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']);
        });
    }

I am new to laravel. So please be gentle. :)

  • 写回答

1条回答 默认 最新

  • douzhang7728 2018-06-03 04:22
    关注

    csrf token can create this type of problem. There are several ways to solve it.

    1. Add {{ csrf_field() }} inside your form like below code & check what happens.

      {{ csrf_field() }} Register
    2. You can add a hidden field like this <input type="hidden" name="_token" value="{{ csrf_token() }}">

    3. You can update VerifyCsrfToken middleware using this approach

      protected $except = [ 'your/route' ];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 streamingtool
  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析