Laravel将id从刀片模板传递到商店控制器

时间:2017-07-04 12:41:20

标签: laravel laravel-5

我正在尝试将id传递给存储控制器,因为我需要保存从另一个表中检索的business_id。但是我明白了:

  

App \ Http \ Controllers \ EventController :: store()

缺少参数2

以下是我的观点:

@extends('master') @section('title', 'Live Oldham')
@section('content')
    <div class="col-lg-y col-lg-offset-3">  
    <ul class="list-group-list">
        @foreach ($business->businesses as $business)
        <li class="list-group-item">
        <a target="_blank" href="{{ url('business/' . $business->id) }}"> {{($business->name) }}</a><a href="{{ url('events/create/' . $business->id) }}" class="fa fa-plus pull-right" aria-hidden="true"></a>
        </li>
        @endforeach       
    </ul>
    </div>
@endsection

控制器:

class EventController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $id = Auth::id();
        $business = User::where('id', $id)
            ->with('businesses')
            ->first();
        return view('events.viewEvent', compact('business'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        return view('events.addEvent')
            ->with('Business', Business::find($id));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $id)
    {
        $event = new Event;
        $event->startdate = $request->input('startdate');
        $event->enddate = $request->input('enddate');
        $event->title = $request->input('title');
        $event->frequency = $request->input('frequency');
        $event->description = $request->input('description');
        $event->business_id = $id;
        $event->save();
    }

形式:

@extends('master') @section('title', 'Live Oldham')
@section('content')
<div class="container">
<!-- Alert Messages -->
@if (session('message'))
@if (session('message')=="success")
    <div class="alert alert-success">
        Event Created
    </div>
@else
    <div class="alert alert-danger">
       There has been a fatal error! Apologies, we are working to fix it!
    </div>
@endif
@endif
<!-- JQuery UI init -->
<script>
  $( function() {
    $( document ).tooltip();
  } );
</script> 
 <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Create Event</div>
                    <div class="panel-body">
                        <form class="form-horizontal" role="form" method="POST" action="{{ action('EventController@store') }}">
                        {{ csrf_field() }}
                        <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                        <div style="display:none;" class="title_message form-control alert-warning"></div>
                            <label for="title" class="col-md-4 control-label">Event Title</label>
                        <div class="col-md-6">
                        <input id="title" type="text" class="form-control" placeholder="Event title"
                        title="What is the event title?" name="title" value="{{ old('title') }}">
                            @if ($errors->has('title'))
                                <span class="help-block">
                            <strong>{{ $errors->first('title') }}</strong>
                                </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('frequency') ? ' has-error' : '' }}">
                        <div style="display:none ;" class="frequency_message form-control alert-warning"></div>
                            <label id="frequency2" for="frequency" class="col-md-4 control-label">Frequency</label>
                        <div class="col-md-6">
                        <select class="form-control" name="frequency" id="frequency">
                            <option selected disabled>Choose event frequency...</option>
                            <option value="One">One-time Event</option>
                            <option value="Daily">Daily</option>
                            <option value="Weekly">Weekly</option>
                            <option value="Monthly">Monthly</option>
                            <option value="Yearly">Yearly</option>
                        </select>
                                @if ($errors->has('frequency'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('frequency') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('startdate') ? ' has-error' : '' }}">
                        <div style="display:none ;" class="startdate_message form-control alert-warning"></div>
                            <label id="startdate2" for="startdate" class="col-md-4 control-label">Event Start Date</label>
                        <div class="col-md-6">
                            <input id="startdate" type="date" class="form-control" placeholder="Start Date"
                            title="When does the event start?" name="startdate" value="{{ old('startdate') }}">
                                @if ($errors->has('startdate'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('enddate') ? ' has-error' : '' }}">
                        <div style="display:none ;" class="enddate_message form-control alert-warning"></div>
                            <label id="address3" for="enddate" class="col-md-4 control-label">Event End Date</label>
                        <div class="col-md-6">
                            <input id="enddate" type="date" class="form-control" placeholder="End Date"
                            title="When does the event end?" name="enddate" value="{{ old('enddate') }}">
                                @if ($errors->has('enddate'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('enddate') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>                        
                        <div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
                        <div style="display:none ;" class="description_message form-control alert-warning"></div>
                            <label id="description2" for="description" class="col-md-4 control-label">Event Description</label>
                        <div class="col-md-6">
                            <textarea id="description" type="text" class="form-control" placeholder="Event description"
                            title="Here goes event description" name="description" value="{{ old('description') }}">
                            </textarea>
                                @if ($errors->has('description'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('description') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" id="submit" class="btn btn-success">
                                    Add Event
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

1 个答案:

答案 0 :(得分:0)

您可以将ID传递给表单操作

<form method="POST" action="{{ action('EventController@store', $business->id) }}" class="form-horizontal" role="form">
...
</form>