May Sale! All accounts are 40% off this week.

AnatoliyViktorovich's avatar

Laravel and Mailchimp API question

Greetings!

I'm new to Laravel. Trying to build an application that gets data using Mailchimp API. It works, but when I'm trying to get a list of lists (sounds funny) I get only 10 out of 15 existed. Anyone already solved that problem? Would be thankful for an advice. Thanks in advance.

The code I use:

Route::get('/', function(){ $client = new MailchimpMarketing\ApiClient(); $client->setConfig([ 'apiKey' => config('services.mailchimp.key'), 'server' => 'usXX', ]);

$response = $client->lists->getAllLists();
$lists = $response->lists;

    return view('index', [
        'lists' => $lists,
    ]);
});
0 likes
15 replies
AnatoliyViktorovich's avatar

@Sinnbeck Thanks! I knew some kind of pagination was involved. ) You mean I can modify the code of that ListsApi.php changing $count = '10' to $count = '20'? Is it legal? I will not get in jail for that? )

AnatoliyViktorovich's avatar

@Sinnbeck Thanks! It works!!! I spent days with chimp support trying to figure this out... And you needed just few minutes. )

One more question if you don't mind. How do I sort received lists by number of members in it Desc? Can't figure it out.

Sinnbeck's avatar

@AnatoliyViktorovich I'm just used to reading the source code when I need to implement new stuff :) That and API docs.

If the a number of members is a field, you can order them by the parameters $sort_field = null, $sort_dir = null. Check the source again. If you are using php 8, you can use named parameters to make your life a bit easier :)

AnatoliyViktorovich's avatar

@Sinnbeck I'll try. ) I'm new to Laravel, I'm ne to OOP php, I'm new to Mailchimp api... So I'm not sure where should I start reading. )))

Sinnbeck's avatar

@AnatoliyViktorovich Well start by looking at what you are getting back. If there is a key called something like "member_count" or similar, you can probably sort by it :)

If you can show the data of just a single "row", I can probably help

AnatoliyViktorovich's avatar

$response = $client->lists->getAllLists(); $lists = $response->lists;

As I understend I'm getting an array of list objects in $lists. when I do dd($lists); I can see +"member_count": 67 under +"stats": {#671 ▼.

Sinnbeck's avatar

@AnatoliyViktorovich If you only have 15 then maybe the easiest is just to do

$lists = collect($response->lists)->sortBy('stats.member_count')->values();
AnatoliyViktorovich's avatar

@Sinnbeck Yes! It works! You're a magician!!! ) I just changed to sortByDesc. Thank you!!!

So that collect is about collections, provided either by Laravel or by php8? I hardly remember reading about it somewhere. Can you advice where to read about it? It looks like I wiill need to use it alot. )

AnatoliyViktorovich's avatar

I'm stuck again. By analogy with fetching a list of lists via mailchimp api and sorting them with collections help, I wanted to do the same with campaigns list.

Api docs tells me $response = $client->campaigns->list(); It works, But again it has $count = '10' limit in CampaignsApi.php. If with "lists" I changed it to 20 and it solved the problem (I have just 15 lists), I don't know what to do with "campaigns" as I have hundreds of them. And by default they show the oldest first and I need the latest. In theory I could raise $count to 1000. And then sort by date desc with your collection. But then I have to limit somehow to ten as I don't need to display more. Please advise.

Sinnbeck's avatar

@AnatoliyViktorovich Please make a new thread so others can give a hand also :) This thread is marked as solved, so most people will ignore it.

But most likely you just want to get it paginated.

1 like

Please or to participate in this conversation.