Elastiquent Elasticsearch in Laravel 5.2 - php

After days of searching, I have decided to come here and ask.
I have used the package before, and literally copy-pasted the code but it still wont work in this project.
I am trying to use the Elasticquent package for Elasticsearch in laravel 5.2.
The error I get is: Invalid argument supplied for foreach()
This is being thrown in the /vendor/elasticsearch/elasticsearch/src/Elasticsearch/ClientBuilder.php file on line 111.
This happens whenever I try to use the package (ie: using search, addAllToIndex, etc.).
From what I can tell, the config is null, so maybe there is a setting in the elasticquent that I missed?
Any ideas on how to fix this?
EDIT
My config/elasticquent.php file (exact same as `vendor/elasticquent/elasticquent/src/config/elasticquent.php):
<?php
return array(
/*
|--------------------------------------------------------------------------
| Custom Elasticsearch Client Configuration
|--------------------------------------------------------------------------
|
| This array will be passed to the Elasticsearch client.
| See configuration options here:
|
| http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_configuration.html
*/
'config' => [
'hosts' => ['localhost:9200'],
'retries' => 1,
],
/*
|--------------------------------------------------------------------------
| Default Index Name
|--------------------------------------------------------------------------
|
| This is the index name that Elastiquent will use for all
| Elastiquent models.
*/
'default_index' => 'my_custom_index_name',
);
EDIT 2
The result to curl -XGET localhost:9200:
{
"name" : "Mac Gargan",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.2",
"build_hash" : "b9e4a6acad4008027e4038f6abed7f7dba346f94",
"build_timestamp" : "2016-04-21T16:03:47Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
EDIT 3
For anyone with this issue, I didn't manage to solve it. I had to use another package mustafaaloko/elasticquent5 found here

Here is my answer in Invalid argument supplied for foreach()
I look into source code and find the problem is that the config doesn't commit as expected, so you can clear cache and publish vendor, this work for me
php artisan config:clear
php artisan cache:clear
php artisan vendor:publish

Related

Laravel 5 : test artisan migrate with path

I need a little help please :
With Laravel migrations, I like to put the migration in subfolder.
in a testing class, this works :
$this->artisan('migrate:fresh');
But, this does not work :
$this->artisan('migrate:fresh --path=/database/migrations/v1');
Do you have the solution please to specify the path with the tests?
Thank you.
The artisan method takes two parameters ($command, $parameters = []).
To get this to work you'll need to put the options in the parameters array:
$this->artisan('migrate:fresh', [
'--path' => 'database/migrations/v1'
]);
Just an FYI, the same is also true for command arguments e.g.
php artisan make:model Product -m
would be
$this->artisan('make:model', [
'name' => 'Product',
'-m' => true,
]);

Using vinkla/hashids package in Laravel 5.2

I am using vinkla/hashids and i have followed the following steps
composer require vinkla/hashids
Add the service provider to config/app.php in the providers array
If you want you can use the facade. Add the reference in config/app.php to your aliases array.
php artisan vendor:publish this step does not create hashid.php in config file
use Vinkla\Hashids\Facades\Hashids;
Hashids::encode(4815162342);
And i get error that hashids class not found
It seems that the provider is not booting.
Try to do this:
php artisan config:clear
php artisan clear-compiled
The first will clear any cached config files, and the later will clear the services cache.
It worked for me, hope it works for you too.
I found the solution here: Laravel 5.2 Service provider not booting
Try checking inside your $laravelSite/config Directory to see if you find a file called hashids.php...
In your Controller; try also to import the the Hashids Class manually like so:
<?php
namespace App\Http\Controllers;
use Vinkla\Hashids\Facades\Hashids;
class SampleClass extends {
public function testHashID(){
$h1 = Hashids::encode(4815162342);
var_dump($h1);
$h2 = Hashids::decode('oaobgb-rnar');
var_dump($h2);
}
}
And by the way; if you don't see hashids.php within your $laravelSite/config Directory; you may try to manually create it. The file just returns an array of Configuration Settings... the content to the file looks like so:
/*
* This file is part of Laravel Hashids.
*
* (c) Vincent Klaiber <hello#vinkla.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
/*
|--------------------------------------------------------------------------
| Default Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the connections below you wish to use as
| your default connection for all work. Of course, you may use many
| connections at once using the manager class.
|
*/
'default' => 'main',
/*
|--------------------------------------------------------------------------
| Hashids Connections
|--------------------------------------------------------------------------
|
| Here are each of the connections setup for your application. Example
| configuration has been included, but you may add as many connections as
| you would like.
|
*/
'connections' => [
'main' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
'alphabet' => 'your-alphabet-string',
],
'alternative' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
'alphabet' => 'your-alphabet-string',
],
],
];
To encode just do this
\Hashids::encode($characters)
And to decode
\Hashids::decode($characters)

How to fix in laravel 5.2 zizaco entrust:migration class name validation?

I have followed zizac/entrust installation tutorial from GitHub Link and faced with error:
Class name must be a valid object or a string in
var/www/html/laravel_test/vendor/zizaco/entrust/src/commands/MigrationCommand.php
on line 86
MigrationCommand.php file url : Link
Outut:
php artisan entrust:migration
Tables: roles, role_user, permissions, permission_role
A migration that creates 'roles', 'role_user', 'permissions', 'permission_role' tables will be created in database/migrations directory
Proceed with the migration creation? [Yes|no] (yes/no) [yes]: yes
Creating migration...
PHP Fatal error: Class name must be a valid object or a string in /var/www/html/laravel_test/vendor/zizaco/entrust/src/commands/MigrationCommand.php on line 86
the command: php artisan vendor:publish was successful.
File : config/entrust.php exist.
I didin't change any options to config/auth.php file same as - auth.php. How to fix it?
in vendor/zizaco/entrust/src/commands/MigrationCommand.php on line 86
remove line :
$usersTable = Config::get('auth.table');
$userModel = Config::get('auth.model');
add line :
$usersTable = Config::get('auth.providers.users.table');
$userModel = Config::get('auth.providers.users.model');
and config/auth.php file write provider line as like me :
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'users',
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
then your problem will solve : happy coding
In vendor/zizaco/entrust/src/commands/MigrationCommand.php on line 86.
Laravel 5.1.* Add Line
$usersTable = Config::get('auth.table');
$userModel = Config::get('auth.model');
Laravel 5.2.* Add Line
$usersTable = Config::get('auth.providers.users.table');
$userModel = Config::get('auth.providers.users.model');
The accepted answer may fix the problem but it is very bad practice to edit direct vendor files. The following will fix the issue you may be having and will support your app still working if you decide to update Entrust and they fix their codebase.
Add the following lines to config/auth.php underneath:
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
Laravel 5.1 - 5.4
'model' => \App\Models\User::class,
'table' => 'users',
Once Entrust rolls out an update you can remove this or keep it. Up to you.
Try running:
php artisan config:cache
to make sure your application is using fresh config files
EDIT
Ok, now I see, this library want to use:
$usersTable = Config::get('auth.table');
$userModel = Config::get('auth.model');
but there is no something like this in auth any more.
So as temporary workaround you should probaby add table and model to auth file like so: https://github.com/laravel/laravel/blob/5.1/config/auth.php
and wait until Entrust will be upgraded to remove this

Missing argument 1 for Illuminate\Auth\AuthManager::createDriver() lumen and JWT

I am trying to implement JWT token in my API using Lumen + JWT. I am using this JWT Library, I have set up it, but when I want to validate passed using JWTAuth::attempt($credentials) I get next error
ErrorException in AuthManager.php line 16:
Missing argument 1 for Illuminate\Auth\AuthManager::createDriver(), called in /home/admin/web/mkopilka.ru/public_html/api/referral/vendor/illuminate/support/Manager.php on line 87 and defined
I know where is the problem, but cannot figure out how to solve it because I don't know internals of framework well.
I have question about how does JWT authenticate the user (checks credentials in database, as I can gues it uses model class provided in jwt.php with the following line 'user' => 'App\Models\User'
By default 'user' => 'App\User'
So even if I changed user model in this file I got the next error
vendor/illuminate/auth/EloquentUserProvider.php line 126:
Class '\App\User' not found
I thought and decided to add config/auth.php file with succeeding content
return [
'model' => 'App\Models\User'
];
And now I get the the first exception.
What is wrong I can quess that I have overridden all parameters in auth config file.
Aslo I wonder where can I find (except source code, it will take a lot of time to understand it) explanation how JWTAuth::attempt works ?
Thanks.
Just had same issue myself and stumbled upon this question.
Solution is to add 'driver' => 'eloquent' into your created auth.php file.
I had the same problem on my upgrade from Laravel 4.1 to 4.2 (I think mainly because I updated all the files and tried to make a composer update afterwards).
For me the following worked (like reverting the relevant update steps):
1. Modify auth.php
Add driver, model and table to config/auth.php main array (additionally to the already existing one in the providers sub array):
<?php
return [
'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'users',
// ...
2. Add ArtisanServiceProvider
To prevent the error Artisan: Command clear-compiled is not defined readd Illuminate\Foundation\Providers\ArtisanServiceProvider to the service providers
<?php
return [
// ...
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
// ...
3. Update and revert changes
Perform update (composer update) and revert the two previous steps by removing the added lines.

how to create modules in laravel 5 or hmvc in laravel 5

I am developing hmvc in laravel 5.But didnt find usefully step and code ,there will be short description each internet solution ,but i am beginner in laravel 5
I am using below library by following this link:
https://github.com/teepluss/laravel-hmvc
I am writing this code in route :
hmvc::get('/', function () {
return view('welcome');
});
but getting error:
HmvcNotFoundHttpException in Hmvc.php line 133: Request Not Found.
Call to undefined method Teepluss\Hmvc\Hmvc::get().
Check your provider and aliases.
'providers' => array(
'Teepluss\Hmvc\HmvcServiceProvider'
)
'aliases' => [
'HMVC' => 'Teepluss\Hmvc\Facades\HMVC',
]
and run
php artisan vendor:publish
at route:
HMVC::get('hello-world');

Categories