Laravel Scout (Meilisearch) - Says imported data, but doesn't - laravel

I've installed and configured meilisearch + Laravel Scout package.
My Model:
class Post extends Model
{
use Searchable;
}
When I run php artisan scout:import 'App\Models\Post' it returns:
Imported [App\Models\Post] models up to ID: 5
All [App\Models\Post] records have been imported.
But when I check the index, it's empty. Why?
The index is being created, but the data doesn't get imported.
The same configuration of meilisearch and Scout package, works for some other models.

I've just run into this issue myself and came across your question. I don't suppose you're specifying what should be stored in the index are you?
I.e. in your model, have you created a toSearchableArray method like the below...
public function toSearchableArray(): array
{
return [
'name' => $this->name,
];
}
If you have, it turns out that your toSearchableArray method must also return the primary key within the array, otherwise the record does not get indexed.
public function toSearchableArray(): array
{
return [
'id' => $this->getKey(), // this *must* be defined
'name' => $this->name,
];
}

You can try to set:
SCOUT_QUEUE=false
To check that there is no issue with your queues and run the import again.

For index you can try:
php artisan scout:index posts
There is no other issue with your queues and run the import again.
If you have SCOUT_QUEUE=true then please, start your queue using php artisan queue:work --daemon and your data will be start importing.

Related

Laravel Excel - MaatWebsite/Excel's Import Operation raising DB Connection not Configured error

I am using Laravel 6.2 and Maatwebsite/Excel 3.1 here I am trying to import a excel file and willing to get data present in it in an Array format and in the Controller I need to validate some data among them and along with some other fields i need to insert that excel fetched array data to the database. Laravel application has no direct database connection. I am calling an API to insert those fields to the database, but the import operation giving Database connection [] not configured. error. Help me to sort out actual issue from it.
Controller Code
public function importExcelData(Request $request) {
// dd($request->excelFile);
$data = Excel::import(new UsersImport, request()->file('your_file'));
dd($data);
}
UsersImport File
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToArray;
class UsersImport implements ToArray
{
public function Array(Array $tables)
{
return $tables;
}
}
?>
After following the error stack appeared in the browser console's network response I got that I can solve this error very easily.
as per Laravel Excel Documentation
enter link description here
we need to run this command in a command prompt
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
after executing this command 'excel.php' file got created under config folder of the application
in this file , we can found this code
'transactions' => [
'handler' => 'db',
],
we need to change it as
'transactions' => [
'handler' => 'null',
],
Now after dumping the import result using dd(), I am seeing those data present in that excel file.

stancl/tenancy Custom Database Name

I'm using laravel version 6 and also using stancl/tenancy package for dynamic database, but now I have some problem when I type this command "php artisan tenants:create -d my-domain1.com name=my-domain-1 plan=free email=foo#test.local othes=test" to create a new database I got database name like this "ecommerce_db_af0a82cc-aa3f-48dc-8ded-3b70c4ba833b" so I want to custom database name by my own I try to read the documentation of that package but can't understand you can see my database name that I have generated
Maybe that helps:
Change the config/tenancy.php with it:
'prefix' => 'tenant_',
Change your command file with it:
protected $signature = 'tenants:create {tenant?}';
and the handle:
if ($this->argument('tenant')) {
$this->info("Creating Tenant #{$this->argument('tenant')}#")
$tenant = Tenant::create([
'id' => "{$this->argument('tenant')}",
]);
}

How to add relations to notifications? And get the related data in json

I am trying to create relations, and when notifications are fetched via $user->unreadNotifications I want to control which fields are shown, and fetch the relations. I cannot figure out where to do this.
I did the following:
php artisan notifications:table
php artisan make:migration add_relations_to_notifications_table
In this new migration I added requester_id.
$table->integer('requester_id')->unsigned()->nullable();
$table->foreign('requester_id')->references('id')->on('users')->onDelete('cascade');
php migrate
php artisan make:notification AnInviteWasRequested
Then in AnInviteWasRequested I removed the toArray and replaced it with toDatabase:
public function toDatabase($notifiable)
{
return [
'requester_id' => Auth::guard('api')->user()->id
];
}
However this does not set the requester_id field, it just put json into the data column that looks like this: {"requester_id":1}.
Is there anyway to get this to update the requester_id field instead of updating data?
And also is it possible somewhere, like a Model file (not in vendor dir) to control which fields are displayed when $user->unreadNotifications is done?
Actually to define which field to show/save, and then you need it to display, you only need to modify the toDatabase method. Example:
public function toDatabase($notifiable)
{
$user = Auth::guard('api')->user();
return [
'requester_id' => $user->id,
'requester_name' => $user->name,
// and more data that you need to show
];
}
So for relational data or any other data, just define it inside this method. Hope it helps. :)

