Laravel 5.4中的logout和TokenMismatchException

时间:2018-01-22 12:17:15

标签: laravel token logout

我有控制器,我保存发票,但可以选择添加项目,银行和公司。当我想保存项目并返回发票视图时,出现问题:

TokenMismatchException
in VerifyCsrfToken.php (line 68)

或刷新页面我收到错误:HttpException

这是我的控制者:

public function create()
  {
    $banks = Bank::all();
    $methods = Payment_method::pluck('name_payment', 'id');
    $users = User::pluck('name', 'id');
    $items = Item::all();
    $vats = Vat::all();
    $companies = Company::all('name', 'id');
    $numberProform = $this->setNumberProform();

    if (isset($_COOKIE["addInvoiceForm"])) {
      $previousData = $_COOKIE["addInvoiceForm"];
      $previousData = unserialize($previousData);
      return view('invoice.addInvoice', compact('items', 'companies', 'users', 'vats', 'numberProform', 'methods', 'banks', 'previousData'));
    }
    return view('invoice.addInvoice', compact('items', 'companies', 'users', 'vats', 'numberProform', 'methods', 'banks'));
  }

  public function store(Request $request)
  {
    if ($request->add === 'item') {
      setcookie('addInvoiceForm', serialize($request->input()), time() + (86400 * 30), "/");
      return redirect('addItem');
    }

    $this->validation($request);
    $input = Req::all();
    $invoice = Invoice::create($input);
    $i = 1;
    while ($request->has('item_' . $i)) {
      $invoice->items()->attach([$request->{'item_' . $i} => ['quantity' => $request->{'quantity_' . $i}]]);
      $i++;
    }
    return redirect('listInvoice');
  }

查看addInvoice.blade.php:

