How to build Laravel REGEX query including paginate - laravel

Following query works fine in phpmyadmin, but I didn`t get figured out, how to put this into Laravel Eloquent with a request:
SELECT * FROM `customers` WHERE company REGEXP '\\MyCompany Corp\\b'
How do I have to create Laravel query if MyCompany Corp is search request like $search->search ??
Thank you for helping out.

use
->where('company', 'regexp', "$search->search")

Related

How to paginate the query results when using Laravel eloquent relationships

I have the following query which retrieves the child configuration records (confch) associated with the parent configuration (confp) and it works fine.
$data["items"] = Confp::find(decrypt($type))->Confch;
I just need to paginate the results.
adding ->paginate(10) would definitly not work as it does not exist in the resulting collection. I found nothing helping in the laravel documentation.
How can I do that please?
Try this instead of above code.
$confp = Confp::find(decrypt($type));
$data["items"] = $confp->setRelation('Confch',$confp->Confch()->paginate(10));

Laravel Scout toSql does not exist

I am trying to get SQL query as a string from query builder. toSql() method works fine without laravel scout. I am using this package for Fulltext search. Here is my code.
Post::toSql(); // Working without scout => select * form post
But I am getting an error when try to search
Post::search('test')->toSql();
Method Laravel\Scout\Builder::toSql does not exist
How Laravel Scout can be extended to add a method toSql to it.
Try this
DB::enableQueryLog();
Post::search('test');
dd(DB::getQueryLog());

How to check eloquent queries in laravel 5?

Im trying to check the eloquent queries to check for n+1 problems.
Im using this "Event::listen('illuminate.query', function($sql){
var_dump($sql);
});" on top of web.php routes file but no query appears. It seems that is because of the version of laravel, that is laravel 5.
Do you know in this version how to properly check the eloquent queries?
Add this to AppServiceProvider::boot() (documentation):
DB::listen(function ($query) {
var_dump($query);
});
try this library dude https://github.com/barryvdh/laravel-debugbar , you will know all of your query

how to fire databse query in laravel 5.2

how to i fire this query in laravel 5.2 please give me answer
"SELECT count(keyword) abc,keyword FROM exclude_keywords GROUP BY keyword order by abc"
thanks in advance
i was find my won question answer in laravel documentation
DB::table('keywords')->select(DB::raw('count(keyword) as abc, keyword')) ->groupBy('keyword')->orderBy('abc', 'desc')->get();
Here is the best way
DB::select(DB::raw(SELECT * FROM table_name));
//In your case
DB::select(DB::raw("SELECT count(keyword) abc,keyword FROM exclude_keywords GROUP BY keyword order by abc"));

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

Resources