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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办