@extends('layouts.app')
@section('content')
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 col-sm-12 col-sm-offset-0">
        <div class="panel panel-default">
          {!! Form::open(['url' => 'saveInvoice', 'method' => 'post', 'class' => 'form-horizontal']) !!}
            {{ csrf_field() }}
          <div class="panel-heading">
            <h2>Proforma nr {{$numberProform}}</h2>
          </div>
          <div class="form-group">
            <div class="col-sm-12">
              {!! Form::hidden('number', $numberProform) !!}
              <span>UWAGA: PRO FORMA nie jest dokumentem księgowym i nie jest podstawą  do odliczenia VAT.</span>
            </div>
          </div>
          <div class="panel-body">
            <div class="form-horizontal">

              <div class="row">
                {!! Form::hidden('proforma', "1") !!}
                {!! Form::hidden('user_id', \Illuminate\Support\Facades\Auth::user()->getAuthIdentifier()) !!}

                <div class="form-group {{ $errors->has('place_issue') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('place_issue', 'Miejsce:') !!}
                  </div>
                  <div class="col-sm-6">
                    {!! Form::text('place_issue', $previousData['place_issue'], ['class' => 'form-control']) !!}
                    @if ($errors->has('place_issue'))
                      <span class="help-block"><strong>{{ $errors->first('place_issue') }} </strong></span>
                    @endif
                  </div>
                </div>

                <div class="form-group {{ $errors->has('date_issue') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('date_issue', 'Data wystawienia:') !!}
                  </div>
                  <div class="col-sm-6">
                    {!! Form::date('date_issue', $previousData['date_issue'], ['class' => 'form-control']) !!}
                    @if ($errors->has('date_issue'))
                      <span class="help-block"><strong>{{ $errors->first('date_issue') }} </strong></span>
                    @endif
                  </div>
                </div>

                <div class="form-group{{ $errors->has('date_payment') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('date_payment', 'Zapłata do:') !!}
                  </div>
                  <div class="col-sm-6">
                    {!! Form::date('date_payment', $previousData['date_payment'], ['class' => 'form-control']) !!}
                    @if ($errors->has('date_payment'))
                      <span class="help-block"><strong>{{ $errors->first('date_payment') }} </strong></span>
                    @endif
                  </div>
                </div>

                <div class="form-group {{ $errors->has('description') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('description', 'Opis:') !!}
                  </div>
                  <div class="col-sm-6">
                    {!! Form::text('description', $previousData['description'], ['class' => 'form-control']) !!}
                    @if ($errors->has('description'))
                      <span class="help-block"><strong>{{ $errors->first('description') }} </strong></span>
                    @endif
                  </div>
                </div>

                {{------------FIRMA------------}}

                <div class="form-group {{ $errors->has('company_id') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    <label for="company_id">Nazwa firmy</label>
                  </div>
                  <div class="col-sm-6">
                    <select name="company_id" id="company_id" class="form-control">
                      @foreach($companies as $company)
                        <option
                            value="{{$company->id }}" {{ $company->id==$previousData['company_id'] ? 'selected' : '' }}>
                          {{ $company->name }}
                        </option>
                      @endforeach
                    </select>
                    @if ($errors->has('company_id'))
                      <span class="help-block"><strong>{{ $errors->first('company_id') }} </strong></span>
                    @endif
                  </div>
                  <div class="col-sm-2">
                    <a href="{{ url('addCompany') }}" class="btn btn-success">
                      <span class="glyphicon glyphicon-plus"></span>
                    </a>
                  </div>
                </div>

                <div class="form-group">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('bank_id', 'Bank:') !!}
                  </div>
                  <div class="col-sm-6">
                    <select class="form-control control-label" name="bank_id">
                      @foreach($banks as $bank)
                        <option value="{{$bank->id}}" {{ $bank->id==$previousData['bank_id']?'selected':''}}>
                          {{$bank->label}} ({{$bank->number_bank}});
                        </option>
                      @endforeach
                    </select>
                  </div>

                  <div class="col-sm-2">
                    <a href="{{ url('addBank') }}" class="btn btn-success">
                      <span class="glyphicon glyphicon-plus"></span>
                    </a>
                  </div>
                </div>

                <div class="form-group {{ $errors->has('item_id') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('item_1_id', 'Dodaj przedmioty:') !!}
                  </div>
                  <div class="col-sm-6">
                    <select class="form-control control-label search-item">
                      @foreach($items as $item)
                        <option value="{{$item->id}}">
                          {{$item->name}}
                        </option>
                      @endforeach
                    </select>
                  </div>

                  <div class="col-sm-2">
                    <button type="submit" name="add" value="item" href="{{ url('addItem') }}" class="btn btn-success">
                      <span class="glyphicon glyphicon-plus"></span>
                    </button>
                  </div>
                </div>
                <div class="col-sm-12">
                  <div class="items-table-wr">

                    <table class="table table-hover items-table">
                      <tr>
                        <th>nazwa</th>
                        <th>vat</th>
                        <th>jednostka</th>
                        <th>cena netto</th>
                        <th>cena brutto</th>
                        <th>ilość</th>
                        <th>suma</th>
                        <th></th>
                      </tr>
                      <tbody class="items-list">
                      </tbody>
                    </table>
                  </div>
                </div>
                <div class="form-group {{ $errors->has('payment_method_id') ? ' has-error' : '' }}">
                  <div class="col-sm-4 control-label">
                    {!! Form::label('payment_method_id', 'Metoda płatności:') !!}
                  </div>
                  <div class="col-sm-6">
                    {!! Form::select('payment_method_id', $methods, ['class' => 'form-control']) !!}
                    @if ($errors->has('payment_method_id'))
                      <span class="help-block"><strong>{{ $errors->first('payment_method_id') }} </strong></span>
                    @endif
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-6 col-sm-offset-4 text-right">
                    <button type="submit" class="btn btn-primary">Zapisz proformę
                      <span class="glyphicon glyphicon-ok"></span></button>
                  </div>
                </div>
                {{--<pre>--}}
{{--                  {{print_r($previousData)}}--}}
                {{--</pre>--}}
                {!! Form::close() !!}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

我添加表单是csrf标记({{csrf_field() }})。 我不知道它是什么,有人可以帮助我吗? :)

0 个答案:

没有答案