Code Monkey home page Code Monkey logo

Comments (6)

andersao avatar andersao commented on July 28, 2024 1

Hi @dixitchopra,

When you use the Filter, the Model will only return the columns requested in the case, id and title. In PostTransformer if you are prepared to make the Model with all its attributes, and when the filter is activated, the other columns are not returned and so it comes as null.

I think that a solution would be to perform a check on your PostTransformer, and only display columns that are defined, or another solution would be to use the method toArray () of the Model.

Ex:

use App\Post;
use League\Fractal\TransformerAbstract;

class PostTransformer extends TransformerAbstract
{
    public function transform(Post $post)
    {
        $data = [];

        if( isset($post->id) )
            $data['id'] = (int) $post->id;

        if( isset($post->title) )
            $data['title'] = $post->title;

        if( isset($post->text) )
            $data['text'] = $post->text;

        return $data;
    }
}

Ex 2:

use App\Post;
use League\Fractal\TransformerAbstract;

class PostTransformer extends TransformerAbstract
{
    public function transform(Post $post)
    {
        $avaliable = ['id','title','text'];
        $data      = array_only($post->toArray(), $avaliable);

        return $data;
    }
}

I hope I help!

from l5-repository.

golinmarq avatar golinmarq commented on July 28, 2024 1

@andersao did you find a solution to him? I'm interested to apply this

from l5-repository.

dixitchopra avatar dixitchopra commented on July 28, 2024

Thanks for your reply. I am using second option in my code. There are two issues here.

  1. How can I change the column name title to Name?
  2. This won't work with 'With'. return $this->postRepository->with(['user'])->all(); I
    am getting only Post model columns and not User columns. Earlier it was working.

from l5-repository.

andersao avatar andersao commented on July 28, 2024

@dixitchopra I'll get a solution for you :)

from l5-repository.

andersao avatar andersao commented on July 28, 2024

@dixitchopra Are you still having problems?

from l5-repository.

hoseinz3 avatar hoseinz3 commented on July 28, 2024

@andersao i'm still having problems.

from l5-repository.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.