Comments (12)
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.
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.
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.
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.
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.
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.
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.
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.
Thanks , that worked perfect !!! :)
from laravel-boilerplate.
@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.
@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.
Don't worry keep learning :)
from laravel-boilerplate.
Related Issues (20)
- Congrats To this Boilerplate and adr1enbe4udou1n [Not Issue]
- Removing Image Cache? HOT 1
- "npm run build" command causes a lot of prettier errors HOT 4
- Can not run php artisan db:seed HOT 1
- php artisan db:seed - error HOT 2
- Error page about HOT 1
- RoleForm.vue does not display feedback HOT 1
- RU translation misspell HOT 1
- User slugs are not unique HOT 2
- admin password reset HOT 1
- IE11 Backend Demo not working HOT 1
- phpunit error HOT 1
- Upgrade to 5.8? HOT 6
- Multiple authentication? HOT 7
- Upgrade to 6.0?
- server exception HOT 10
- Ways to images ? HOT 22
- [Vue warn]: Error in nextTick: "TypeError: Cannot convert undefined or null to object" HOT 1
- seocontroller and sitemap HOT 1
- Can you make the code without using the JSON data type? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-boilerplate.