'laravel Illegal offset type error
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:
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))
Solution 1:[1]
You forgot a little bit. A get and 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 NULL value (second param in with).
$data =DB::table('TestCase')->where('TestCaseID', $id)->get();
return view('managements.testcase-details')->with('data', $data);
In view use data like you use an array: foreach($data ...).
Solution 2:[2]
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 -
<?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);
}
}
Solution 3:[3]
i added this in model
<pre>
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'));
}
</pre>
Solution 4:[4]
If you want to return an specific id record use this
use app\Model;
public function show($id){
$data =Model::select('n.data')->findOrFail($id);
return view('managements.testcase-details')->with($data);
}
Solution 5:[5]
my problem: My table had no auto-increment column and the laravel was trying to access the auto-increment column because Laravel assume every table has an auto-increment id, so Laravel sent me this error.
solution : add public $incrementing = false; to your Model Class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sadidul Islam |
| Solution 3 | It's VIN |
| Solution 4 | |
| Solution 5 | Mohammadjan Naser |
