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

Ability to use appends fields in Relation field #877

Closed
Nks opened this issue May 30, 2019 · 9 comments
Closed

Ability to use appends fields in Relation field #877

Nks opened this issue May 30, 2019 · 9 comments
Labels
Docs Must be added to documentation

Comments

@Nks
Copy link
Contributor

Nks commented May 30, 2019

Expected Behavior

Relation::make('categories')
                ->multiple()
                ->title(__('Categories'))
                ->fromModel(Category::class, 'nested_title'),

It should return nested_title which appends to the model Category

Actual Behavior

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nested_title' in 'field list' (SQL: select `nested_title`, `id` from `categories` where `nested_title` like %% limit 10)

Relation endpoint returning 500 error

Specifications

  • ORCHID Version: master
  • Laravel Version: 5.8
  • PHP Version: 7.3
  • Database Version: mysql 5.7
@tabuna
Copy link
Member

tabuna commented May 30, 2019

This field is computed. Accordingly, we can not implement a really optimal sql query.
But we can try to output it, for example:

Relation::make('categories')
    ->multiple()
    ->title(__('Categories'))
    ->fromModel(Category::class, 'title')
    ->displayAppend('nested_title'),

This will not be a clean solution, but will output what you want.

tabuna added a commit that referenced this issue May 30, 2019
@tabuna
Copy link
Member

tabuna commented May 30, 2019

I added a rough solution, but I think it requires some discussion and testing.

@Nks
Copy link
Contributor Author

Nks commented May 30, 2019

Maybe something like this works too?
In this case we can also show attributes which are not in appends.
Also I'm gonna add ->allowHtml() as we did for the Relationship.

Relation::make('categories')
    ->multiple()
    ->title(__('Categories'))
    ->fromModel(Category::class, function($category) {
         return $category->nested_title;
    });

@tabuna
Copy link
Member

tabuna commented May 31, 2019

In such processing, the anonymous function will have to be executed on Ajax request, and there is a logical question - how to pass it.
One option is serialization, but it imposes many restrictions.

@tabuna
Copy link
Member

tabuna commented Nov 21, 2019

Such a decision has already completely entered the system and is now not planning to change. It is necessary to document this

@tabuna tabuna added the Docs Must be added to documentation label Nov 21, 2019
@tabuna
Copy link
Member

tabuna commented Nov 25, 2019

Example:

declare(strict_types=1);

namespace App;

use Orchid\Platform\Models\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * @return string
     */
    public function getFullAttribute(): string
    {
        return $this->attributes['name'] . ' (' . $this->attributes['email'] . ')';
    }
}

Usage:

Relation::make('users.')
    ->fromModel(User::class, 'name')
    ->displayAppend('full')
    ->multiple();

Screenshot:
image

@tabuna
Copy link
Member

tabuna commented Nov 25, 2019

This is published in 6.7.0

@tabuna tabuna closed this as completed Nov 25, 2019
tabuna added a commit that referenced this issue Nov 25, 2019
tabuna added a commit that referenced this issue Nov 25, 2019
@kenanchristian
Copy link

Hello, I'm wondering if this feature also available for Select with fromModel method?

@Stehos
Copy link

Stehos commented Feb 6, 2024

Hi @tabuna please is there a way to "paginate" fromModel? E.g. I have this code:

        $newFields[] = Relation::make('products')
            ->fromModel(Product::class, 'title', 'id')
            ->searchColumns('sku')
            ->displayAppend('orchidTitle')
            ->multiple()
            ->required()
            ->title(__('Products'))
            ->help(__('Select products to be new price applied on'));

but it returns only first chunk of searched products and I can not find any other option than is in the first chunk.

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Must be added to documentation
Development

No branches or pull requests

4 participants