Mohammad hayajneh
Mohammad hayajneh

Reputation: 674

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

Upvotes: 13

Views: 63603

Answers (6)

Eloy Ruiz
Eloy Ruiz

Reputation: 767

In my case was an error passing the view_args to the MailDispatcher:

$booking = Booking::find(25);
$details = [
    'to' => env('MAIL_DEBUG') ? env('MAIL_DEBUG_ADDRESS') : $to,
    'subject' => env('MAIL_DEBUG') ? "[Original recipient: $to] $subject" : $subject,
    'view' => $view,
    'view_args' => $booking //error
];
dispatch(new MailDispatcher($details));

The parameter should be an array like this:

'view_args' => [ 'booking' => $booking ]

Upvotes: 0

Mohammadjan Naser
Mohammadjan Naser

Reputation: 246

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

Upvotes: 6

Jonathan Otero
Jonathan Otero

Reputation: 1

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

Upvotes: 0

DIO
DIO

Reputation: 1

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>

Upvotes: 0

Sadidul Islam
Sadidul Islam

Reputation: 1348

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

Upvotes: 8

shukshin.ivan
shukshin.ivan

Reputation: 11340

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

Upvotes: 11

Related Questions