laravel scout when does shouldBeSearchable get triggered - laravel

Lets say I have a shouldBeSearchable() set like this:
public function shouldBeSearchable()
{
return $this->is_active === 1;
}
By default in my app a newly created post gets is_active set to 1 so it will be added to my search index.
But if I now update that same post and set is_active to 0 does it automatically then get removed from my search index?

Yes, it should. 👍
If it doesn't work, it will be a bug.

Run php artisan scout:flush "App\Models\YourModel".
Then run php artisan scout:import "App\Models\YourModel.

Related

Laravel call method on any artisan command

Is it possible to set a method that changes a value in my database automatically when I run a php artisan command? What I'm trying to accomplish is change the value of the first row in my "domains" table to suit the url from my .env file automatically whenever I push codes to my live/staging environment. Are there any ways to do this automatically without me manually going into my DB and changing it.
You could setup a Listener for the native event CommandFinished and check if the command is the config:cache.
Event::listen('Illuminate\Console\Events\CommandFinished', function ($event) {
if ($event->command == 'config:cache') {
// Change domains table data using Eloquent or Query Builder
}
});
To learn more about Events, see: https://laravel.com/docs/5.7/events#generating-events-and-listeners

Laravel cache null error

I'm using File Cache in Laravel 5.3. Everything works fine. After few days in production I have many errors in my laravel.log that WHERE was called on NULL value (from cache). Why is this happening? Every time I open page everything works fine without errors. Can this be caused because of bots accessing my page? I have not idea what I'm doing wrong...
CacheServiceProvider.php
// insert settings into cache
Cache::add('settings', Settings::with('langs')->get(), env('CACHE_TIMEOUT'));
Helpers.php
$value = Cache::get('settings')->where('name', $name)->first()->value;
I tried to use conditions if cache value exists then use it but it's really anoying to put it everywhere. And what if value doesn't exists? Should I call database query?
in Helpers.php I would suggest you using:
$settings = Cache::remember('settings', env('CACHE_TIMEOUT'), function() {
return Settings::with('langs')->get();
});
$value = !empty($settings) ? $settings->where('name', $name)->first()->value : null;
Remove what you have in CacheServiceProvider.php because this code will fire once when it is needed to get data and cache it for next time.
Hope that helps!

what happend to getRelatedIds in Laravel 5.4

I was using this function in 5.3 and now when I try to use it in 5.4 like this:
$post->tags()->getRelatedIds();
I am getting errors that function does not exist, I checked the documentation for 5.4 and it's not there anymore.
Anyone knows why this usefull function was removed and what I can do to get all ids from related model?
In Laravel 5.4 and 5.5 the getRelatedIds is replaced by allRelatedIds.
$post->tags()->allRelatedIds();
I can't speak for reasons why it has been removed but if you know the primary key name ahead of time (i.e. all your tables have an id column) you can simply do
$post->tags()->select('id')->pluck('id');
if you want a more generic way you'd need to jump through some hoops
$related = $post->tags();
$post->tags()
->select($related->getQualifiedKeyName())
->pluck($related->getKeyName());

Laravel Scout how refresh index with ElasticSearch?

I'm working on a Laravel project and I'm using https://laravel.com/docs/5.3/scout with ElasticSearch on a model Offer.
I already have some offers in my database, so I just run the command
`php artisan scout:import "App\Models\Offer"`
for generate the index for use my datas with ElasticSearch.
After that it's ok, I can search in my offers with, for example :
`$offers = Offer::search($request->keywords)->get();`
Now I have a function for create other offers in my database, and I dont know how can I refresh the index for use these new datas ?
In the documentation https://laravel.com/docs/5.3/scout#adding-records, I can read
all you need to do is save a model instance and it will automatically be added to your search index
I tried this and no, when I save() a new Offer, I can't find it in my index.
I tried to re run the command php artisan scout:import "App\Models\Offer" after add anew OFfer, but it's the same, I can't find it in my index.
Did I miss something ? Any ideas ?
Sorry for a late response on this but I ran into this issue myself when I attempted to use Scout. Everything went fine until I realized that the project's data would scale at a rate that went far beyond what scout could handle. In this case, however you can use the push() method instead of save(). I'm not sure why this isn't documented at all and it's rather frustrating but there's an answer at least.
So use:
->push()
instead of:
->save()
to update your indexes.
If that does not work for your specific version there is another way you can do it but it is "slightly" redundant. It involves a mix of using the queue system with the Artisan system and a command. In this you:
Create a queue/job php artisan make:job JobNameHere (As of Laravel 5.2 - 5.4)
Add use Artisan; to the top of that newly created Job Class so you can pull in the Artisan's functionality
In the handle of that Job Class add
class JobNameHere implements ShouldQueue {
...
...
public function handle() {
Artisan::call('scout:import', ['name' => "App\YourModelNameHere"]);
}
}
Add a dispatch call to that job class right after your DB push() process is called.
Example:
class YourController extends Controller {
public function yourUpdateMethod(Request $request) {
//Some code you wrote
//Some more code you wrote
$update_obj->push( $array_to_update_obj);
dispatch(new JobNameHere());
}
}
Test your index by searching on it
If all went well then you should no longer get any errors. Please leave a comment and let me know how it went... provided you're still having this issue.
I would also like to mention that Laravel Scout does not support ElasticSearch anymore as of August of 2016 (I believe). No one really knows why the support was removed but there are a few tutorials out there to help you get Laravel and Elastic search working together again.
I will also note, based on my research, that if your project is small then you should be fine to use Scout and not need to use ElasticSearch. If your project is going to get huge, then you're probably better off finding a package that supports and well documents how handle Laravel's relationships between models. Elastic search is capable of accomplishing this but there are tons of docs that make figuring it out difficult. Here are some semi-decent tutorials to help get you going down the right path.
https://tirdeamihai.com/blog/laravel-and-elasticsearch
https://laravel-news.com/laravel-and-elasticsearch
Plastic is a package that I would currently recommend as it's being actively worked on. Elasticquent hasn't been touched or updated since last year in June.
https://github.com/sleimanx2/plastic#1---create-a-new-index

Laravel about Requests

i am new in programing laravel. so here I will not ask about my problems but I am here just want to ask how to use laravel. here I would like to ask how to use:
Determining If an Input Value Is Present in laravel. and what it does?
thanks ^^
Laravel is really a good MVC Framework.
I can suggest you some source from where you can get better understanding of Laravel Framework.
https://laravel.com/docs/5.2
https://laracasts.com/skills/laravel
For simple example - How to use Laravel after installation
Go to path using terminal ex. /var/www/laravel/project
Go to - /var/www/laravel/project/resources/views
Create a directory test and create a file index.php
Create a controller - php artisan make:controller TestController
Create a function testGet() and return view - return view('test.index'); //test is directory and index is file
Create a function testPost(Request $request) and to get data use $data = $request->all(); and then print this data.
Create a model with migration file - php artisan make:model Test --migration
Go to route file - /var/www/laravel/project/app/Http/routes.php
Add line Route::get('/test', 'TestController#testGet');
Add line Route::post('/test', 'TestController#testPost');
Now check GET request to your project http://project/test it will call testGet function.
POST request http://project/test?name=Adam will call testPost function and will print your name.
as said in the comments you better check laracasts! its the laravel school
anyway to anwser your question, its simple
View
<input type="text" name="fname">
Controller
public function doingstuff (Illuminate\Http\Request $request) {
if ($request->has('fname')) {
// a man gotta do what a man gotta do here
}
}
smooth huh? :3

Resources