LaravelExcel Maatwebsite does not export data - php

I'm working with Laravel 5.8 and I have added LaravelExcel Maatwebsite to export some data from the DB into Excel file.
So I tried adding this method to the Model:
public static function getAllData()
{
$records = DB::table('orders')->select('ord_id','ord_date','ord_total')->get()->toArray();
}
And then I made this Export Class:
class OrderAllDataExport implements FromCollection, WithHeadings
{
public function headings():array
{
return [
'ID',
'Date',
'Total',
];
}
public function collection()
{
return collect(Order::getAllData());
}
}
And also added this method to the Controller:
public function exportAllDataIntoExcel()
{
return Excel::download(new OrderAllDataExport,'orders_all.xlsx');
}
And this is the route:
Route::get('export/data/list/orders' , 'OrdersController#exportAllDataIntoExcel')->name('exportAllDataIntoExcel');
But when I test this, the Excel file downloaded properly but it is empty!

Have you try putting your select query to collection to test it?
public function collection()
{
$data = DB::table('orders')
->select('ord_id','ord_date','ord_total')
->get();
return $data;
}
also you can try add return to your getAllData()
public static function getAllData()
{
$records = DB::table('orders')->select('ord_id','ord_date','ord_total')->get();
return $records;
}
and because u use collection i removed the toArray() function

Related

How to Export data into excel in laravel 8 with maatwebsite-excel. excel not exporting

I have used maatwebsite-excel but i am getting issue while exporting. This is my Controller.
public function exportOneTimePayments(){
return Excel::download(new ExportOneTimePayments, 'one-time-payments-'.date("Y-m-d").'.csv');
}
And This is my export Class
class ExportOneTimePayments implements FromQuery, WithHeadings, WithMapping {
public function query()
{
return SubscriptionOneTimePayment::query()
when(request()->pg_subscription_id,function($query){
$query->whereHas('subscription', function ($q){
$q->where('pg_subscription_id', request()->pg_subscription_id);
});
})
->orderBy('id','desc')
->get();
}
public function batchSize(): int
{
return 1000;
} }

How can I filter this with eloquent

I try to filter SurveyQuestionnaire which have Answer = 'poor' and their Questionnaire have step = 'rating'.
I've tried to look through the documentation of Eloquent and I've found nothing help.
This is my models.
class Questionnaire extends Model {
...
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id');
}
public function answers() {
return $this->hasMany(Answer::class, 'question_id');
}
public function questionnaires() {
return $this->hasMany(QuestionnaireTranslation::class, 'question_id' );
}
}
class SurveyQuestionnaire extends Model {
public function survey() {
return $this->belongsTo(Survey::class ,'survey_id');
}
public function questionnaires() {
return $this->belongsTo(Questionnaire::class, 'question_id');
}
public function answer() {
return $this->belongsTo(Answer::class, 'answer_id');
}
}
Well, the hasMany method returns query builder, so you can simply add some conditions:
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id')->where('Answer', 'poor');
}
Also, you can read this link Constraining Eager Loads and add your conditions manually after taking an instance of your model:
$items = App\Questionnaire::with(['surveysQuestionnaires' => function ($query) {
$query->where('Answer', 'poor');
}])->get();

App\PageList::coupon must return a relationship instance in laravel 5.5

I am trying to get a count() from database but I am getting an error.
I am trying to find how many N are in is_ordered field.
My Model look like:
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id')
->where('is_ordered','N')->count();
}
and my blade view:
<p>{{optional($value->coupon)->is_ordered}}</p>
I am unable to find a solution.
your help will be highly appreciated!
public function pageListHere()
{
$list = PageList::all();
return view('page-list',compact('list'));
}
class PageList extends Model
{
protected $table = 'page_master';
protected $fillable = ['business_id', 'page_url', 'page_name'];
public function particulars()
{
return $this->hasOne('App\Sale','user_id','business_id');
}
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id');
}
}
You can try doing it this way.
Leave your first function coupon like this:
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id')->where('is_ordered','N');
}
And then you can have a second function as so:
public function couponCount()
{
return $this->coupon->count();
}
After that you can use it in blade on your variable like so: $coupon->couponCount();
Your code and what you want is little bit confusing. try this:
PageList.php
public function coupon()
{
return $this->hasMany('App\PageCoupon','page_id','business_id');
}
Controller
public function pageListHere()
{
$list = PageList::all();
return view('page-list',['list'=>$list]);
}
View
#foreach($list as $listItem)
<p>{{$listItem->coupon()->where('is_ordered','N')->count()}}</p>
#endforeach

