rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.7k stars 320 forks source link

[Bug]: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous #1655

Closed qcodeinfotech closed 3 months ago

qcodeinfotech commented 4 months ago

What happened?

Getting Column 'user_id' in where clause is ambiguous error even after setting the proper configuration.

Note :

When i remove the project.name from the column then it's working again. i don't want to use the join too.

TasksTable.php

<?php

namespace App\Livewire\Company;

use App\Livewire\BaseDatatableComponent;
use App\Models\Task;
use App\Models\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Component;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;

class TasksTable extends DataTableComponent
{
    public ?string $defaultSortColumn = 'created_at';
    protected $model = Task::class;

    public function configure(): void
    {
        $this->setPrimaryKey('id');
    }

    public function columns(): array
    {
        return [
            Column::make("Title", 'title')->searchable()->sortable(),
            Column::make("Project", 'project.name'),

            Column::make("Created At", 'created_at')->format(
                fn ($value, $row, Column $column) => Carbon::parse($value)->diffForHumans()
            )->sortable(),

            Column::make("Action", 'id')->format(
                fn ($value, $row, Column $column) => view('components.actions', [
                    'showUrl' => route('company.tasks.show', $row->id),
                    'editUrl' => route('company.tasks.edit', $row->id),
                    'deleteUrl' => route('company.tasks.destroy', $row->id),
                    'recordId' => $row->id,
                ])
            )->html(),
        ];
    }

    public function builder(): Builder
    {
        $query = Task::with('project')->user(Auth::id())->select('*');

        return $query;
    }
}

Task.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    use HasFactory, DefaultUserTrait;

    protected $table = 'tasks';

    protected $fillable =  [
        'title',
        'status_id',
        'project_id',
        'due_date',
        'description',
        'user_id'
    ];

    protected $casts = [
        'user_id' => 'integer'
    ];

    public function users()
    {
        return $this->belongsToMany(User::class, 'task_users', 'task_id', 'user_id');
    }

    public function project()
    {
        return $this->belongsTo(Project::class, 'project_id', 'id');
    }
}

How to reproduce the bug

No response

Package Version

^3.2

PHP Version

8.2.x

Laravel Version

10.X

Alpine Version

No response

Theme

None

Notes

No response

Error Message

[Bug]: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous

stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.