laravel Illegal offset type error

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


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

Tags:

Php

Laravel