I'm trying to add export functionalities to my Laravel app. I'd like to export db data into an excel spreadsheet. I'm using the Maatwebsite package.
I'm using Laravel 7.12 and 3.1.19 of the Maatwebsite package.
I'm getting the following error when trying to export the data:
Call to undefined method Maatwebsite\Excel\Excel::create()
I added the foloowing use statement to my controller:
use Maatwebsite\Excel\Facades\Excel;
And registered the followings in config/app.php
Maatwebsite\Excel\ExcelServiceProvider::class,
in providers and
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
in the aliases section
Here is my function from the controller file:
public function excel() {
$subscribers = DB::table('subscribers')->get()->toArray();
// Use this for excel spreadsheet header
$subscriber_array[] = array('Name', 'Email');
// Convert subscriber data from php object to array and store them under $subscriber_array
foreach($subscribers as $subscriber) {
$subscriber_array[] = array(
'Name' => $subscriber->name,
'Email' => $subscriber->email
);
}
// "Subscriber Data" will be the name of the generated excel file
Excel::create('Subscriber Data', function($excel) use ($subscriber_array) {
$excel->setTitle('Subscriber Data');
$excel->sheet('Subscriber Data', function($sheet) use ($subscriber_array) {
$sheet->fromArray($subscriber_array, null, 'A1', false, false);
});
})->download('xlsx');
}
Did you try this?
use Maatwebsite\Excel\Facades\Excel;
OR
use Excel
Related
I'm currently on a laravel 5.8 + vue.js 2 project and I want to be able to upload an array of files in a form so it can appear inside the form data and saved.
Inside my form there is a Vue component, made to display the intended files to upload:
<input
name="documents[0][files]"
accept="image/*,application/pdf"
type="hidden"
v-model="files"
>
It has file as its model:
data(){
return{
files: null,
types: {},
label: 'Seleccionar Archivo',
selected_file: null,
file_type_id: '',
file_types: null
}
},
Within methods, onSelectFile assigns the file to a data variable, then it's sent to addNewFile, so said value may come to file, the model:
onSelectFile(e){
if(!e.target.files.length) return;
this.selected_file = e.target.files[0];
this.addNewFile(this.selected_file);
e.target.value = null;
},
addNewFile(file){
this.files = file;
},
However, when it's time to save and store. The formData on File looks like this: Image
Action sends you to the store method in the controller, which sends you to the addPolicy function:
public function store(StorePolicy $request)
{
Policy::addPolicy($request->validated());
addPolicy does a DB transaction for all data (in this case I'll only show what it does to save the file):
public static function addPolicy(array $data)
{
DB::transaction(function () use($data) {
if ( isset($data['documents']) ) {
foreach($data['documents'] as $files){
$policy->documents()->create([
'file' => $files['files']->file('file')->store($policy->employee->urlFolderEmployee(), 'public'),
'file_name' => $files['files']->file('file')->getClientOriginalName()
]);
}
}
return $policy;
});
}
However, the lines of code above never save said file, because it's recognized as a String instead of (binary).
Whenever I want to getClientOriginalName() for example it displays the error:
Call to a member function getClientOriginalName() on string
I've also set the form to accept files:
{!! Form::open(
[
'route'=>['policy.store'] ,
'method'=>'POST',
'id' => 'form_register_new_policy_modal',
'data-prefix' => "{$register}",
'data-modal' => "register_policy_modal",
'files' => true
])
!!}
Is there any way to get the file inside of the input data as a file instead of a string?
i am trying to validate a multi file array for a larvel 7 project
i followed this guide: that suggest using the function:
Validator::make()
However my controller is unable to locate this method and i cannot find it anywhere:
I did use this at the top of my controller:
use \Illuminate\Validation\Validator;
Below is the method i used on my controller
public function uploadSubmit(Request $request)
{
$input = $request->all();
$validator = Validator::make(
$input,
[
'images.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'images.*.required' => 'Please upload an image',
'images.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'images.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);
}
This is the Error i get:
Call to undefined method Illuminate\Validation\Validator::make()
Any suggestions on how to validate multiple files/Images in Laravel 7 would be appreciated.
make function exists in Validator facade. You can use it like so use Illuminate\Support\Facades\Validator;
i have a query to return all the the data in visits table
$visits = Visit::get();
i want to pass the returned data in $data so that i can send it on mail and display the data in the email body
$data = array();
Mail::send('mails.mail', $data, function ($message) use ($host_email, $to_name) {
$message->from('', '');
$message->to($host_email);
$message->subject('Visitor arrived');
});
how do i do that
You can use Laravel structure, You can create a mail class with this command:
php artisan make:mail OrderShipped
it will generate a class in App\Mail path sample named OrderShipped in build method you can call a blade view for this mail and send data to it like:
public function build()
{
return $this->view('emails.orders.shipped')
->with([
'orderName' => $this->order->name,
'orderPrice' => $this->order->price,
]);
}
and finally use this to send email to user:
Mail::to($request->user())->send(new OrderShipped($order));
look here for full documentation:
laravel mail
I am using Laravel 5.8 and maatwebsite/excel 3.1 to export to Excel but got an error.
Call to undefined method Maatwebsite\Excel\Excel::create()
I have written export code in Controller and View
config/app.php
/*
* Package Service Providers...
*/
Maatwebsite\Excel\ExcelServiceProvider::class,
//Class Aliases
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
Controller
use Excel;
public function msisdnExport()
{
$msisdns = User::select(
"phone"
)
->get();
// Initialize the array which will be passed into the Excel
// generator.
$msisdnsArray = [];
// Define the Excel spreadsheet headers
$msisdnsArray[] = ['MSISDN'];
// Convert each member of the returned collection into an array,
// and append it to the payments array.
foreach ($msisdns as $msisdn) {
$msisdnsArray[] = $msisdn->toArray();
}
// Generate and return the spreadsheet
// Excel::create('MSISDN', function($excel) use ($msisdnsArray) {
Excel::download('MSISDN', function($excel) use ($msisdnsArray) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('MSISDN');
$excel->setCreator('Developers')->setCompany('Cloud Africa');
$excel->setDescription('users msisdn file');
// Build the spreadsheet, passing in the payments array
$excel->sheet('sheet1', function($sheet) use ($msisdnsArray) {
$sheet->fromArray($msisdnsArray, null, 'A1', false, false);
});
})->download('xlsx');
}
View
<i class="fa fa-file-excel-o"></i> Excel
When I click on Excel in view, it suppose to export to excel but got this error.
Call to undefined method Maatwebsite\Excel\Excel::create()
The create method has been removed. You have to use one of the following:
Excel::download($yourExport);
Excel::store($yourExport);
As stated in the upgrade guide:
Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
Source: https://docs.laravel-excel.com/3.1/getting-started/upgrade.html#upgrading-to-3-from-2-1
I want to create a PDF using the barryvdh/laravel-dompdf package and send this with an email as attachment.
The code I have now is:
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message) use ($pdf)
{
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});
But now I get the following error:
Serialization of 'Closure' is not allowed
You can only send serializable entities to the queue. This includes Eloquent models etc. But not the PDF view instance. So you will probably need to do the following:
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message)
{
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});