laravel 5.7 : paginate with unique data - laravel

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

Related

Correct way to connect tables together in Laravel and retrieve in blade

I'm currently working on a project in Laravel.
I have 3 TABLES in MySQL.
Clicks - Costs - Profit
I want to show the data from these 3 tables in one blade.
What's the efficient way to achieve this?
For example: Having a shared same column value in each 3 tables, then returning the query with that value from every table.
Or maybe something like a belongsToMany Eloquent?
I think this is all you want - link
What is the database look like, do they share a foreign key? What data you explicitly want to extract and show to the blade?

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

sync() and attach() not working together?

In my controller first i attach array of ids like this for my additional fields:
$property->features()->attach($additional_ids);
Then im using sync() because i have checkboxes :
$property->features()->sync($features);
But problem is when i have this then my attach() is not working. Any suggestion how can i fix this? When i remove this sync() my attach is working. So idea is that i have checkboxes for features, and i have input fields for some additional features.I store this ids in pivot table .
sync() will override all changes made by attach(). So, just merge arrays and use sync():
$property->features()->sync(array_merge($features, $additional_ids));
Use this:
$property->features()->sync($features, false);
Which will sync without detaching.
Or you could also call:
$property->features()->syncWithoutDetaching($features)
Which will do the same.

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

Is this possible to get categories sorted by RAND()?

I want to display product categories in random order.
Is that possible?
How can I achieve this?
I'm using custom theme, and categories are fetch by:
$_categories = $this->getStoreCategories();
Maybe it will be easier to use php shuffle function?
After you have got the collection of the categories, do you want to try using this
$arrCategories->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit('5');
I have used this on a different collection of items, so I am not sure if this can work on Categories. But I think its worth trying..It is just writing a select query with Order by RAND().
Hope it works.
Cheers,
Swapna

Resources