JWTGenerateCommand::handle() does not exist

I am using Laravel 5.4 and JWT Auth Library for user authentication in API development. After installation while i am running php artisan jwt:generate then it throws me error of
Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
Any idea what i am missing ?
This error generally display when you install jwt package in laravel 5.5 version. then after you set service providers and run following command.
php artisan jwt:generate
then you seen this error message in terminal.
how to resolve it? simple follow this step
Step - 1 Re-install package
composer require tymon/jwt-auth:dev-develop --prefer-source
or the following is a new release package use laravel 6.X
composer require tymon/jwt-auth:1.0.*
in this developement version this errors fixed.
Step - 2 Set Service Provider
'providers' => [
....
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class to
Tymon\JWTAuth\Providers\LaravelServiceProvider::class
],
Step - 3 Generate key
php artisan jwt:secret
i found this solution from here https://laravelcode.com/post/method-tymonjwtauthcommandsjwtgeneratecommandhandle-does-not-exist
Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this method
public function handle() { $this->fire(); }
It's never a great idea to change anything in the vendor folder but the there's two ways to deal with this ...
Generate a random string yourself and just change the value in the JWT config file.
Go to Tymon\JWTAuth\Commands\JWTGenerateCommand and change the fire method to handle.
go to given file path
vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
change function name
public function fire() to public function handle()
run command:
php artisan jwt:generate
I'm publishing this answer because I have crash in this error more than one time.
The only solution I found that it works with Laravel 5.6 is the following:
Add "tymon/jwt-auth": "1.0.0-rc.1" to composer.json and run composer update
Open config/app.php and add the following:
config/app.php:
'providers' => [
/*
* JWT Service Provider...
*/
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
],
'aliases' => [
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
],
Execute:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
Finally, execute: php artisan jwt:secret
After all that, when I hit my endpoint for login I got the following exception:
Class Tymon\JWTAuth\Providers\JWT\NamshiAdapter does not exist
This was fixed by:
Open config/jwt.php and change the following:
config/jwt.php:
'jwt' => Tymon\JWTAuth\Providers\JWT\Namshi::class,
'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class,
'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
Finally, note that in order to work your User model should be defined as follows:
class User extends Authenticatable implements JWTSubject
{
...
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
...
}
I can advise one solution. Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this part of code public function handle() { $this->fire(); }
I know this is not an elegant solution, but it works. I hope this might help until official fix arrive.
see here for more info
Change fire() function to handle() in this path
vendor/tymon/jwt-auth/src/commands/JWTGenerateCommand.php
In the file path: /vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
Add public function
public function handle()
{
$this->fire();
}

Datatable server side php class for laravel

I want my datatables to process the data on the server side, I am referencing this example.
Server Side Example
However, the server side php class "ssp.class.php" given in this example uses core php with raw sql, I can not use it directly for laravel projects. Does anyone has reference to laravel way doing datatables. I don't want to use any packages at the moment though.
UPDATED 31/01/2023
You can use syamsoul/laravel-datatable-two-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-two-ssp
In your controller, need to add use SoulDoit\DataTableTwo\SSP;, and you can follow the code below:
private function dtColumns()
{
return [
['label'=>'ID', 'db'=>'id', 'formatter'=>function($value){
return str_pad($value, 8, '0', STR_PAD_LEFT);
}],
['label'=>'Email', 'db'=>'email' ],
['label'=>'Username', 'db'=>'username' ],
['label'=>'Created At', 'db'=>'created_at' ],
];
}
private function dtQuery($selected_columns)
{
return \App\Models\User::select($selected_columns);
}
This is working perfectly on Laravel 8 and above.
.
.
Below is my old answer.. (21 March 2019)
You can use syamsoul/laravel-datatable-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-ssp
In your controller, need to add use SoulDoit\DataTable\SSP;, and you can follow the code below:
public function get(){
$dt = [
['db'=>'id', 'dt'=>0, 'formatter'=>function($value, $model){ return str_pad($value, 8, '0', STR_PAD_LEFT); }],
['db'=>'email', 'dt'=>1],
['db'=>'first_name', 'dt'=>2, 'formatter'=>function($value, $model){ return $value . ' ' . $model->last_name; }],
['db'=>'created_at', 'dt'=>3],
['db'=>'email_verified_at'],
['db'=>'last_name'], // must include this because need to re-use in 'first_name' formatter
];
$dt_obj = new SSP('\App\User', $dt);
$dt_arr = $dt_obj->getDtArr();
return response()->json($dt_arr);
}
This is working perfectly on Laravel 5.8 ... I'm not sure about the other versions.

Resources