Preston Garvey
Preston Garvey

Reputation: 1421

Pass URL id to form via hidden input ? Laravel 5

I'm wondering is there a way to pass URL id to form via hidden input (not using route parameter) ? I got this form url : http://1mark.dev/jurnal/create?edisi=1 which is generated by this url : <a href="../jurnal/create?edisi={{$edisi->id}}" class="btn btn-primary">Tambah Jurnal</a> I wanted to get the id and pass it to the create form view :

@if (isset($jurnal))
    {!! Form::hidden('id', $jurnal->id) !!}
@endif

@if ($errors->any())
    <div class="form-group {{ $errors->has('judul') ? 'has-error' : 'has-success' }}">
@else
    <div class="form-group">
@endif
    {!! Form::label('judul', 'Judul :', ['class' => 'control-label']) !!}
    {!! Form::text('judul', null, ['class' => 'form-control']) !!}
    @if ($errors->has('judul'))
        <span class="help-block">{{ $errors->first('judul') }}</span>
    @endif
</div>

@if ($errors->any())
    <div class="form-group {{ $errors->has('penulis') ? 'has-error' : 'has-success' }}">
@else
    <div class="form-group">
@endif
    {!! Form::label('penulis', 'Penulis :', ['class' => 'control-label']) !!}
    {!! Form::text('penulis', null, ['class' => 'form-control']) !!}
    @if ($errors->has('penulis'))
        <span class="help-block">{{ $errors->first('penulis') }}</span>
    @endif
</div>

@if ($errors->any())
    <div class="form-group {{ $errors->has('abstrak') ? 'has-error' : 'has-success' }}">
@else
    <div class="form-group">
@endif
    {!! Form::label('abstrak', 'Abstrak :', ['class' => 'control-label']) !!}
    {!! Form::textarea('abstrak', null, ['class' => 'form-control']) !!}
    @if ($errors->has('abstrak'))
        <span class="help-block">{{ $errors->first('penulis') }}</span>
    @endif
</div>

@if ($errors->any())
    <div class="form-group {{ $errors->has('file') ? 'has-error' : 'has-success' }}">
@else
    <div class="form-group">
@endif
    {!! Form::label('file', 'File Jurnal (PDF) :') !!}
    {!! Form::file('file') !!}
    @if ($errors->has('file'))
        <span class="help-block">{{ $errors->first('file') }}</span>
    @endif
</div>
    {!! Form::hidden('id', $edisi->id) !!}    
<div class="form-group">
    {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
</div>

My question is, how to pass the edisi id to the view so that I can pass it back to controller .. Thank you

create controller :

public function create(Edisi $edisi) {
        return view('jurnal/create', compact('edisi'));
    }

store controller :

public function storejurnal(JurnalRequest $request) {
        $input = $request->all();
        //Input PDF
        if ($request->hasFile('file')) {
            $input['file'] = $this->uploadPDF($request);
        }
        //Insert data jurnal
        $id = $request->id;
        $jurnal = Edisi::findOrFail($id)->jurnal()->create($input);
        return redirect('jurnal');
    }

Upvotes: 1

Views: 1456

Answers (2)

Andrej
Andrej

Reputation: 7504

You can pass the variable to a view like

return view('your view')->with('edisi', \Request::get('edisi'));

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

You need to pass variable to a view as usual:

return view('my.view', compact('edisi'));

Then you can pass this variable to a controller. Use this in a view as you do now:

{!! Form::hidden('id', $edisi->id) !!}

And use this in controller:

public function someAction(Request $request)
{
    $id = $request->id;
}

Upvotes: 0

Related Questions