Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Illegal offset type error

Tags:

php

laravel

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

like image 865
Mohammad hayajneh Avatar asked Aug 20 '17 08:08

Mohammad hayajneh


2 Answers

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 ...).

like image 126
shukshin.ivan Avatar answered Nov 03 '22 14:11

shukshin.ivan


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);
    }
}
like image 41
Sadidul Islam Avatar answered Nov 03 '22 12:11

Sadidul Islam