没有明显的原因,变量从控制器到视图将变为空

时间:2018-10-29 19:00:19

标签: laravel laravel-5.5

我陷入下一个话题。帮助将不胜感激。 想要对视图进行简单的调用,但是在此过程中它变为空。最初我不是做过滤器,而是显示所有内容(notes:all())。现在,我试图按日期,ID或用户名进行过滤,这就是我在单击搜索按钮(底部)之前在左侧控件上捕获的内容。但是现在应用了过滤器(notas -sales-),它变为空... 我将代码发布给您,因此您会很友善,并帮助我确定原因。我怀疑路线(这是我手动定义路线的第一时间)。

enter image description here

路线

    Route::get('notas/notasGet/', 'NotasController@notasGet')->name('notas.notasGet');
    Route::post('notas/notasPost/', 'NotasController@notasPost')->name('notas.notasPost');
    Route::get('notas/create/', 'NotasController@create')->name('notas.create');
    Route::get('notas/store/', 'NotasController@store')->name('notas.store');
    Route::put('notas/update/{id}', 'NotasController@update')->name('notas.update');
    Route::get('notas/{id}/edit', 'NotasController@edit')->name('notas.edit');
    Route::delete('notas/destroy/{id}', 'NotasController@destroy')->name('notas.destroy');
    Route::post('notas/cajaAbrir/', 'NotasController@cajaAbrir')->name('notas.cajaAbrir');
    Route::post('notas/cajaCortar/', 'NotasController@cajaCortar')->name('notas.cajaCortar');

有问题的不是notasGet,notasPost,我首先从索引中调用

我从app.blade.php打电话给他们

<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                        <li class="nav-item">
                            <a class="nav-link" href="/gymmgr/public/registroaccesos/destinationSearchGet/">Acceso</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/gymmgr/public/notas/notasGet/">Venta</a>
                        </li>

在我的控制器上,dd($ notas);效果很好

public function notasGet()
{
    $fechaInicio = null;
    $fechaFin = null;

    $headData = array('pageTitle' => 'Admin Home - View all destinations');

    $currentDate = Carbon::now()->format('Y-m-d');

    //$notas = Nota::whereRaw('dtmHoraCargo IS NULL')->get()->first();
    $notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get();

    //dd($notas);

    $users = null;
    $users = User::all();

    $cajaAbierta = Caja::whereRaw('dtmCorte IS NULL')->get()->first();
    //dd($cajaAbierta);


    $data = array('notas'=>$notas,'fechaInicio'=>$fechaInicio,'fechaFin'=>$fechaFin, 'users'=>$users, 'caja'=>$cajaAbierta);
    return view('notas.index', $data);
}


public function notasPost(Request $request)
{

    $strSearch = $request->input('strSearch');
    $fechaInicio = $request->input('fechaInicio');
    $fechaFin = $request->input('fechaFin');
    $strPatron = $request->input('strPatron');


    $notas = null;


    switch ($strSearch) {
        case 0:
            $notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get(); 
            break;
        case 1:
                $notas = Nota::whereRaw("dtmHoraCargo >= ? AND dtmHoraCargo <= ?",  array($fechaInicio." 00:00:00", $fechaFin." 23:59:59"))->get();
            break;
        case 2:
            $notas = Nota::find($strPatron); 
            break;
        case 3:
            $notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get(); 
            break;
    }            



    $users = null;
    $users = User::all();

    $cajaAbierta = Caja::whereRaw('dtmCorte IS NULL')->get()->first();
    // dd($cajaAbierta);

    $data = array('notas'=>$notas,'fechaInicio'=>$fechaInicio,'fechaFin'=>$fechaFin, 'users'=>$users, 'caja'=>$cajaAbierta);


dd($notas); 



/*It echoes the right nota but suddenly and going to the view it's null and the view calling crashes.
    Nothing happens in between, so I don't know*/


    return view('notas.index', $data);
    //return view('registroaccesos.index', ['headData'=>$headData, 'usuarioSearch'=>$usuarioSearch]);
}

在索引

  <table class="table">
    <thead class="thead-light">

      <tr>
        <th>Folio</th>
        <th>Fecha y hora</th>
        <th>Total</th>
        <th>Aplica a</th>
        <th>Saldo</th>
     </tr>
    </thead>
    <tbody>
      @foreach($notas as $nota)
      <tr>
        <td> <a href="/gymmgr/public/notas/{{ $nota->idNota }}/edit">{{ $nota->idNota }} </a>></td>
        <td> {{ $nota->dtmHoraCargo }} </td>
        <td> {{ $nota->dcmTotal }} </td>

        <td> {{ empty($nota->idAplicaA)? '' : $nota->aplicaa->strPaterno . ' ' . $nota->aplicaa->strMaterno . ' ' . $nota->aplicaa->strNombre }} </td>
        <td> {{ $nota->dcmSaldo }} </td>                
      </tr>

      @endforeach

    </tbody>
  </table>













    </main>
  </div>
</div>

它会触发异常(notas为null,为什么????)

试图获取非对象的属性“ idNota”(查看:C:\ xampp \ htdocs \ gymmgr \ resources \ views \ notas \ index.blade.php)

1 个答案:

答案 0 :(得分:1)

在开关in case 2中,您将获得单个对象,在其他情况下,您将获得集合。因此,似乎在此case 2中发生了异常。

case 2:
        $notas = Nota::find($strPatron); // Will return object, but not the collection.
        break;

您可以尝试以下方法:

case 2:
        $notas = collect(Nota::find($strPatron)); 
        break;