Download xlsx file without Headers, Vuejs - laravel

I have a download action, that it works, but in my .xlsx file I don't have the header, and I am not sure how can I get them. I'm using Vuejs / Axios with Laravel.
<a type="button" class="mr-3" href="/json/persons/export" download="file.xlsx">
<button #click="exportCSV" class="btn btn-primary">
Export CSV
</button>
</a>
exportCSV() {
axios
.get("/json/persons/export", {
params: {
sort_by: this.sortBy,
sort_direction: this.sortDesc
}
})
.then(response => {
//
})
.catch(error => {
//
});
},
Export Class Code
namespace App\Exports;
use App\ViewData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Excel;
class DataExport implements FromCollection
{
public function collection()
{
return ViewData::all();
}
}

Well you can actually do something like
namespace App\Exports;
use App\ViewData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Excel;
class DataExport implements FromCollection, WithHeadings
{
public function collection()
{
return ViewData::all();
}
//Define your desired headings this way, it's just for an example
public function headings(): array
{
return [
'Name',
'Surname',
'Email',
'Twitter',
];
}
}

Related

laravel log debug is not working.I checked A response is returned from the server side

laravel log debug is not working.
I create app with laravel5 and vue.js.
I send a request to the server side with vue and catch this with laravel Controller.php.
I code Log::debug to PostsController.php, but the log is not written in laravel.log.
I try use Illuminate\Support\Facades\Log; and use Log; , but both are not working.
Do you find anything wrong with my code?
Thank you for your help.
// web.php
Route::get('/posts/items', 'PostsController#items');
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Model\Post;
use App\Model\Item;
// use Log;
use Illuminate\Support\Facades\Log;
use App\Http\Resources\Post as PostResource;
use App\Http\Resources\Item as ItemResource;
class PostsController extends Controller
{
public function items(Request $request)
{
Log::info('kita');
Log::debug('kurukao');
logger()->debug('debug');
$post_id = $request->input('post_id');
return $post_id;
}
//vue
methods: {
mouseenter(id) {
this.itemId = id;
},
mouseleave(id) {
this.itemId = null;
},
getItems() {
axios.get("/posts/items", {
post_id: this.$route.params.id
})
.then((res) => {
// console.log(res.data)
console.log('ok')
this.postDataList = res.data.data;
// console.log(this.postDataList)
if(this.postDataList != null){
this.itemDataList = this.postDataList[0]['items'];
}
});
},

Not displaying the content while broadcasting with pusher in Laravel

I am developing a live chat app with Laravel 7 and Pusher as broadcast driver.
And i am facing a weird problem because its broadcasting an empty object,
but in my console I am receiving the right object/comment.
indexComponent.vue:
<template>
<div>
<h1>All Comments</h1>
<div style="margin-bottom:20px;">
<textarea class="form-control" rows="3" name="body" placeholder="write a comment" v-model="commentBox" ></textarea>
<button class="btn btn-success" style="margin-top:10px" v-on:click="createComment()" >Save Comment</button>
</div>
<div v-for='comment in this.listOfComments' v-bind:key="comment.id" class='card card-body mb-2'>
<p>{{comment.body}}</p>
</div>
</div>
</template>
<script>
export default {
props: ['comments'],
data() {
return {
commentBox: '',
listOfComments: this.comments,
}
},
created: function() {
this.listen();
},
methods: {
createComment: function() {
axios.post('/api/comment', {
body: this.commentBox,
title: 'from axios',
user_id: '1'
})
.then(response => {
this.listOfComments.unshift(response.data)
this.commentBox = '';
})
.catch(error => {
console.log(error.response);
});
},
listen: function() {
Echo.channel('commentsChannel')
.listen('NewComment', (cmn) => {
console.log(cmn);
this.listOfComments.unshift(cmn);
console.log(this.comments[0]);
})
}
},
}
</script>
NewComment.php (event)
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; //for no queueing
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Comment;
class NewComment implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $comment;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Comment $comment)
{
$this->comment = $comment;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('commentsChannel');
}
}
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use Auth;
use App\Events\NewComment;
class CommentsController extends Controller {
public function index() {
$comments = Comment::orderBy('created_at','desc')->get();
return view('comments/index')->with('comments',$comments);
}
public function store(Request $request) {
$comment = new Comment;
$comment->body = $request->body;
$comment->title = $request->title;
$comment->user_id = $request->user_id;
if($comment->save()){
// event(new NewComment($comment));
broadcast(new NewComment($comment))->toOthers();
return $comment->toJson();
}
}
}
Please note that wen i submit a new comment the event is happening, a new division have been created, but its empty.
Anyone can help with this problem ?! Thank you in advance

