0

I have this code

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Stevebauman\Location\Drivers\Driver;
use Stevebauman\Location\Exceptions\DriverDoesNotExistException;

Class CRUD extends Eloquent {
protected $collection;
   public function __construct($collection ,array $attributes = array())
   {

  parent::__construct($attributes);

       $this->collection = $collection;

   }

}

when I am using this code in order to call the constructor , I get nothings $device_mode=new CRUD('HW101950054393'); because as I noticed when I trying to echo the $collection variable inside the constructor I get this error

Array to string conversion

I don't understand that as I am passing the variable as string but the model working with it as array. why is that happening, and how can I solve it

M.Bwe
  • 159
  • 2
  • 4
  • 16

1 Answers1

0

Your code should work. Double check your if code is identical to what you posted.

I am using Laravel 5.6 and this is working without problems:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    protected $collection;

    public function __construct($collection, array $attributes = array())
    {
        parent::__construct($attributes);

        $this->collection = $collection;
    }
}

Calling

Route::get('/test', function () {
    $test = new \App\Test('1234');
}

works fine.

Adam
  • 25,960
  • 22
  • 158
  • 247
  • @M.Bwe I added how I called the test class. I simply added a route and called it though browser `mydomain.dev/test` – Adam Feb 11 '20 at 12:53
  • ok , thank u , Still not working for me , thank you for your help – M.Bwe Feb 11 '20 at 13:02
  • when I am using this code in order to call the constructor , I get nothings $device_mode=new CRUD('HW101950054393'); because as I noticed when I trying to echo the $collection variable inside the constructor I get this error > Array to string conversion I don't understand that as I am passing the variable as string but the model working with it as array. why is that happening, and how can I solve it – M.Bwe Feb 11 '20 at 13:25
  • @M.Bwe not sure, maybe collection name is used in `Jenssegers\Mongodb\Eloquent\Model`. You could try to rename your attribut `collection` to `test` or something – Adam Feb 11 '20 at 13:29
  • @M.Bwe, you cant echo an array, use `var_dump` or `dd` (stops execution immediately) to see their content safely. – Bagus Tesa Feb 11 '20 at 13:31
  • @Adam I changed the name from collection to test , but when I write echo $test , the result is HW101950054393Array which is mix of string and array even I called it using this command $device_mode=new CRUD('HW101950054393') which is only string – M.Bwe Feb 11 '20 at 13:36