Laravel export to excel - download issue - excel

I can't generate xlsx file. I'am using maatwebsite/excel 3.1. I have installed it via this:
composer require maatwebsite/excel
I've added 'Excel' => Maatwebsite\Excel\Facades\Excel::class, to Aliases and Maatwebsite\Excel\ExcelServiceProvider::class, to providers.
I have structure of project like ProjectName->app->Exports.
<?php
namespace App\Exports;
use App\Order;
use App\Status;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class ActiveOrderExport implements FromCollection
{
public function collection()
{
return Status::all();
}
}
In repository I have function excel, also with these inclusions:
use App\Exports\ActiveOrderExport;
use Maatwebsite\Excel\Facades\Excel;
public function excel()
{
return Excel::download(new ActiveOrderExport(), 'activeOrders.xlsx');
}
Route:Route::get('/export_excel', 'HomeController#excel')->name('admin.export_excel.excel');
Home Controller:
{
$this->AOrepository->excel();
//return back();
}
My problem is that no error is shown, only blank page. No download is executed in the browser.
I've watched several tutorials, I haven't noticed any important diffrence among the codes.
Can you help me please?

Related

Association is in database, but can't be retrieved via DAL. Will retrieve empty array of associations

I am following the advanced developer tutorial (https://docs.shopware.com/en/shopware-platform-dev-en/how-to/indepth-guide-bundle).
Currently I'm at step 7, and according to the tutorial what I've made so far should work.
But it doesn't.
In the database it shows the association, but I can't retrieve them from the repository.
You have to add the association to the Criteria.
$criteria->addAssociation("name_of_association")
Without it, the associations come as null.
Okay, turns out I switched up two parameters by accident. When I set them correctly it worked as it should.
<?php declare(strict_types=1);
namespace Swag\BundleExample\Core\Content\Product;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Inherited;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Swag\BundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use Swag\BundleExample\Core\Content\Bundle\BundleDefinition;
class ProductExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new ManyToManyAssociationField(
'bundles',
BundleDefinition::class,
BundleProductDefinition::class,
'product_id',
'bundle_id'
))->addFlags(new Inherited())
);
}
public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}
I'm talking about the 'product_id' and 'bundle_id'. In my case I had the 'product_id' as the last parameter.

Export large amount of data with Laravel Excel Export (version 3.1)

I'm trying to export a large amount of data with Laravel Excel Export, version 3.1, but I always have a lack of memory (my limit is 512M).
In the export class:
class ExcelExport implements FromQuery
{
use Exportable;
public function query()
{
return DB::table('myTable')->orderBy('date');
}
}
In the controller:
Excel::store(new ExcelExport(), 'myFile.xlsx');
From the official documentation I see the following:
"By using the FromQuery concern, we can prepare a query for an export. Behind the scenes this query is executed in chunks."
But it seems not to work as expected.
Is the use of Query Builder the problem?
Also, is there a way to set a chunk size?
I have tryied to use a limit clause in the query, like this:
public function query()
{
return DB::table('myTable')->orderBy('date')->limit(1000);
}
but it doesn't work: it seems that the limit is not used.
I have tryied to catch the error in a try...catch block:
try{
Excel::store(new ExcelExport(), 'myFile.xlsx');
}
catch(\Exception $e){
Log::error($e->getMessage());
}
but, still, it doesn't catch any exception: all that I see is a 500 internal server error.
Can someone help me with that?
Thank you very much in advance.
You could try implicit export queueing.
use Illuminate\Contracts\Queue\ShouldQueue;
class ExcelExport implements FromQuery, ShouldQueue
{
use Exportable;
public function query()
{
return DB::table('myTable')->orderBy('date');
}
}
Call your export like this:
(new ExcelExport)->store('myFile.xlsx');
In this way multiple jobs will be chained in order to chunk the process.
Read more in the docs.

How to properly design API module in TypeScript?

