Skip to content

[6.x] Call to a member function getCreatedAtColumn() on string #30508

@maguilar92

Description

@maguilar92
  • Laravel Version: 6.4.1
  • PHP Version: 7.3.1
  • Database Driver & Version: Mysql 5.6

Description:

When using sync on relationship with using pivot class Call to a member function getCreatedAtColumn() on string is throwed.

Steps To Reproduce:

  • As on my case, create a many to many relationship in mysql, for example products, categories and product_category with timestamps.

  • Create the following models:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';
    protected $guarded = ['id'];
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'products';
    protected $guarded = ['id'];

    public function categories()
    {
        return $this->belongsToMany(Category::class, (new ProductCategory)->getTable())
            ->using(ProductCategory::class)
            ->withTimestamps();
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class ProductCategory extends Pivot
{
    protected $table = 'product_category';
    public $pivotParent = Product::class;
    protected $foreignKey = 'product_id';
    protected $relatedKey = 'category_id';
}
  • Attempt to sync product with categories:
$product = new Product();
$product->save();
$category = new Category();
$category->save();
$product->categories()->sync([$category->id]);

Exception trace:

Error : Call to a member function getCreatedAtColumn() on string
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php:222
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php:211
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php:45
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php:76
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1077
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:499
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:642
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:330
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:309
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:282
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:257
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:167
 /var/www/jet8-core/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php:119

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions