Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation 'getCampus' is not instance of HasOne or BelongsTo. #174

Open
kiyu92 opened this issue Feb 3, 2021 · 2 comments
Open

Relation 'getCampus' is not instance of HasOne or BelongsTo. #174

kiyu92 opened this issue Feb 3, 2021 · 2 comments

Comments

@kiyu92
Copy link

kiyu92 commented Feb 3, 2021

I have defined the relationship in the model:

use Kyslik\ColumnSortable\Sortable;
use EloquentFilter\Filterable;

class Building extends Model
{
    use HasFactory;
    use Sortable;
    use Filterable;
    public $sortable = ['id', 'code', 'description', 'created_at' , 'updated_at'];
    
    public static function getTableName()
    {
        return (new self())->getTable();
    }
    
    //$product->getCampus
    public function getCampus(){
        return $this->belongsTo(Campus::class, 'campus', 'id' );
    }

}

And when y sort by getCampus.id

Kyslik\ColumnSortable\Exceptions\ColumnSortableException
Relation 'getCampus' is not instance of HasOne or BelongsTo.
http://localhost/smart_clean_urv/public/buildings?direction=desc&sort=getCampus.id

If i not define the keys in belongsTo relationship:

use Kyslik\ColumnSortable\Sortable;
use EloquentFilter\Filterable;

class Building extends Model
{
    use HasFactory;
    use Sortable;
    use Filterable;
    public $sortable = ['id', 'code', 'description', 'created_at' , 'updated_at'];
    
    public static function getTableName()
    {
        return (new self())->getTable();
    }
    
    //$product->getCampus
    public function getCampus(){
        return $this->belongsTo(Campus::class);
    }

}

I have and a MYSQL error becouse my key is not campus_id , is only campus:

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'buildings.get_campus_id' in 'on clause' (SQL: select count(*) as aggregate from `buildings` left join `campuses` on `buildings`.`get_campus_id` = `campuses`.`id`)
http://localhost/smart_clean_urv/public/buildings?direction=desc&sort=getCampus.id

My Laravel version is 8.25.0

Thanks and sorry for my English!

@Kyslik
Copy link
Owner

Kyslik commented Mar 25, 2021

How does @sortablelink in view look like - https://github.com/Kyslik/column-sortable#blade-and-relation-sorting.

@mikeda37
Copy link

mikeda37 commented May 3, 2023

@kiyu92
Have you already solved this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants