'Laravel 8 Array to string conversion

I am facing a Laravel 8 array to string conversion. I am trying to get data from the units table, but I am getting the following error.

Array to string conversion

Unit Model

class Unit extends Model
{
    use HasFactory;
    use Uuid;

    protected $keyType = 'string';
    public $incrementing = false;
    protected $guarded = [];

    public function getKeyType()
    {
        return 'string';
    }

    public function __construct($table_name)
    {

        $this->setTable($table_name.'_units');
    }
        
    public function building()
    {
        return $this->belongsTo(Building::class);
    }
}

Controller table_name_prefix is unix time 1623748840

public function show($id)
{
    $building = Building::find($id);
    if ($building == NULL) {
        return response()->json(['message' => 'Building not exists'], 400);
    }
    
    $table_name_prefix = $building->stage->complex->table_name_prefix;
    if (Schema::hasTable($table_name_prefix.'_units')) {
        $units_table = new Unit($table_name_prefix);
        $units_table->where('status', 'Liber')->get();

        return view('buildings.show')->with('building', $building)->with('units', $units);
    } else {
        return view('buildings.show')->with('building', $building);
    }
}

Please help me to find problem. Next code is working:

$unit = new Unit($table_name_prefix);
...
$unit->save();

The next part of the Model has the error: Array to string conversion.

$this->setTable($table_name.'_units');

Screenshot of Model error

Screenshot of Controller error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source