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

Declaration of Column::data does not compatible with Fluent::data #3201

Closed
fdteam-deanie opened this issue Nov 26, 2024 · 20 comments · Fixed by yajra/laravel-datatables-html#230

Comments

@fdteam-deanie
Copy link

fdteam-deanie commented Nov 26, 2024

Summary of problem or feature request

Error

Declaration of Yajra\DataTables\Html\Column::data(array|string $value): static must be compatible with Illuminate\Support\Fluent::data($key = null, $default = null)

Code snippet of problem

File vendor/yajra/laravel-datatables-html/src/Html/Column.php, function data (line 305 - 310) was the reason:

    public function data(array|string $value): static
    {
        $this->attributes['data'] = $value;

        return $this;
    }

System details

  • Operating System: Mac OS 15
  • PHP Version: 8.3
  • Laravel Version: laravel/framework ^11.34
  • Laravel-Datatables Version: yajra/laravel-datatables ^11.0

Solution

File vendor/yajra/laravel-datatables-html/src/Html/Column.php, change function data (line 305 - 310) to:

    public function data($key = null, $default = null): static
    {
        $this->attributes['data'] = $key;

        return $this;
    }
@alberto-morais-oliveira-f
Copy link

alberto-morais-oliveira-f commented Nov 26, 2024

I have the same problem, this was caused by the update in Laravel 11.34

@madmouse17
Copy link

madmouse17 commented Nov 27, 2024

I have the same problem now,
please solve my problem
Laravel 11.34

image

@madmouse17
Copy link

While I commented on the function, and my website is running normally again, please improve the function

image

@asheekofficial
Copy link

any updates? having this issue from today

@asoltec
Copy link

asoltec commented Nov 27, 2024

same error with me

@OzanKurt
Copy link
Contributor

Here is the update that broke many projects:

laravel/framework#53665

@OzanKurt
Copy link
Contributor

The solution is avoiding the error but having a second parameter here doesn't make sense, we need to find a proper way to implement this method.

Solution

File vendor/yajra/laravel-datatables-html/src/Html/Column.php, change function data (line 305 - 310) to:

    public function data($key = null, $default = null): static
    {
        $this->attributes['data'] = $key;

        return $this;
    }

@yajra
Copy link
Owner

yajra commented Nov 28, 2024

Should we petition to revert the latest PR (laravel/framework#53665), remove the data method added to the framework, or move on with the quick solution suggested by @OzanKurt?

@OzanKurt
Copy link
Contributor

I would personally like a revert.

The new pull request doesn't seem to be adding much to the codebase other than "forcefully combining" two completely different classes' (Request and Fluent) functionalities.

BUT, while we wait for an action we should implement a temporary solution for the data method.

@fdteam-deanie
Copy link
Author

The solution is avoiding the error but having a second parameter here doesn't make sense, we need to find a proper way to implement this method.

Solution

File vendor/yajra/laravel-datatables-html/src/Html/Column.php, change function data (line 305 - 310) to:

    public function data($key = null, $default = null): static
    {
        $this->attributes['data'] = $key;

        return $this;
    }

Dear friends, while waiting for the author to fix it, there is no better way than temporarily implementing the solution I mentioned at the end. Measures to close the code line or remove $default or change statics properties will cause other errors.

@vipertecpro
Copy link

How about giving us a way to override entire fluent method, i mean leave that door open so that we won't 100% rely on package manager to make that update, it is possible ?

@donny26utama
Copy link

or just down to previous laravel version until any they fix it

@nuernbergerA
Copy link
Contributor

@yajra why dont you copy laravels fluent class into your codebase and you are good?

@OzanKurt
Copy link
Contributor

@yajra why dont you copy laravels fluent class into your codebase and you are good?

That is actually a great idea, having fluent as a standalone package. Simply something like xxx/fluent which would not nuke anybodies codebase. 😄

@yajra
Copy link
Owner

yajra commented Nov 28, 2024

@yajra why dont you copy laravels fluent class into your codebase and you are good?

I agree, this is my initial plan if the revert never happens in the framework. It seems like you went ahead with it :)