Laravel return Illegal offset type when I trie return a model

I'm working with a large json that returns several data from the database and I need to return an integer model that does not have any kind of relationship, I just need to return all the records that LampModels model with this great json. But Laravel always returns me Illegal offset type.
Controller
public function showAllUdiJson()
{
$allLamps = LampModels::all();
return Ilumination::with('street')
->with('neighborhood')
->with('iluminationinfo')
->with('economyplan')
->with('lamp')
->with('reactor')
->with('aluminumcable')
->with('steelconduit')
->with('alllamps', $allLamps)
->with('ticket')->get();
}
LampModels
<?php
class LampModels extends \Eloquent {
protected $fillable = [];
protected $table = 'lampmodel';
}
Illumination
<?php
class Ilumination extends \Eloquent {
protected $fillable = [];
protected $table = 'ilumination';
public function street()
{
return $this->belongsTo('street');
}
public function neighborhood()
{
return $this->hasOne('neighborhood', 'id');
}
public function iluminationinfo()
{
return $this->hasOne('iluminationinfo');
}
public function ticket()
{
return $this->hasMany('ticket');
}
public function economyplan()
{
return $this->hasOne('economyplan', 'id' ,'street_id');
}
public function lamp()
{
return $this->hasOne('lamp', 'id');
}
public function reactor()
{
return $this->hasOne('reactor', 'id');
}
public function aluminumcable()
{
return $this->hasOne('aluminumcable', 'id');
}
public function steelconduit()
{
return $this->hasOne('steelconduit', 'id');
}
}
See the error
Your error report is quite bad, but seems that your Ilumination model doesnt have an alllamps method.
You should attacth LampModels to your Ilumination model with a relationship, insted of doing what you doing, cause is a wrong approach.
I think you access somewhere ticket method which was created in Illumination Model that offset error encountered..
public function ticket()
{
return $this->hasMany('ticket');
}
if you want to access illumination->ticket, you must use this method with loop.
foreach(illumination->tickets as ticket) {
$field1 = ticket->field1;
}
If your still facing any issue than share your error log page here..

Eager loading on polymorphic relations

class Admin {
public function user()
{
return $this->morphOne('App\Models\User', 'humanable');
}
public function master()
{
return $this->hasOne('App\Models\Master');
}
}
class Master {
public function admin()
{
return $this->hasOne('App\Models\Admin');
}
}
class User {
public function humanable()
{
return $this->morphTo();
}
public function images()
{
return $this->hasOne('\App\Models\Image');
}
}
class Image {
public function user()
{
return $this->belongsTo('\App\Models\User');
}
}
Now if I dump this:
return \App\Models\Admin::where('id',1)->with(array('user.images','master'))->first();
I get the perfect result one master, one user and one image record.
But if I do this
return $user = \App\Models\User::where('id',1)->with(array('humanable','humanable.master'))->first();
I only get one Admin record, the query get * from masters doesn't even run.
Any idea what I'm doing wrong, I'm sure this is possible.
If I remember correctly Laravel has lots of pitfall. You can try to use the protected $with var in Admin model instead of query builder with function.
class Admin {
protected $with = ['master'];
public function user() {
return $this->morphOne('App\Models\User', 'humanable');
}
public function master() {
return $this->hasOne('App\Models\Master');
}
}
In query builder, only need to include humanable. Now you should see master inside the humanable object.
return $user = \App\Models\User::where('id',1)->with('humanable')->first();
Hope this help.

Categories