【问题标题】:Laravel(500- Internal server error): Unable to get data from model to controllerLaravel(500-内部服务器错误):无法从模型获取数据到控制器
【发布时间】:2017-12-01 17:05:31
【问题描述】:

我一直试图在索引页面中列出一个包含数据库数据的下拉列表。我创建了一个模型并在控制器中进行了一些更改以在我的视图页面中显示它,但是在控制器中进行任何更改都会在控制台中显示一个空白页面,其中包含 500 内部服务器错误。请帮我解决这个问题。

表名:walker_type

路线:

Route::get('/', 'WebController@index');

型号:ProviderType.php:

<?php

class ProviderType extends Eloquent {
  protected $table = 'walker_type';
}

控制器:WebController.php

 public function index() {
    $walkerTypeList = ProviderType::all();
    return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}

查看:index.php

@foreach ($walkerTypeList as $car)
  <option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
@endforeach

【问题讨论】:

  • 请检查您的错误日志是什么导致了 500 错误。如果您在开发人员机器上运行代码,您还可以启用调试以显示错误消息而不是 500 页面。只是快速浏览一下您的代码 sn-ps,我最初的假设是您缺少 WalkerType 中 SoftDeletingTrait 的 use 语句,但最好查看错误消息以确定。
  • 你试过with('walkerTypeList', $walkerTypeList);吗?
  • dd($walkerTypeList) 在您的控制器中返回视图之前,看看它是否首先被填充。
  • @dbrumann 我发现了我的一个错误。已经有一个用于相同表名 ProviderType 的模型。所以我只是在控制器的索引函数中将模型名称替换为相同的名称。然后调试它显示了结果。但是我仍然无法将变量传递给查看。

标签: laravel


【解决方案1】:

你有没有在你的命名空间下声明你的模型?

例如。 use App\WalkerType;

你也忘了给你的模型声明一个命名空间。

它应该有namespace App;

或者,如果您有一个文件夹来存放您的模型以使其更传统。

您应该在每个模型上都有一个命名空间。

例如。 App\Model

然后在你的命名空间和类之间通过 declarin 在每个控制器中使用它

例如。

namespace App\Controllers;

use App\Model\WalkerType;

class SomeController extends Controller{
     protected $data;  //this is a class variable that can call anywhere to your class by calling it this way $this->data

     public function some_method(){

         $this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a

        $this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum

         return view('someview',$this->data);
     }


}

我希望这可以有效地帮助您的项目,因为我们曾经经历过我们忘记包含一些已在控制器上处理并需要显示在您的视图文件中但忘记包含的数据。

【讨论】:

  • 我在调试时在控制器中得到结果,但无法将其传递到视图页面。
  • 我将编辑我的答案,您可以大致了解我如何在视图文件中传输数据。
  • 你可以试试 View::make('someview') 方法
  • 当我写的时候:return View::make('website.index');它有效但返回 view("website.index")->with(["walkerTypeList" => $walkerTypeList]);给出错误
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 2014-10-25
  • 2015-08-12
  • 2017-04-12
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多