how to fire databse query in laravel 5.2 - laravel

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"));

Related

How to build Laravel REGEX query including paginate

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")

laravel 5.7 : paginate with unique data

I want create a query that sort by desc after it get unique event_name (i have many same name in my table but i want show one of them) and after it i want to paginate it
i tried this
DB::table('events')->latest()->get()->unique('event_name');
but i cant paginate it because its a collection
i tried this
Event::order By("created_at","DESC")->paginate(6)->all();
but i cant unique data
anyone can help me?
You can use
Event::distinct('event_name')->orderby("created_at","DESC")->paginate(6);
Try This
Event::orderBy("created_at","DESC")->distinct('event_name')->paginate(6);
If I understood you correctly, you need something like this?
Event::groupBy('event_name')->orderBy('created_at', 'desc')->paginate(6);

How can I retrieve this in laravel?

Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();

How to get table column names as array from model

I'm new to laravel
I have post table, this table has 10 columns.
So i want to get all column names in model
Laravel Does this feature already work??
Or do I have to make it myself ??
Any suggestion or advice would be appreciated.
Thank you in advance
You can use the getColumnListing() method:
use Schema;
Schema::getColumnListing($this->getTable())
$columns = Schema::getColumnListing('posts');
dd($columns);

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