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

Check if a relation actually exists in a table column, otherwise assume an array should be accessed #3378

Merged

Conversation

dododedodonl
Copy link
Contributor

Post model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'data',
    ];

    protected $casts = [
        'data' => 'array',
    ];
}
<?php

namespace App\Http\Livewire;

use Filament\Tables;
use Livewire\Component;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;

class PostsTable extends Component implements Tables\Contracts\HasTable
{
    use Tables\Concerns\InteractsWithTable;

    protected function getTableColumns(): array
    {
        return [
            Tables\Columns\TextColumn::make('data.key'), // line A
            Tables\Columns\TextColumn::make('key')
                ->getStateUsing(fn (Model $record) => Arr::get($record, 'data.key')), // line B
        ];
    }

Currently, line A throws a RelationNotFoundException because queriesRelationship() does not check if the relation actually exists. With the new behaviour, a check is being done before assuming a relation exists.

  • I am not sure about the changes to HasLabel, but displaying the last part as label make more sense.
  • $this->getRecord() can return null, when it does it will be assumed no relation exists. Is that the correct behaviour?

@zepfietje zepfietje added the enhancement New feature or request label Aug 4, 2022
@danharrin danharrin merged commit b268a7b into filamentphp:2.x Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants