How to get table column names as array from model - laravel

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

Related

Laravel model find other column

Is it possible to overwrite the Laravel model 'find' function?
When I use model::find() it search in for the 'id' column but my table doens't have a id column but a SubscriptionId colum.
I know I can use: model::where('subscriptionId', $id) but my queued jobs are not working right now..
model::where('subscriptionId', $id)->first()
Make sure your subscriptionId is unique.
Let me know if my snippets code not working
Or
model::findOrFail($id, 'subscriptionId')
Find in Laravel searches the table based on the primary key. If you are not using id as your key, you can tell Laravel this within your model.
protected $primaryKey = 'subscriptionId';
This will tell Laravel to search this model based on the subscriptionId primary key instead of id. This should solve it for you. Docs on Primary Keys

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

relational fields of the same model [ODOO 10 ]

is it possible to use a relational field of the own model i've worked?
For example
_name = 'x.x'
x_ids = fields.Many2many('x.x')
I need to show the records I've created of the same model in relation field, any suggestions?
That is entirely possible, and you can use relational ways exactly the same way then you would do it with an another model.

Laravel Schema::getColumnListing not returing in a specific table

I'm using \Schema::getColumnListing to get all columns of a table. It works wonders in all tables except one.
This one table is up in the database, it's model it's working like a charm but when I call \Schema::getColumnListing it shows and empty array.
How can this be solved?
DB::getSchemaBuilder()->getColumnListing('table');
table is a reserved keyword and cannot be used.
That's because you're passing the wrong table name to the getColumnListing() method, that happened to me

Resources