【问题标题】:No query results for model []模型[]没有查询结果
【发布时间】:2014-12-26 19:30:43
【问题描述】:

当我尝试访问我的提示/索引页面时出现错误,我正在使用带有 jeffrey 方式生成器的 laravel 4 框架来加快路由、控制器、模型等的创建过程。

这是我的代码:

路线:

Route::get('tipos/index', 
    array(
        'as' => 'index',
        'uses' => 'TiposController@index')
);

型号:

class Tipo extends Eloquent{

    protected $guarded = array();

        public static $rules = array(
        'clave_tipo' => 'required',
        'nombre_tipo' => 'required',
        'status' => 'required',
        'created_by' => 'required',
    );
}

控制器:

class TiposController extends BaseController {

protected $tipo;

public function index() 
    {
        $tipos = $this->tipo->all();
        return View::make('tipos.index', compact('tipos'));
    }

我的 master.blade 中的特定路线:

{{ link_to('tipos/index', trans('common/messages.tipos'))}} |

index.blade:

@extends('layouts.master')

@section('content')
    <h1>
        Tipos
    </h1>

<form name="form" id="form" method="post">

 <a class="editar button clear" href="/sistema/crearTipo">Nuevo Tipo</a>
    <input type="button" onclick="javascript:exportar();" value="Exportar" class="button" style="margin: 0px;">
    <input type="hidden" name="format" id="format" value="yy-mm-dd">
    <input type="hidden" name="excel" id="excel" value="false">
 <br><br>

<iframe name="x" height="0" width="0" style="display: none;"></iframe>

<div class="title">
    <form name="form" id="form" method="post">
        <iframe name="x" height="0" width="0" style="display: none;"></iframe>
        <table class="datatable" id="tipos">
            <thead>
                <th>C&Oacute;DIGO</th>
                <th>DESCRIPCI&Oacute;N</th>
                <th>EDITAR</th>
                <th>ELIMINAR</th>
            </thead>

        </table>
    <div class="foot">

    </div>
    </form>
</div> 
@stop

并且出现这个错误:Illuminate \ Database \ Eloquent \ ModelNotFoundException 没有模型 [Tipo] 的查询结果。

如果有人能提供帮助,我将不胜感激!

【问题讨论】:

  • 您在数据库中没有记录。在访问视图之前处理异常。
  • 我没有在你的模特中看到protected $table = '';
  • 嗨,我在模型中添加了:protected $table = 'tipos';但仍然出现错误:没有模型 [Tipo] 的查询结果。

标签: php laravel blade laravel-routing


【解决方案1】:

您需要将您的 Tipo 模型注入到控制器中。

class TiposController extends BaseController {

    protected $tipo;

    function __construct(Tipo $tipo){
        $this->tipo = $tipo;
    }

}

不确定为什么会收到 ModelNotFoundException,因为您从未在控制器中注入模型。您的tipos 表中有记录吗?

【讨论】:

    【解决方案2】:

    已解决,我有两条提示路线:

    Route::resource('tipos', 'TiposController');
    

    Route::get('tipos/index', 
        array(
            'as' => 'index',
            'uses' => 'TiposController@index')
    );
    

    只需删除重复的路由tipos/index并放入mi master.blade:

    {{ link_to**_route**('tipos.index', trans({{'common/messages.tipos'))}} |
    

    冲突是因为我需要 mi master 中的 _route

    谢谢!

    【讨论】:

      【解决方案3】:

      我有同样的问题。我将索引路由放在单个列表之后。然后我只是将索引任务放在单个列表之前。然后它的作品。

      Route::get('index', 'TasksController@index');
      
      Route::get('/{task}', 'TasksController@single');
      

      它已连接但有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-16
        • 2018-09-12
        • 2019-09-16
        • 1970-01-01
        相关资源
        最近更新 更多