I want to change the name of the column heading.
How do you do?
This is the code in the controller:
public function excelterima($nm_perusahaan){
if($user = Auth::user()->admin == 2){
$users = Mstbeasiswa::select('NO_ANGGOTA', 'NM_ANGGOTA', 'GOLONGAN', 'NPK', 'CABANG', 'NM_ANAK', 'NM_SKL', 'id')
->where('flag_terima', '1')
->where('NM_PERUSAHAAN', $nm_perusahaan)
->orderBy('id', 'asc')
->get()->toArray();
//work on the export
Excel::create($nm_perusahaan, function($excel) use ($users){
$excel->sheet('sheet 1', function($sheet) use ($users)
{
$sheet->fromArray($users);
ob_end_clean();
});
})->download('xlsx');
return redirect('/beasiswaditerima');
}else{
return redirect('/home');
}
}
output current:
By default the fromArray() method of LaravelExcelWorksheet instance will auto-generate the heading for you based on the given array keys. In order to disable this auto-generation, you need to pass false to the fifth parameter (the $headingGeneration). Here's the fromArray() method signature for your reference:
public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false, $headingGeneration = true)
You can use the row() method to add a custom heading. Using your code example, now the code should look like this:
$users = Mstbeasiswa::select('NO_ANGGOTA', 'NM_ANGGOTA', 'GOLONGAN', 'NPK', 'CABANG', 'NM_ANAK', 'NM_SKL', 'id')
->where('flag_terima', '1')
->where('NM_PERUSAHAAN', $nm_perusahaan)
->orderBy('id', 'asc')
->get()
->toArray();
Excel::create($nm_perusahaan, function ($excel) use ($users) {
$excel->sheet('sheet 1', function ($sheet) use ($users) {
// Set your custom header.
$sheet->row(1, ['COL1', 'COL2', 'COL3', 'COL4', 'COL5', 'COL6', 'COL7', 'COL8']);
// Set the data starting from cell A2 without heading auto-generation.
$sheet->fromArray($users, null, 'A2', false, false);
});
})->download('xlsx');
Or you can actually just keep the fromArray() method calling, but later replace the auto-generated header with row() method like so:
$excel->sheet('sheet 1', function ($sheet) use ($users) {
$sheet->fromArray($users);
// Replace the header, but this should come after fromArray().
$sheet->row(1, ['COL1', 'COL2', 'COL3', 'COL4', 'COL5', 'COL6', 'COL7', 'COL8']);
});
Hope this help!
Related
I'm trying to filter products based on query string. My goal is to get products from a collection if it's given, otherwise get every product. Could someone help me what's wrong with the following code?
$products = \App\Product::where([
'collection' => (request()->has('collection')) ? request('collection') : '[a-z]+',
'type' => (request()->has('type')) ? request('type') : '[a-z]+'
])->get();
PS.: I've also tried with 'regex:/[a-z]+', it's not working...
$products = \App\Product::where(['collection' => (request()->has('collection')) ? request('collection') : 'regex:/[a-z]+'])->get();
What you can do is use when eloquent clause, so your where clause for collections will be triggered only when the request('collection') exists, same logis applie to type as well.
$products = \App\Product::
when(request()->has('collection'), function ($q) {
return $q->where('collection', request('collection'));
});
->when(request()->has('type'), function ($q) {
return $q->where('type', request('type'));
})
->get();
Or another way if you have your request values assigned to a variable something like:
$collection = request('collection');
$type= request('type');
$products = \App\Product::
when(!empty($collection), function ($q) use ($collection) {
return $q->where('collection', $collection);
});
->when(!empty($type), function ($q) use ($type) {
return $q->where('type', $type);
})
->get();
I have this code in Lumen 5.6 (Laravel microframework) and I want to have an orderBy method for several columns, for example, http://apisurl/books?orderBy=devices,name,restrictions,category also send asc or desc order.
Lumen's documentation says that we can use the orderBy like this
$books = PartnersBooks::all()->orderBy('device', 'asc')->orderBy('restrictions', 'asc')->get();
So, I made a function with a foreach to fill an array with different orderBy requests values and tried to put on eloquent queries without succeeding.
Can anybody help me?
use Illuminate\Http\Request;
public function index(Request $request)
{
$limit = $request->input('limit');
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->orderBy('id', 'asc')
->paginate($limit, ['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
You can do this:
// Get order by input
$orderByInput = $request->input('orderBy');
// If it's not empty explode by ',' to get them in an array,
// otherwise make an empty array
$orderByParams = !empty($orderByInput)
? explode(',', $orderByInput)
: [];
$query = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
});
// Foreach over the parameters and dynamically add an orderBy
// to the query for each parameter
foreach ($orderByParams as $param) {
$query = $query->orderBy($param);
}
// End the query and get the results
$result = $query->paginate($limit);
I'm new on Resfully services and Lumen (Laravel micro-framework).
I want to pass the /books?limit=10&offset=5 parameters to the controller and set it on the json response and I can't figure out how.
web.php
$router->get('/books/', ['uses' => 'BooksController#index']);
BooksController.php
public function index()
{
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset(5) // This should have an default value until the user pass a value through url
->limit(30) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
Could you please help me?
Thanks,
You could to use the Request object to do that:
use Illuminate\Http\Request;
public function index(Request $request)
{
$limit = $request->input('limit');
$offset = $request->input('offset');
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset($offset) // This should have an default value until the user pass a value through url
->limit($limit) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
I want to download 2 user's data from tables, I want to generate excel file of that data.
here is the download function for only one data table called registerdetails.
public function export(){
$items = registerdetails::all();
Excel::create('General Trainee Details', function($excel) use($items){
$excel->sheet('Sheet 1', function($sheet) use($items){
$sheet->fromArray($items);
});
})->export('xlsx');
}
I need this controller to be modified get datas from registerdetails and bankdetails. if anyone can help me to get this solved?
try this
public function collectionexport_all(){
$items = registerdetails::join('bankdetails', 'bankdetails.id', '=', 'registerdetails.id')->get();
$itemsArray = [];
foreach ($items as $item) {
$itemsArray[] = $item->toArray();
}
Excel::create('Full Details', function($excel) use($itemsArray){
$excel->sheet('Sheet 1', function($sheet) use($itemsArray){
$sheet->fromArray($itemsArray);
});
})->export('xlsx');
}
I'm not sure what you want to achieve.. But I hope this helps.
The Excel Library you're using (hoping it's maatwebsite/excel or its like) can make use of Eloquent queries or Query Builder like so:
public function export($id){
Excel::create('General Trainee Details', function($excel) use($id){
$excel->sheet('Sheet 1', function($sheet) use($id){
$query= registerdetails::select(['registerdetailscolumn_1', 'registerdetailscolumn_2')->leftjoin('bankdetails', 'registerdetailscolumn_id', '=', 'bankdetailscolumn_id')->where(registerdetailscolumn_id, '=', $id);
$sheet->fromModel($query);
});
})->export('xlsx');
}
Of course, knowing you can provide and pass along whatever employee's id it is.
you can export one table on one sheet and another table on another sheet like so:
public function export(){
Excel::create('General Trainee Details', function($excel) use($id){
$excel->sheet('Sheet 1', function($sheet){
$query= registerdetails::all();
$sheet->fromModel($query);
});
$excel->sheet('Sheet 2', function($sheet){
$query= bankdetails::all();
$sheet->fromModel($query);
});
})->export('xlsx');
}
I want to make export data into excel in my project, i have made it, but after I check the results, the results doesnt like what I want. here I would like to make a result there is a title table.
This code my controller:
public function getExport(){
Excel::create('Export data', function($excel) {
$excel->sheet('Sheet 1', function($sheet) {
$products=DB::table('log_patrols')
->join("cms_companies","cms_companies.id","=","log_patrols.id_cms_companies")
->join("securities","securities.id","=","log_patrols.id_securities")
->select("log_patrols.*","cms_companies.name as nama_companies","securities.name as nama_security")
->get();
foreach($products as $product) {
$data[] = array(
$product->date_start,
$product->date_end,
$product->condition_status,
$product->nama_security,
$product->nama_companies,
);
}
$sheet->fromArray($data);
});
})->export('xls');
}
this my problem result :
and it should be :
my problem is how to change the number into text what i want in the header table.
what improvements do i have to make to the code to achieve my goal?
NB : i use maatwebsite/excel
From the official docs:
By default the export will use the keys of your array (or model
attribute names) as first row (header column). To change this
behaviour you can edit the default config setting
(excel::export.generate_heading_by_indices) or pass false as 5th
parameter:
Change:
$sheet->fromArray($data); to $sheet->fromArray($data, null, 'A1', false, false);
how to change the number into text what i want in the header table.
Then you can define your own heading and prepend it to the first row of the sheet.
$headings = array('date start', 'date end', 'status condition', 'security', 'company');
$sheet->prependRow(1, $headings);
That should make it work.
Try this in your controller
$data=Insertion::get()->toArray();
return Excel::create('yourfilename', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->cell('A1:C1',function($cell)
{
$cell->setAlignment('center');
$cell->setFontWeight('bold');
});
$sheet->cell('A:C',function($cell)
{
$cell->setAlignment('center');
});
$sheet->cell('A1', function($cell)
{
$cell->setValue('S.No');
});
$sheet->cell('B1', function($cell)
{
$cell->setValue('Name');
});
$sheet->cell('C1', function($cell)
{
$cell->setValue('Father Name');
});
if (!empty($data)) {
$sno=1;
foreach ($data as $key => $value)
{
$i= $key+2;
$sheet->cell('A'.$i, $sno);
$sheet->cell('B'.$i, $value['name']);
$sheet->cell('C'.$i, $value['fathername']);
$sno++;
}
}
});
})->download(xlsx);