尝试分配非对象的属性 - Laravel 5.2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35051748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:08:07  来源:igfitidea点击:

Attempt to assign property of non-object - Laravel 5.2

laravellaravel-5.2object-property

提问by scrfix

Issue:Converting my Laravel 4 code to Laravel 5.2. I'm at the point of moving and converting my views and cannot get past the following error.

问题:将我的 Laravel 4 代码转换为 Laravel 5.2。我正在移动和转换我的视图,无法克服以下错误。

Error:ErrorException in IndexController.php line 27: Attempt to assign property of non-object

错误:IndexController.php 第 27 行中的 ErrorException:尝试分配非对象的属性

Debug Information:

调试信息:

  1. in IndexController.php line 27
  2. at HandleExceptions->handleError('2', 'Attempt to assign property of non-object', 'C:\Apache24\B2B_Contracts\app\Http\Controllers\IndexController.php', '27', array('numberofpcs' => object(additionalPCs), 'addtpcs' => array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100'))) in IndexController.php line 27
  3. at IndexController->index()
  1. 在 IndexController.php 第 27 行
  2. 在 HandleExceptions->handleError('2', '尝试分配非对象的属性', 'C:\Apache24\B2B_Contracts\app\Http\Controllers\IndexController.php', '27', array('numberofpcs' = > object(additionalPCs), 'addtpcs' => array('0', '1', '2', '3', '4', '5', '6', '7', '8', ' 9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21' , '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', ' 34, '35', '36', '37', '38', '39', '40', '41',“42”、“43”、“44”、“45”、“46”、“47”、“48”、“49”、“50”、“51”、“52”、“53”、“54” ', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', “67”、“68”、“69”、“70”、“71”、“72”、“73”、“74”、“75”、“76”、“77”、“78”、“79” ', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', IndexController 中的“92”、“93”、“94”、“95”、“96”、“97”、“98”、“99”、“100”)))。php 第 27 行
  3. 在 IndexController->index()

Request:Please assist in figuring out why the error comes up and please explain in detail how to prevent it and what I am doing wrong with example, if possible.

请求:请协助找出错误出现的原因,如果可能,请详细解释如何防止它以及我在示例中做错了什么。

Notes:I used to receive a similar error on Laravel 4 after I would seed the database but I was able to refresh the migrations and reseed the database and everything would start working again. This does not work with this error in Laravel 5. This code works on L4.

注意:我曾经在 Laravel 4 上为数据库设置种子后收到类似的错误,但我能够刷新迁移并重新设置数据库,一切都会重新开始工作。这不适用于 Laravel 5 中的此错误。此代码适用于 L4。

Attempts:I have been reading a lot on Google and have tried various items such as php artisan clear-compiled, composer dump-autoload, php artisan optimizeto no avail. I believe the error is coming from $numberofpcs = new additionalPCs(); however I have not been able to confirm that. I have also removed all of the variables I am sending to the view and the error still persisted so it looked like $this->layout->content = View::make('index');

尝试:我在谷歌上阅读了很多,并尝试了各种项目,如php artisan clear-compiledcomposer dump-autoloadphp artisan optimize都无济于事。我相信错误来自$numberofpcs = new additionalPCs(); 但是我无法证实这一点。我还删除了我发送到视图的所有变量,但错误仍然存​​在,所以它看起来像$this->layout->content = View::make('index');

IndexController.php

索引控制器.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());
    }
}

additionalPCs.php

附加PCs.php

<?php
namespace App\Library;

use App\Library\additionalComputer;

class additionalPCs extends additionalComputer {
    public function display() {
        return $this->displayMenu();    
    }
}

additionalComputer.php

附加计算机.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;
        }

    }

BaseController.php- (This is only here to show that I do have a BaseController.php file for the IndexController. I'm aware that L5 doesn't come with one by default.)

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);
        }
    }

}

采纳答案by scrfix

Patricus assisted me with finding this answer but I wanted to post this because the answer provided still had issues when I attempted it. He was correct however in that I could no longer utilize Controller Layouts.

Patricus 帮助我找到了这个答案,但我想发布这个,因为在我尝试时提供的答案仍然存在问题。然而他是对的,因为我不能再使用控制器布局。

I changed

我变了

$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());

to

return view('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown()); 

This resolved that issue.

这解决了这个问题。

回答by patricus

The controller layout functionality was removed in Laravel 5. Because of this, the setupLayout()method is not getting called, meaning your layoutproperty is staying as just the string 'master'. Since it is just a string, when you do $this->layout->content = ..., you're attempting to assign a property of a non-object.

Laravel 5 中删除了控制器布局功能。因此,该setupLayout()方法不会被调用,这意味着您的layout属性只是字符串“master”。因为它只是一个字符串,所以当你这样做时$this->layout->content = ...,你试图分配一个非对象的属性。

I believe you should be able to add this back in if you want. In your BaseController, you will need to override the callActionmethod to the way it used to be:

我相信如果您愿意,您应该能够重新添加它。在您的 中BaseController,您需要将callAction方法重写为以前的方式:

public function callAction($method, $parameters)
{
    $this->setupLayout();

    $response = call_user_func_array(array($this, $method), $parameters);

    // If no response is returned from the controller action and a layout is being
    // used we will assume we want to just return the layout view as any nested
    // views were probably bound on this view during this controller actions.

    if (is_null($response) && ! is_null($this->layout)) {
        $response = $this->layout;
    }

    return $response;
}