【问题标题】:Laravel - Undefined variable: vendedor (View: C:\wamp64\www\oficina2.0\oficina2.0\oficina\resources\views\index.blade.php)Laravel - 未定义变量:供应商(查看:C:\wamp64\www\oficina2.0\oficina2.0\oficina\resources\views\index.blade.php)
【发布时间】:2020-08-18 06:21:57
【问题描述】:

我正在使用 Laravel,我在视图中收到此错误未定义变量,我将 3 个变量发送到视图并说此变量未声明

这是我的看法

@extends('templates.template')
@section('content')

@csrf
<table class="table table-striped table-dark">
<h2 class="text-center font-weight-bold mb-4">Orçamentos</h2>
<br>
  <tr>
    <td>
     <select data-column="0" class="form-control filter-input">
        <option value="">Vendedores</option>
        @foreach($vendedor as $obj)
        <option value="{{$obj}}">{{$obj}}</option>
        @endforeach
     </select>
    </td>
    <td>
      <input type="text" class="form-control filter-input" placeholder="Procurar pelo cliente" data-column="8">
    </td>
    <td>
      <input type="date" class='form-control filter-input' placeholder="Procurar pelo cliente" data-column="2">
    </td>
  </tr>
  <thead>
    <tr>
      <th  scope="col">Vendedor</span></th>
      <th  scope="col">Cliente</span></th>
      <th  scope="col">Data </span></th>
      <th  scope="col">Valor</th>
      <th   scope="col">
      <a href="{{url('orcamentos/create')}}">
        <button type="button" class="btn btn-light mb-2"><i class="fas fa-plus"></i> Adicionar </button>
      </a>
      </th>
    </tr>
  </thead>
  <tbody>
      

这是控制器函数,我在其中发送按视图中的函数排序的数据

 public function index()
 {
   $orcamentos = ModelsOrcamentoModel::get();
   $vendedores = ModelsOrcamentoModel::sortBy('vendedor')->pluck('vendedor')->unique();
   $clientes = ModelsOrcamentoModel::sortBy('cliente')->pluck('cliente')->unique();
   $data = ModelsOrcamentoModel::sortBy('created_at');

   return view('index')->with('orcamento',$orcamentos,'vendedor',$vendedores,'cliente',$clientes,'data',$data);
 }

【问题讨论】:

  • 你的方法不对,试试return view('index', compact('orcamento','vendedor','cliente',,'data'));
  • return view('index')->with(array('orcamento' => $orcamento, 'vendor' => $vendor, 'cliente' => $clienter, 'data' => $数据));也试试这个
  • 现在又给我一个错误Call to undefined method App\Models\OrcamentoModel::sortBy()

标签: php laravel undefined-variable


【解决方案1】:
return view('index', [
  'orcamento' => $orcamentos,
  'vendedor'  => $vendedores,
  'cliente'   => $clientes,
  'data'      => $data
]);

【讨论】:

  • 现在给我另一个错误 Call to undefined method App\Models\OrcamentoModel::sortBy()
  • 更改 ModelsOrcamentoModel::all()-&gt;sortBy('vendedor') 而不是 ModelsOrcamentoModel::sortBy('vendedor')
【解决方案2】:

在控制器中传递多个变量以像这样查看,

return view('index')->with(array('orcamento' => $orcamento, 'vendedor' => $vendedor, 'cliente' => $clienter, 'data' => $data));

return view('index')->with(compact('orcamento', 'vendedor', 'cliente', 'data'));

return view('index', compact(['orcamento', 'vendedor', 'cliente', 'data']));

【讨论】:

  • 现在给我另一个错误 Call to undefined method App\Models\OrcamentoModel::sortBy()
  • sortby 用于集合
  • ModelsOrcamentoModel::pluck('vendor')->unique()->sortBy('vendor');
猜你喜欢
  • 2019-08-06
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多