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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 实值特征权重向量和特征选择,多分类
      • ¥15 INVEST年产水量模块结果出处理
      • ¥15 Neo4j 不显示关系
      • ¥15 这段代码有什么bug
      • ¥15 pip默认安装位置修改不成功
      • ¥15 C++ inline、staitc、const三合一谁能讲通透
      • ¥15 关于pyqt5中QThread的问题
      • ¥20 STM32基于HAL库使用串口发送16进制数组
      • ¥15 OpenCV报错 You can enable it via 'OPENCV_IO_ENABLE_OPENEXR'
      • ¥15 AT89C51的程序问题