@HydroMoon
Copy link

For now you can downgrade using composer update --with "laravel/framework:11.33.1".

@crynobone
Copy link

crynobone commented Nov 28, 2024

A good solution for this is to extend a decorator class instead of extending Fluent directly:

<?php

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\ForwardsCalls;
use JsonSerializable;

/**
 * @template TKey of array-key
 * @template TValue
 *
 * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
 * @implements \ArrayAccess<TKey, TValue>
 */
abstract class FluentDecorator implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
{
    use ForwardsCalls;

    /**
     * The Fluent instance.
     *
     * @var \Illuminate\Support\Fluent<TKey, TValue>
     */
    protected $fluent;

    /**
     * Create a new fluent instance.
     *
     * @param  iterable<TKey, TValue>  $attributes
     * @return void
     */
    public function __construct($attributes = [])
    {
        $this->fluent = new \Illuminate\Support\Fluent($attributes);
    }

    /**
     * Convert the fluent instance to an array.
     *
     * @return array<TKey, TValue>
     */
    public function toArray()
    {
        return $this->fluent->getAttributes();
    }

    /**
     * Convert the object into something JSON serializable.
     *
     * @return array<TKey, TValue>
     */
    public function jsonSerialize(): array
    {
        return $this->toArray();
    }

    /**
     * Convert the fluent instance to JSON.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->jsonSerialize(), $options);
    }

    /**
     * Determine if the given offset exists.
     *
     * @param  TKey  $offset
     * @return bool
     */
    public function offsetExists($offset): bool
    {
        return $this->fluent->offsetExists($offset);
    }

    /**
     * Get the value for a given offset.
     *
     * @param  TKey  $offset
     * @return TValue|null
     */
    public function offsetGet($offset): mixed
    {
        return $this->fluent->offsetGet($offset);
    }

    /**
     * Set the value at the given offset.
     *
     * @param  TKey  $offset
     * @param  TValue  $value
     * @return void
     */
    public function offsetSet($offset, $value): void
    {
        $this->fluent->offsetSet($offset, $value);
    }

    /**
     * Unset the value at the given offset.
     *
     * @param  TKey  $offset
     * @return void
     */
    public function offsetUnset($offset): void
    {
        $this->fluent->offsetUnset($offset);
    }

    /**
     * Handle dynamic calls to the fluent instance to set attributes.
     *
     * @param  TKey  $method
     * @param  array{0: ?TValue}  $parameters
     * @return $this
     */
    public function __call($method, $parameters)
    {
        return $this->forwardDecoratedCallTo($this->fluent, $method, $parameters);
    }

    /**
     * Dynamically retrieve the value of an attribute.
     *
     * @param  TKey  $key
     * @return TValue|null
     */
    public function __get($key)
    {
        if (method_exists($this->fluent, 'value')) {
            return $this->fluent->value($key);
        }

        return $this->fluent->get($key);
    }

    /**
     * Dynamically set the value of an attribute.
     *
     * @param  TKey  $key
     * @param  TValue  $value
     * @return void
     */
    public function __set($key, $value)
    {
        $this->fluent->offsetSet($key, $value);
    }

    /**
     * Dynamically check if an attribute is set.
     *
     * @param  TKey  $key
     * @return bool
     */
    public function __isset($key)
    {
        return $this->fluent->offsetExists($key);
    }

    /**
     * Dynamically unset an attribute.
     *
     * @param  TKey  $key
     * @return void
     */
    public function __unset($key)
    {
        $this->fluent->offsetUnset($key);
    }
}

@tikrack
Copy link

tikrack commented Nov 28, 2024

Hi
i have this error

Declaration of Yajra\DataTables\Html\Column::data(array|string $value): static must be compatible with Illuminate\Support\Fluent::data($key = null, $default = null)

What should I do?

@eduah33b
Copy link

For now you can downgrade using composer update --with "laravel/framework:11.33.1".

I have is working in 11.33.2 as well

@amiranbari
Copy link

Same problem here!

@github-actions github-actions bot locked and limited conversation to collaborators Dec 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.