【问题标题】:Missing required parameters for [Route: proforms.edit] [URI: proforms/{proform}/edit][Route: proforms.edit] [URI: proforms/{proform}/edit] 缺少必需的参数
【发布时间】:2020-12-08 15:21:47
【问题描述】:

我认为我正在发送所有重要的变量来查看,但没有。我不知道为什么会收到此错误:缺少 [Route: proforms.edit] [URI: proforms/{proform}/edit] 所需的参数。 在这行代码上:return redirect()->route('proforms.edit', compact('proform', 'query'))

这是控制器方法:

  public function destroy2(Proform $proform, $query2)
        {
          $query = DB::table('dynamic_fields')
          ->join('proforms', 'dynamic_fields.proform_id', '=', 'proforms.id')
          ->select('dynamic_fields.*','proforms.*')
          ->where('proform_id', '=', $proform->id)
          ->get();  
        
          DB::table('dynamic_fields')->where('id_pozycji', '=', '$query2')->delete();
        
        
        return redirect()->route('proforms.edit', compact('proform', 'query'))
        ->with('success','Product deleted successfully');
        }
        }

这是视图 proforms.edit 的所有代码,它会生成该错误。

@extends('layouts.app')


@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edytuj produkt</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('proforms.index') }}"> Wstecz</a>
            </div>
        </div>
    </div>


    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif


    <form action="{{ route('proforms.update',$proform->id) }}" method="POST">
        @csrf
        @method('PUT')


         <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Data wystawienia:</strong>
                    <input type="text" name="proformdate" value="{{ $proform->proformdate }}" class="form-control" placeholder="Data wystawienia">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Kontrahent:</strong>
                                        <br>
                    <select class="form-controll" name="user_id">
                            @foreach($users as $user)
                            <option value="{{$user->id}}">{{$user->showname}}</option>
                            @endforeach
                    </select>
                    <br>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Waluta:</strong>
                                        <br>
                    <select class="form-controll" name="currency_id">
                            @foreach($currencys as $currency)
                            <option value="{{$currency->id}}">{{$currency->currency}}</option>
                            @endforeach
                    </select>
                    <br>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Forma płatności:</strong>
                                        <br>
                    <select class="form-controll" name="form_id">
                            @foreach($forms as $form)
                            <option value="{{$form->id}}">{{$form->form}}</option>
                            @endforeach
                    </select>
                    <br>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Data sprzedaży:</strong>
                    <input type="text" name="selldate" value="{{ $proform->selldate }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Termin płatności:</strong>
                    <input type="text" name="paymentdate" value="{{ $proform->paymentdate }}" class="form-control" placeholder="Termin płatności">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Forma płatności:</strong>
                    <input type="text" name="paymentmethod" value="{{ $proform->paymentmethod }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Miejsce wystawienia:</strong>
                    <input type="text" name="city" value="{{ $proform->city }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Status:</strong>
                    <input type="text" name="status" value="{{ $proform->status }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Uwagi:</strong>
                    <input type="text" name="name" value="{{ $proform->comments }}" class="form-control" placeholder="Uwagi">
                </div>
            </div>
            <div class="pull-left" style="margin: 35px;">
                <h3>Pozycje faktury</h3>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Nazwa towaru lub usługi:</strong>
                    <input type="text" name="name" value="{{ $proform->name }}" class="form-control" placeholder="Data wystawienia">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>PKWiU:</strong>
                    <input type="text" name="PKWIU" value="{{ $proform->PKWIU }}" class="form-control" placeholder="Kontrahent">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Ilość:</strong>
                    <input type="text" name="quantity" value="{{ $proform->quantity }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Jednostka:</strong>
                    <input type="text" name="unit" value="{{ $proform->unit }}" class="form-control" placeholder="Termin płatności">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Cena netto jednostki:</strong>
                    <input type="text" name="netunit" value="{{ $proform->netunit }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Netto razem:</strong>
                    <input type="text" name="nettotal" value="{{ $proform->nettotal }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Stawka VAT:</strong>
                    <input type="text" name="VATrate" value="{{ $proform->VATrate }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Brutto jednostka:</strong>
                    <input type="text" name="grossunit" value="{{ $proform->grossunit }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Brutto razem:</strong>
                    <input type="text" name="grosstotal" value="{{ $proform->grosstotal }}" class="form-control" placeholder="Status">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
              <button type="submit" class="btn btn-primary">Zapisz</button>
            </div>
        </div>


    </form>

  
    <table class="table table-bordered">
        <tr>

            <th scope="col">@sortablelink('id', 'Numer')</th>
            <th scope="col">@sortablelink('name', 'Nazwa pozycji')</th>
            <th scope="col">@sortablelink('PKWIU', 'PKWiU')</th>
            <th scope="col">@sortablelink('quanity', 'Ilość')</th>
            <th scope="col">@sortablelink('unit', 'Jednostka')</th>
            <th scope="col">@sortablelink('netunit', 'Jednostka netto')</th> 
            <th scope="col">@sortablelink('nettotal', 'Razem netto')</th> 
            <th scope="col">@sortablelink('VATrate', 'Stawka VAT')</th>
            <th scope="col">@sortablelink('grossunit', 'Brutto jednostka')</th> 
            <th scope="col">@sortablelink('grosstotal', 'Brutto razem')</th>  
            <th width="280px">Akcja</th>
        </tr>
        @foreach ($query as $query2)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $query2->name }}</td>
            <td>{{ $query2->PKWIU }}</td>
            <td>{{ $query2->quantity }}</td>
            <td>{{ $query2->unit }}</td>
            <td>{{ $query2->netunit }}</td>
            <td>{{ $query2->nettotal }}</td>
            <td>{{ $query2->VATrate }}</td>
            <td>{{ $query2->grossunit }}</td>
            <td>{{ $query2->grosstotal }}</td>
           
            <td>
                <form action="{{ route('proforms2.destroy2',$query2->id_pozycji) }}" method="GET">
                    
                    @can('product-edit')
                    <a class="btn btn-primary" href="{{ route('proforms.edit',$query2->id_pozycji) }}">Edytu</a>
                    @endcan


                    @csrf
                    @method('GET')
                    @can('product-delete')
                    <button type="submit" class="btn btn-danger">Usuń</button>
                    @endcan
                </form>
            </td>
        </tr>
        @endforeach
    </table>

 

<p class="text-center text-primary"><small>ARTplus 2020</small></p>
@endsection

这是视图和控制器的路由:

Route::get('delete/proforms/{query2}','ProformController@destroy2')->name('proforms2.destroy2');

【问题讨论】:

  • 你需要通过这条路由proforms.edit'传递id参数,但我认为你刚刚删除了这个id,所以如果你传递id参数那么它就不起作用了,所以重定向到另一个路由,比如@ 987654326@
  • 我没有删除 id。 $proforom->id 没有从视图发送到这个控制器。在 $proform 中,我只有带有列名的空数组。但是当我将 $proform 添加到路由时,我收到错误 404。

标签: php laravel frameworks web-frameworks


【解决方案1】:

您的错误表明:

缺少 [Route: proforms.edit] [URI:proforms/{proform}/edit] 的必需参数。

请注意,您应该将proform 传递给路由重定向器

所以你应该改变

return redirect()->route('proforms.edit', compact('proform', 'query'))
        ->with('success','Product deleted successfully');

return redirect()->route('proforms.edit', // [$id] or $id )
        ->with('success','Product deleted successfully');

【讨论】:

  • 类似的东西?它对我不起作用。 return redirect()->route('proforms.edit', compact('proform', 'query', 'proform->id')) ->with('success','产品删除成功');
  • 没有像eturn redirect()-&gt;route('proforms.edit',$proform-&gt;id ) -&gt;with('success','Product deleted successfully'); 这样的东西,在重定向到路由时忘记紧凑
  • 这行不通,因为他们刚刚删除了这个$id
  • 那你不能重定向到编辑路由,最好重定向到索引路由
  • 它没有用。但我检查了 $proform 数组,它没有值,只有列名。为什么它不是sneding。我只删除了查询中的值,而不是形式 ID。但是为什么没有从视图发送形式 ID。我的路线看起来像这样,但如果我向它添加形式,它会返回错误 404 找不到页面。 Route::get('delete/proforms/{query2}','ProformController@destroy2')->name('proforms2.destroy2');
【解决方案2】:

你不应该使用 compact 方法来传递参数,因为它通常用于将数据传递给视图,所以你应该写这样的东西:

return redirect()->route('proforms.edit',[$proform,$query])

【讨论】:

  • 问题是视图正在向这个控制器发送空数组 $proform。它只有列名。我必须将 $proforom 发送到此控制器 ID,这样就可以了。但是如何做到这一点。
  • 实际上您的错误是缺少必需的参数,这意味着您传递的参数数量错误。要么你传递一个参数,它接收到 2 个参数
【解决方案3】:

你命名路由proforms.editid参数是必须的,所以你需要传递参数。 捷径是:

 return redirect()->back()->with('proform', $proform)->with('query', $query)
        ->with('success','Product deleted successfully');

另一种方法是将参数作为隐藏字段传递。 在您的刀片上,将此 input 字段添加到您的 form 元素中:

<input type="hidden" name="proform_id" value="{{ $proform->id }}">

那么你的控制器代码将是这样的:

return redirect()->route('proforms.edit', $request->proform_id)
  ->with('proform', $proform)
  ->with('query', $query)
  ->with('success','Product deleted successfully');

【讨论】:

    【解决方案4】:

    您应该将执行名称传递给每个执行过的路线,如下例所示:

       <form action="{{ route('proforms.update',['proform'=>$proform->id)] }}" method="POST">
            @csrf
            @method('PUT')
    

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2022-01-16
      • 2021-03-10
      • 2020-08-17
      相关资源
      最近更新 更多