Algolia save only strings using Laravel 5.4 - laravel

I use Laravel 5.4 and i integrate algolia with my project, but I have some problems with data type of columns.
If I run the command from the localhost, status columns (and others) appear like integer in algolia database, but if I run the same artisan command from the production environment, the status column is now string, and I can't use ->where('status', 1) in my code, because algolia can use only integers for where clauses.
Is there a problem with my database? But is the same database from my localhost, same mysql version...

I don't know what could be the reason of this, but I think I know a good work around.
Let me also say that Algolia can have string as a where clause, but it's an intentional limitation of Laravel Scout.
You can use the toSearchableArray in your model to customize the array sent to Algolia. I'd use it to cast the data.
https://laravel.com/docs/5.5/scout#configuring-searchable-data
You should have something like this:
public function toSearchableArray()
{
$data = $this->toArray();
$data['attribute'] = (int) $data['attribute'];
return $data;
}

Related

Laravel Search Model

Im new to laravel and have completed a project on the latest version laravel 5.6
However, the only thing that is pending to learn and implement is the search functionality which is the core of every application. The application i have coded is a huge project and i do not want to mess it up so would like to learn how search functionality works in order to implement in multiple sections of the project.
Let's say i have a the following models 1.Country 2.State 3.City 4.Professional_Categories 5.Professionals and i have added the relations in the models accordingly
I fetch the records normally using the following code
public function index()
{
$categories = ProfessionalCategory::all();
$topprofessionals = Professional::where('status', 1)->limit(12)->get()->sortByDesc('page_views');
$professionals = Professional::where('status', 1)->latest()->paginate(9);
return view('professionals.index',compact('professionals', 'categories', 'topprofessionals'));
}
I can pull up Countries, States and Cities as i pulled in categories and with foreach i can display them in the select box. Or i could use pluck('name', 'id') that works good too. But i have never tried search queries.
I want a search form on the professionals page where a user can select country, state, city, category and type keywords in locality and professional name to find the professionals. My next step would be to learn dependent dropdown for country, state and city.
And how the results can be displayed on the view file where im currently fetching records with foreach($professionals as $professional)
I do not need code, but just a basic example on how these kind of things work in laravel. Once i learn the basics i want to implement auto suggest from database and things like that. And a single form to search all the models.
It sounds like you'll need full text searching across a number of tables and associated columns. I'd recommend using Laravel Scout or a similar service (such as elastisearch) for this.
These will index your records in a way to allow for fast and efficient fuzzy searching. Using Scout as an example, you can directly query on your models like:
$orders = App\Order::search('Star Trek')->get();
Searching with Scout
This will save you from writing numerous queries using LIKE which becomes very slow and inefficient quite quickly.
Using Eloquent you might do something like this in your Controller:
public function search(Request $request)
{
$countries = Country::where('name', 'like', '%' . $request->search_value . '%')->get();
}
This will return the countries whose name contains the search_value provided by the user.

Where to process data in Laravel

Back when I was using CodeIgniter I had functions in my models like
public function GetArticlesFormatted($inactive = FALSE)
and then in my controller I could have had
$articles->GetArticlesFormatted(true);
And so on.
How should I achieve the same with Laravel 5.4? The database of the app I'm building is already built and full and is a mess so most of the "automated" super-restrictive things that Laravel has don't work out of the box.
For example there is a Country Code that I'm retrieving and I need it as is, but in some instances I need it converted in a Country Name which I had in another table.
Right now I just have in my controller wherever I am retrieving data from the model:
$countryResult = Country::where('country_code', $item['country_code'])->first();
$countryArray = $countryResult->toArray();
$item['country'] = $countryArray['country_name'];
Can I somehow achieve that in a more elegant way?
I tried accessors but for some reason couldn't get anything to work for my purposes.
A select query can be used to limit selection to a particular column.
$countryName = Country::select('country_name')
->where('country_code', $item['country_code'])
->first();

Laravel 5.3 Pulling comments from the fields of a DB table

Simple question here:
In laravel 5.3 how would I pull the comments from a database table? Is there a clean way of doing it using some of the out-of-the-box features that laravel provides??
Thank you in advance.
Laravel 5.2-5.3, as far as I know, comes with a built-in package called doctrine that allows you to interact with a lot more within a database and it's tables than eloquent. I believe the framework members will eventually add more to the system so you can make more dynamic use of a DB and tables etc.
For the time being this is how I implement accessing the structure (comments included) of a database table:
$settings = SomeModel::where($items_match)->get(); //Making use of Eloquent
$columns = DB::connection('database_name_here')
->getDoctrineSchemaManager()
->listTableDetails('table_name_here');
foreach ($settings as $key => $value) {
if ($comments[$key] = $columns->getColumn($key)->getComment()) {
}
}
It's fairly clean and get's the job done. The only downside I see is it's a double hit to the DB which I'm thoroughly against, I'm working on a way to combine the 2 implementations in laravel so that it's only 1 query doing both jobs.

laravel 5.2 work with belongs to with two different queries

I have write down this query
Controller
$data = User::where('name',$name)->with('country');
In User model
function country () {
return $this->belongsTo('App\Country');
}
In view
echo $data->country->name;
It is working fine but it run 2 queries :(
Select * from user where name = "xyz"
Select * from country where id = "745"
I want to stop this, I want to fetch data with one query only. Join is the solution, Is any other solution for this?
Unfortunately this is the way Eloquent works. It uses two queries because it's a simpler task to initialise your models and to avoid column naming conflicts.
If you are concerned about performance but still want some sort of querying tool, use the Query Builder shipped with Laravel.
To answer your question, joins will be your best bet.
$data=user::with('country')->where('id',745)->where('name','xyz')->get();
i hope that will help you

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