I want to design a TypeScript (2.7) module for accessing external IS, let's call it InfoSys. I used the following approach.
I created info-sys.ts which defines a API class and related interfaces and enums, like:
class Api {
constructor(private endpoint: Endpoint) {
// ...
}
}
enum Endpoint {
CONTACTS = "contacts"
}
interface Contact {
name: string;
}
Now I want to export all the stuff under specific name. So I appended the export statement:
export const InfoSys = {
Api,
Endpoint,
Contact
};
When I try to use the module in another file, like:
import { InfoSys } from "info-sys";
// this line throws error: "Cannot find namespace 'InfoSys'"
private api: InfoSys.Api;
// but this line is ok
api = new InfoSys.Api(InfoSys.Endpoint.CONTACTS);
The way that works is the following - to export every piece individually:
export class Api {
constructor(private endpoint: Endpoint) {
// ...
}
}
export enum Endpoint {
CONTACTS = "contacts"
}
export interface Contact {
name: string;
}
and import them all to a single variable:
import * as InfoSys from "info-sys";
But the name of the variable can be whatever. It is not critical for functionality but I want to force developers, who will use the info-sys module, to use a specific name while accessing it (for easier readability and maintainability). How to properly design such module?
You can use namespace:
export namespace InfoSys {
Api,
Endpoint,
Contact
};
In general, this approach should be avoided. But in your case, it is fine as you are delivering things that are tightly related.
If Api is the single entry point to all these, I would also recommend this:
export class InfoSysApi { ... }
export namespace InfoSysApi {
export enum Endpoint = { ... }
export interface Contact { ... }
}
UPDATE:
To make sure I get the point through, DON'T do the following:
export namespace Foo {
export function X() { return 'x' }
export function Y() { return 'y' }
}
Only use export namespace to export "tugged in types", not values.
In TypeScript handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html
Although the table says namespace can contain values, it is considered bad practice if you are writing ESM (import/export).
Namespace and ESM are two different mechanisms to achieve similar result.
Don't mix them together.

Best practice for string String in Ionic 3

What is the best practice for storing all app Strings in Ionic 3. Should I created one data provider and store all strings as constants?
There are multiple ways.But I have chosen very simple approach here.That is, Just create a static class as shown below.
Note: I extracted below example from my working project.
Constant file:
Handlers.ts
export class Handlers {
static BUDGET_PAGE_TOTAL_HANDLER = "budget-page-total-handler";
static NEW_PROJECT_PAGE_BUDGET_HANDLER = "new-project-page-budget-handler";
static HOME_PAGE_TRANSACTION_HANDLER = "home-page-transaction-handler";
}
Use like this:
.ts
import { Handlers } from '../../constants/Handlers';
eventHandlers() {
this.events.subscribe(Handlers.HOME_PAGE_TRANSACTION_HANDLER, this.transactionHandler);
}

Breeze & EFContextProvider - How to properly return $type when using expand()?

I am using Breeze with much success in my SPA, but seem to be stuck when trying to return parent->child data in a single query by using expand().
When doing a single table query, the $type in the JSON return is correct:
$type: MySPA.Models.Challenge, MySPA
However if I use expand() in my query I get the relational data, but the $type is this:
System.Collections.Generic.Dictionary 2[[System.String, mscorlib],[System.Object, mscorlib]]
Because of the $type is not the proper table + namespace, the client side code can't tell that this is an entity and exposes it as JSON and not a Breeze object (with observables, entityAspect, etc.).
At first I was using my own ContextProvider so that I could override the Before/After saving methods. When I had these problems, I reverted back to the stock EFContextProvider<>.
I am using EF5 in a database first mode.
Here's my controller code:
[BreezeController]
public class DataController : ApiController
{
// readonly ModelProvider _contextProvider = new ModelProvider();
readonly EFContextProvider<TestEntities> _contextProvider = new EFContextProvider<TestEntities>();
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IQueryable<Challenge> Challenges()
{
return _contextProvider.Context.Challenges;
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
public IQueryable<ChallengeNote> ChallengeNotes()
{
return _contextProvider.Context.ChallengeNotes;
}
}
Here's my BreezeWebApiConfig.cs
public static void RegisterBreezePreStart()
{
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi",
routeTemplate: "breeze/{controller}/{action}"
);
}
Is there a configuration setting that I am missing?
Did you try "expanding" on server side? Is it needed to do expand on client side? I tried to do expand before but failed for me as well, did some research and decided I'd rather place it on server:
[HttpGet]
public IQueryable<Challenge> ChallengesWithNotes()
{
return _contextProvider.Context.Challenges.Include("ChallengeNotes");
}
This should be parsed as expected. On client side you would query for "ChallengeNotes" instead of "Challenges" and you wouldn't need to write expand part.
I strongly suspect that the problem is due to your use of the [Queryable] attribute.
You must use the [BreezeQueryable] attribute instead!
See the documentation on limiting queries.
We are aware that Web API's QueryableAttribute has been deprecated in favor of EnableQueryAttribute in Web API v.1.5. Please stick with BreezeQueryable until we've had a chance to write a corresponding derived attribute for EnableQuery. Check with the documentation for the status of this development.

Resources