Laravel scout with ElasticSearch, how reset the index? - laravel

I'm working on a Laravel project and I'm using https://laravel.com/docs/5.3/scout with ElasticSearch on a model Offer.
With the command php artisan scout:import "App\Models\Offer" I can generate an index for ElasticSearch with offers from my database, and it's ok. After that I can search offers in this index.
But now, I removed somes offers from my database, and I don't know how can I reset or re-generate the index for remove old ids ?
For example I removed Offer with id = 15 in my database, but when I do a search, this offer (id=15) is always indexed, and I get an error because this offer doesn't exist.
I tried to re run the command scout:import but no effect. The only solution I fond is to rename the index with https://laravel.com/docs/master/scout#configuring-model-indexes but I can't do that each times...
Any ideas ?

You can use php artisan scout:flush "App\Models\Offer"
You can also remove physical files for indexes from storage folder

The driver should automatically remove/update the record when you do a remove/update on any Model declared as Searchable.
To flush the models use: php artisan scout:import Model
You could check the bugs area for laravel-scout, maybe there is something related or try an update to L5.4

Related

Change default directory for creating migration in Laravel

I want to change the default directory for creating migration i.e. If I do php artisan make:migration create_users_table, it should create users table in /database/migrations/child_database. I do not want to use --path every time I create a migration for child_database. I want to change the default directory for php artisan make:migration.
you can create your own artisan command to achieve this:
create your a new artisan command and use it to make migration in your custom directory using this article. hope help you ^_^
try this package github repo or use laracast link
You can use loadMigrationsFrom() in your AppServiceProvider.
Inside the boot() function, run:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder1',
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder2',
]);/
This question has been answered before, even though it's a little bit old it still has it's value.
Have a look at: https://stackoverflow.com/a/35895106
If you use this answer, the default directory is changed and you will have the solution you want.

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Laravel Cashier - publish migration results in "cannot declare class CreateCustomersColumns"

I have a fresh Laravel installation and I've added Cashier to my project.
Since the Users model on my app won't have a stripe connection, but rather an Accounts model, I need to alter their migration to add columns to Accounts instead of Users
The documentation says to run:
php artisan vendor:publish --tag="cashier-migrations"
which adds the two migration files to database/migrations
From there I can change users to accounts in the migration file.
When I try to run php artisan migrate, I get:
Whoops\Exception\ErrorException : Cannot declare class CreateCustomerColumns, because the name is already in use
This problem only goes away when I delete the migration files, but then the new columns are added to users.
The documentation states that you can disable their migration files by putting Cashier::ignoreMigrations(); in AppServiceProvider
I didn't realize that's what I wanted to do. I thought the publish command only published the two files I needed to edit, however, those are the only migration files that come with Cashier.
Be sure to add Cashier::ignoreMigrations(); in the register method.
And add use Laravel\Cashier\Cashier;
The file (and thus the class) must exist twice, probably in your database/migrations directory, most likely with 2 different datetime prefixes on the filenames.

How does Laravel keeping track of batch value?

I created some migration files which created the table "users" and "user_info".
Running php artisan migrate created the tables and added them to the migrations table in the database.
Then I needed to create a new column and add some new rows to the user_info table. So I created a new migration file with the same name as the first migration file which was used to create the "user_info" table.
Since Laravel adds a timestamp to the file I didn't think that would create any problems. However, in the the new migration file I used the same class name as for the the first migration file, did the changes I wanted and finally ran php artisan migrate again.
That seemed to create problems since I got an error saying that the class name was already in use.
So I changed the migration filename to be user_info2 and used the class name UserInfo2 and tried again.
This time it worked and the batch has been bumped up to 2 in the migration table in the database.
But how does Laravel know that the migration file actually is related to the first user_info migration file since I changed both the filename and class name? I thought they had to have the same name in order to make Laravel have control of the "batch-value".
Thanks for any help!
The batch number of the migrations is used to specify multiple migrations that were created while running php artisan migrate.
So for example you create two migrations: users and user_info you run php artisan migrate this two migrations will be created and will be referenced by the same batch number: 1.
Then you create another migration, when you migrate this migration will have the number 2 as batch number.
When you do php artisan migrate:rollback based on the batch number Laravel knows which migrations to rollback, in this case only one migration with the batch number of 2.
So batch in this terms means an array of migrations that has not been run. Those will be referenced by the same ID, in order to make the rollback possible.
Hope this explains a bit more.

how to create migration from existing database in laravel

I am new to laravel. i know command to create database from laravel migration.
I have already created database in mysql. so how can i convert migration from that database.
You could use Jeffrey Way's generator tool for Laravel 4, and do them by hand. It is a very useful tool. https://github.com/JeffreyWay/Laravel-4-Generators (if you are using Laravel 5, use this package instead: https://github.com/laracasts/Laravel-5-Generators-Extended)
Or you could try out one of the following packages, they should convert your existing schema to Laravel migrations. I have not tried them, but have a look:
https://github.com/adamkearsley/laravel-convert-migrations
https://github.com/barryvdh/laravel-migration-generator
Also, read the following answer in another question, might be useful to you or others: Reverse engineering or Auto-Generations of Entities in Laravel?
For modern versions of Laravel you should probably use a package like https://github.com/kitloong/laravel-migrations-generator
I quick wrote this python script to make migration files out of an existing database.
https://github.com/aljorhythm/sql-to-laravel-migrations
After set up, just do
python retrieve-and-convert.py
If the accepted answer (at the time of editing this post) does not work for you, use this simple approach.
Check out this package by Kitloong is really easy to use.
https://github.com/kitloong/laravel-migrations-generator
After installation, just run something like this to export the migration to a location of your choice:
php artisan migrate:generate --path="path\to\existing\folder"
Example:
php artisan migrate:generate --path="C:\xampp\htdocs\laravel_bs4\database\migrations\my_new_migrations"
You should end up with migrations created from the database and saved in the location you have specified.
You can omit the --path option and simply run
php artisan migrate:generate
This would export it to the default Laravel's migration folder (which is something I usually do not want).
Be sure to follow the prompts. Sticking to the default is usually okay.
How about this?
Migrations Generator.It's for Laravel, 4, 5, 6, 7, but I don't know if it is for Laravel 8
Simple solution, to use online Laravel migration generator for your existing SQL table schema without installing any package. Just enter your SQL schema and get Laravel migration file.
Try: https://laravelarticle.com/laravel-migration-generator-online
If you don't mind using Doctrine 2 in your project instead of Eloquent that has reverse engineering built in.
For laravel 9 follow below link
https://laravel.com/docs/9.x/migrations#generating-migrations

Resources