Giter VIP home page Giter VIP logo

Comments (12)

adr1enbe4udou1n avatar adr1enbe4udou1n commented on June 13, 2024 2

Ha just re-looked quickly your code and finded why your parent was null, i didnt see it.

You're simply forget to add parent_id in your sql fields so we should have :

return $requestSearchQuery->result([
            'product_categories.id',
            'product_categories.name',
            'product_categories.parent_id',
            'product_categories.slug',
            'product_categories.status',
            'product_categories.created_at',
            'product_categories.updated_at',
        ]);

The related ids is mandatory for getting "with relation" working ;)

from laravel-boilerplate.

adr1enbe4udou1n avatar adr1enbe4udou1n commented on June 13, 2024 1

You must explicit the property of the parent you want get in the results. Example :

return $requestSearchQuery->result([
            'product_categories.id',
            'product_categories.name',
            'parent.name',
            'product_categories.slug',
            'product_categories.status',
            'product_categories.created_at',
            'product_categories.updated_at',
        ]);

from laravel-boilerplate.

adr1enbe4udou1n avatar adr1enbe4udou1n commented on June 13, 2024

Hello,

I'm pretty sure this because you use a query SQL join on search method, so "$with" property cant apply in this case.
Just add manually a query join with his parent and it should work.

from laravel-boilerplate.

webdesign7 avatar webdesign7 commented on June 13, 2024

Thanks @adr1enbe4udou1n

I thoight so but I tried to put this in search function:

$query->leftJoin('product_categories', 'product_categories.id', '=', 'parent_id');

but I got :

"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'product_categories' (SQL: select count(*) as aggregate from product_categories left join product_categories on product_categories.id = parent_id)

/**
     * Show the application dashboard.
     *
     * @param Request $request
     *
     * @throws \Exception
     *
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function search(Request $request)
    {

        /** @var Builder $query */
        $query = $this->productCategories->query();

        if (! Gate::check('view product_categories')) {
            // Filter to only current user's posts
            $query->whereUserId(auth()->id());
        }

        $query
            ->leftJoin('product_categories', 'product_categories.id', '=', 'parent_id');


        /** @var RequestSearchQuery $requestSearchQuery */
        $requestSearchQuery = new RequestSearchQuery($request, $query, [
            'name',
            'summary',
            'body',
        ]);

        if ($request->get('exportData')) {
            return $requestSearchQuery->export([
                'name',
                'status',
                'product_categories.created_at',
                'product_categories.updated_at',
            ],
                [
                    __('validation.attributes.title'),
                    __('validation.attributes.status'),
                    __('labels.created_at'),
                    __('labels.updated_at'),
                ],
                'product_categories');
        }


        return $requestSearchQuery->result([
            'product_categories.id',
            'product_categories.name',
            'product_categories.slug',
            'product_categories.status',
            'product_categories.created_at',
            'product_categories.updated_at',
        ]);


    }```

from laravel-boilerplate.

adr1enbe4udou1n avatar adr1enbe4udou1n commented on June 13, 2024

It's because you join with the same table with same alias.
Try with a different alias :
https://stackoverflow.com/questions/24570209/not-unique-table-alias-in-mysql?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

from laravel-boilerplate.

webdesign7 avatar webdesign7 commented on June 13, 2024

Getting there :
I done this

$query ->leftJoin('product_categories as parent', 'parent.id', '=', 'product_categories.parent_id');

but in the response 'parent' value is still null

Do i need to modify search function somehow as I think it somehow needs to be included in response.

??

Thanks
Serg

from laravel-boilerplate.

webdesign7 avatar webdesign7 commented on June 13, 2024

Perfect that worked.. 👍

But how come you could get 'owner' object in PostController->search() but in this case I have to be granular ?

Ideally I would like to get the whole object in the search result

Thanks
Sergiu

from laravel-boilerplate.

adr1enbe4udou1n avatar adr1enbe4udou1n commented on June 13, 2024

Ha in fact i get this owner thanks to "$with" property.

For posts I use users join only for make order by working.
So indeed strange it's not working on your side, maybe a particular case as it's the same table/object ?

from laravel-boilerplate.

webdesign7 avatar webdesign7 commented on June 13, 2024

Thanks , that worked perfect !!! :)

from laravel-boilerplate.

shirshak55 avatar shirshak55 commented on June 13, 2024

@webdesign7 He used Eager loading. You can check in laravel docs also. You can also do it bro .And I guess you are duplicating lot of things why don't you abstract some of code to methods

Thanks

from laravel-boilerplate.

webdesign7 avatar webdesign7 commented on June 13, 2024

@bloggervista for me personally wasn't too obvious the solution to that issue, being new to Laravel its normal I think for questions like that to come up.. especially on CMSes done by other developers.

from laravel-boilerplate.

shirshak55 avatar shirshak55 commented on June 13, 2024

Don't worry keep learning :)

from laravel-boilerplate.

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.