scr*_*fix 5 object-property laravel laravel-5
问题:将我的 Laravel 4 代码转换为 Laravel 5.2。我正要改变和转变我的观点,但无法克服以下错误。
错误: IndexController.php 第 27 行中的 ErrorException:尝试分配非对象的属性
调试信息:
请求:请协助找出出现错误的原因,并详细解释如何防止该错误以及我在示例中做错了什么(如果可能)。
注意:在为数据库做种后,我曾经在 Laravel 4 上收到类似的错误,但我能够刷新迁移并重新为数据库做种,一切都会重新开始工作。这不适用于 Laravel 5 中的此错误。此代码适用于 L4。
尝试:我在 Google 上阅读了很多内容,并尝试了各种项目,例如php artisan clear-compiled、composer dump-autoload、php artisan optimize ,但均无济于事。我相信错误来自$numberofpcs = new extraPCs();但我无法证实这一点。我还删除了发送到视图的所有变量,但错误仍然存在,因此它看起来像$this->layout->content = View::make('index');
IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use App\Models\businesstype;
use App\Models\contractterm;
class IndexController extends BaseController
{
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->display();
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
}
}
Run Code Online (Sandbox Code Playgroud)
附加电脑.php
<?php
namespace App\Library;
use App\Library\additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
Run Code Online (Sandbox Code Playgroud)
附加计算机.php
<?php
namespace App\Library;
/** Counts up the Number of Additional PC Options
* and stores them into an array then sends them to the view.
*/
class additionalComputer {
protected function displayMenu() {
$addtpcs= [];
for ($i=0; $i <= 100; $i++) {
$addtpcs[$i] = $i;
}
return $addtpcs;
}
}
Run Code Online (Sandbox Code Playgroud)
BaseController.php - (这只是为了表明我确实有一个用于 IndexController 的 BaseController.php 文件。我知道 L5 默认情况下没有一个文件。)
<?php
namespace App\Http\Controllers;
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
Run Code Online (Sandbox Code Playgroud)
帕特里克斯帮助我找到了这个答案,但我想发布这个答案,因为当我尝试时,提供的答案仍然存在问题。但他是正确的,我不能再使用控制器布局。
我变了
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
Run Code Online (Sandbox Code Playgroud)
到
return view('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题。
| 归档时间: |
|
| 查看次数: |
9326 次 |
| 最近记录: |