如何将数据从一个模型的显示页面插入到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. :)

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