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 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算