我有一个页面,显示单个测试用例的详细信息。出于某种原因,即使发送$id
,我也无法通过此错误。这是我的控制器:
public function show($id)
{
$data =DB::table('TestCase')->where('TestCaseID', $id);
return view('managements.testcase-details')->with($data);
}
这是错误:
在View.php第180行中 at HandleExceptions-> handleError('2','非法偏移类型','C:\ xampp \ htdocs \ terkwazmng \ vendor \ laravel \ framework \ src \ Illuminate \ View \ View.php','180',数组( 'key'=> object(Builder),'value'=> null))
答案 0 :(得分:4)
get
并设置数据变量名称。您的错误意味着您传递的是查询构建器而不是其结果。第二个错误是您传递NULL
值(with
中的第二个参数)。
$data =DB::table('TestCase')->where('TestCaseID', $id)->get();
return view('managements.testcase-details')->with('data', $data);
在视图中使用data
就像使用数组一样:foreach($data ...)
。
答案 1 :(得分:0)
此方法解决了我的问题,我在这里以示例方式显示-
我们要使用的类-
<?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;
}
定制的模型类,我从某处复制了函数,在这里我不能引用他,因为我无法获得从-
中获得的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);
}
}
答案 2 :(得分:0)
我在模型中添加了这个
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')); }