Join two tables and import as one Indice into Algolia? - laravel

I'm using Laravel and I'm wondering, is it possible to create a join, and then import that result as an Indice into Algolia for Searching? I know that Algolia imports models, so I'm not sure if there's a way this can be done. I'm using Laravel Scout and Algolia for search and Vue js for my front-end. This would solve the problem that I am having.
Thanks in advance.

You can try to customize the searchable data that is synchronized to Algolia by specifying toSearchableArray() method on the model. In that method, you can pass in the model and relation (whichever model you want to join with) data. More info:
https://laravel.com/docs/7.x/scout#configuring-searchable-data

Related

Custom Laravel Scout transformer for Algolia

I've gotten my model data into Algolia by using having a large transformation occur in my model.
I'd like to move this into a custom transformer now as I refactor.
Algolia has this article https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?client=php#writing-custom-transformers but it doesn't explain the transformer service class. Has anyone done this before?

Is there a way to get all the relations and fields of particular model in laravel?

I am trying to create a script that could help me generate a GraphQL schema with all fields and relations exist in the models.
if I could get that, php-lighthouse would help me for the rest of the work.
There is no way to get all relations of a particular Eloquent model without the user specifying them in some way, such as an attribute on the model that holds all relation names, or type-hinting those methods returning a relation. See laravel-eloquent-to-graphql for an example.
You can however get all fields of a model (that are saved in the database) by inspecting its table.
If you're looking for a GraphQL generator, there are Laravel Bakery and laravel-eloquent-to-graphql. Laravel Bakery generates a GraphQL schema on-the-fly, while laravel-eloquent-to-graphql generates a GraphQL schema statically.
There is also Lighthouse-Utils, which can generate queries and mutations for already-defined types.

Using Eloquent Laravel to show country's levels with big data

I have a table locations (about 35000 rows and more than in the future).
I search on the internet and try it: Eloquent: Recursive hasMany Relationship with Unlimited Subcategories
I try to run it, it kept loading and did not stop. I think so data is big.
In addition, the above solution shows the tree. I want to show the table
I want to set up a query using eloquent model or anything to pass to the view (can use DataTables package)
Thanks so much!

How to use eager loading with custom query builder in Laravel

I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:
$view = DB::table('blocks')
->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');
I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.
Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.
But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?
Block dates and modules are conditional, Sometime I dont want get data of this table with block.
Please help me on this.
You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.
But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.

How to get M:1 relation in Yii2

I am trying to get related model however i cannot seem to find correct documention. In yii 1.x i can do $jobsprocess->category0, but yii 2.x tell me to do $jobsprocess->getCategory(). This does not return a model but an ActiveQuery. How can I return a model object?
In your query use $model = YourModel::find()->with(['category])->all().
All relations using the getRelation() functions can be access with the with() function, but without the get and lowercase first letter.
You can then access the relational data with $model->category.

Resources