php laravel 非法偏移类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45780159/
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-08-26 03:08:51  来源:igfitidea点击:

laravel Illegal offset type error

phplaravel

提问by Mohammad hayajneh

I have a page that shows the details of a single test case. For some reason, I can't get past this error, even to send the $id. Here's my controller:

我有一个页面显示单个测试用例的详细信息。出于某种原因,我无法克服这个错误,即使发送$id. 这是我的控制器:

public function show($id)
{
    $data =DB::table('TestCase')->where('TestCaseID', $id);
    return view('managements.testcase-details')->with($data);
}

Here's the error:

这是错误:

in View.php line 180 at HandleExceptions->handleError('2', 'Illegal offset type', 'C:\xampp\htdocs\terkwazmng\vendor\laravel\framework\src\Illuminate\View\View.php', '180', array('key' => object(Builder), 'value' => null))

在 View.php 第 180 行的 HandleExceptions->handleError('2', 'Illegal offset type', 'C:\xampp\htdocs\terkwazmng\vendor\laravel\framework\src\Illuminate\View\View.php', ' 180', array('key' => object(Builder), 'value' => null))

回答by shukshin.ivan

You forgot a little bit. A getand to set up data variable name. Your error means, that you pass a query builder rather than its results. The second error is that you passing a NULLvalue (second param in with).

你忘记了一点。Aget和设置数据变量名。您的错误意味着您传递的是查询构建器而不是其结果。第二个错误是您传递了一个NULL值(第二个参数 in with)。

$data =DB::table('TestCase')->where('TestCaseID', $id)->get();
return view('managements.testcase-details')->with('data', $data);

In view use datalike you use an array: foreach($data ...).

在视图中使用data就像您使用数组一样:foreach($data ...)

回答by Sadidul Islam

This method solve my problem, i am showing it here as an example -

这个方法解决了我的问题,我在这里展示它作为一个例子 -

Class that we want to use -

我们想使用的类 -

<?php

namespace App;

use App\Helpers\ModelMPK; //MPK stands for Multi-column Primary Key handling

class AccountSession extends ModelMPK
{
    protected $hidden = ["account_id", "id"];
    protected $primaryKey = ['account_id', 'session'];
    public $incrementing = false;
}

Customized model class, I copied the function from somewhere, i can't refer him here because I can't resource URL I get this from -

自定义模型类,我从某处复制了该函数,我无法在这里引用他,因为我无法获取此 URL 的资源 URL -

<?php

namespace App\Helpers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class ModelMPK extends Model
{
    /**
     * Set the keys for a save update query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();
        if(!is_array($keys)){
            return parent::setKeysForSaveQuery($query);
        }

        foreach($keys as $keyName){
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
        }

        return $query;
    }

    /**
     * Get the primary key value for a save query.
     *
     * @param mixed $keyName
     * @return mixed
     */
    protected function getKeyForSaveQuery($keyName = null)
    {
        if(is_null($keyName)){
            $keyName = $this->getKeyName();
        }

        if (isset($this->original[$keyName])) {
            return $this->original[$keyName];
        }

        return $this->getAttribute($keyName);
    }
}

回答by DIO

i added this in model

我在模型中添加了这个

        namespace App;
        use Illuminate\Database\Eloquent\Model;
        use Illuminate\Database\Eloquent\Builder;

        class holding extends Model
        {
            public $timestamps = false;
            public $incrementing = false;
          public $keyType = 'string';
            protected $table = 'tb_holding';
            protected $primaryKey = ['qsymbol','id_user'];
            protected $fillable = ['qsymbol','qlotbuy','qbuyprice','qstoploss','qlaststopls','qbuydate','idnote','id_user'];


            //---> Illegal offset type while updating model 
            //---> because primary key more than 1 --> add this
            //https://laracasts.com/discuss/channels/laravel/illegal-offset-type-while-updating-model?page=1
            protected function setKeysForSaveQuery(Builder $query)
            {
                return $query->where('qsymbol', $this->getAttribute('qsymbol'))
                             ->where('id_user', $this->getAttribute('id_user'));
            }