Maatwebsite excel export

I have a simple query that generates files using the maatwebsite
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use App\MyDB;
use Auth;
class ReportExport implements FromCollection
{
public function collection()
{
$email = Auth::user()->email;
return MyDB::MyFunction($email)
->select('Reference_Number')->get();
}
}
everything is working fine, but how can i add headers?
I tried looking at the documentation but it confused me more.
https://laravel-excel.maatwebsite.nl/docs/3.0/export/mapping
I must also admit. The documentation on Laravel Excel tries to mess with your head. You can add headings by adding a headings function to your export class:
use Maatwebsite\Excel\Concerns\WithHeadings;
class ReportExport implements FromCollection, WithHeadings
{
public function collection()
{
$email = Auth::user()->email;
return MyDB::MyFunction($email)
->select('Reference_Number')->get();
}
public function headings(): array
{
return [
'Heading 1',
'Heading 2',
'Heading 3',
];
}
}

Vue.js Property or method "columns" is not defined on the instance but referenced during render

Where am I making mistakes? I'm the beginner when it comes to Vue.js. Can somebody help me and explain? I'm trying to make the table filters.
namespace App\Http\Controllers\cofus\reports;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Client;
class ClientController extends Controller{
public function __construct()
{
$this->middleware('auth:admin');
}
public function allClients()
{
$clients = Client::orderBy('id', 'desc')->paginate(10);
return view('cofus.reports.clients')->with('clients', $clients);
}
public function getData()
{
$model = Client::searchPaginateAndOrder();
$columns =Client::$columns;
response()
->json([
'model' => $model,
'columns' => $columns
]);
}
}
web.php
Route::prefix('cofus')->group(function(){
Route::get('/reports/clients', 'cofus\reports\ClientController#allClients')->name('reports.clients');
Route::get('/api/clients', 'cofus\reports\ClientController#getData');
});
localhost:8088/cofus/cofus/reports/clients
<script src="https://unpkg.com/vue#2.5.16/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="root">
<clients source="/cofus/cofus/api/clients" title="Client Data"></clients>
<option v-for="column in columns" :value="column"></option>
</div>
<script>
var clients = Vue.component('clients',{
props: ['source', 'title'],
data(){
return {
model: {},
columns: {}
}
},
render: function created(){
this.fetchIndexData()
},
methods: {
fetchIndexData(){
var vm = this
axios.get(this.source)
.then(function(response) {
Vue.set(vm.$data, 'model', response.data.model)
Vue.set(vm.$data, 'columns', response.data.columns)
})
.catch(function(response){
console.log(response)
})
}
}
});
new Vue({
el: '#root',
})
</script>

FatalErrorException in Articles.php line 22: Call to undefined method Carbon\Carbon::createFormFormat()

I am getting error on this code so I start it from blade, then controller then modle kindly give me solution why this problem happened.
blade:
#extends ('lay')
#section('content')
<h1>Write a New Article</h1>
<hr>
{!!Form::open(['url'=>'articles'])!!}
<div class="form-group">
{!!Form::label('title','Title:')!!}
{!!Form::text('title','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('body','Body:')!!}
{!!Form::textarea('body','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('published_at','Published on:')!!}
{!!Form::input('date','published_at',date('Y-m-d'),['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Article',['class'=>'btn btn-primary form-control','name'=>'submit'])!!}
</div>
{!!Form::close()!!}
#stop
controller: this is my controller of the program all this is made in laravel
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Articles;
use Request;
use Carbon\Carbon;
class ArticlesController extends Controller {
public function index()
{
/*$art=[
'title'=>'ashwani',
'body'=>'rathi',
'published_at'=>'Carbon\Carbon::now()'
];
Articles::create($art);*/
$articles=Articles::latest()->get();
return view('articles.index', compact('articles'));
}
public function show($id)
{
$article=Articles::findorFail($id);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store()
{
//$input=Request::all();
//$input['published_at']=Carbon::now();
//Articles::create($input);
Articles::create(Request::all());
return redirect('articles');
}
}
model: this is model where i using date and its not working
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Articles extends Model {
protected $fillable= [ 'title', 'body', 'published_at' ];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d|', $date);
}
}
It looks like this is stemming from a small typo.
You have put createFormFormat, where you have spelt Form instead of From.
You just need to correct this in your function:
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
You can use
public function getPublishedAtAttribute($date){
return Carbon::parse($date)->format('Y-m-d');
}
public function setPublishedAtAttribute($date){
$this->attributes['published_at'] = Carbon::parse($date);
}
in place of
public function setPublishedAtAttribute($date){
//$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d', $date);
return $this->attributes['published_at']->format('Y-m-d');
}

Resources