export to excel file one specific row records - php

in laravel 5.1 using maatweb / Excel package i need to export specific row records to excel file where the current page id is viewing
in my VlistController
public function export()
{
Excel::create('Company List', function($excel)
{
$excel->sheet('companies', function($sheet)
{
$data = Vlist::all();
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
$sheet->fromArray($companies);
});
})->download('xlsx');
}
in my routes file
Route::get('/vlist/{vlist}/export' , 'VlistController#export');
in my show view
<li><i class='fa fa-link'></i> <span>Export Supplier : {!! $vlist->vname !!}</span></li>
the above controller code list all the records in excel sheet and i need only one specific record with the active id .

If I understand you problem,you can try this code.You can query by your primary key to retrieve specific row.
public function export($id)
{
Excel::create('Company List', function($excel) use ($id)
{
$excel->sheet('companies', function($sheet) use ($id)
{
$data = Vlist::where('id', $id)->get();
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
$sheet->fromArray($companies);
});
})->download('xlsx');
}
and I don't think the following code is necessary.
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
You can simply use the retrieve specific data
$data = Vlist::where('id', $id)->get(['vname', 'vaddress']);
and pass it to the method
$sheet->fromArray($data);
Thanks

I am in not sure about your Question, in my point of view you want to put(show) record form a specified row then my solution will work.
$projectArr[] = ["col1","col2","col3","col4","col5","col6"];
Excel::create('project_directory_result_sheet', function($excel) use($projectArr) {
$excel->sheet('Project_Directory_Result', function($sheet) use( $projectArr ) {
$sheet->fromArray($projectArr,null, 'A3');
}
}
In my solution noticeable point is $sheet->fromArray($projectArr,null, 'A3');
here i am giving 3 parameter value that are
first:Project Value Array.
second: show header true or false
third: row number where you want to represent record.

Related

Laravel - Copy collection data to another table and delete

I want to delete entries which are older than 3 days and move them to another (archive) table.
So far I do it like this:
public function handle() {
$route = Route::where('created_at', '<=', Carbon::now()->subDays(3))->get();
$routeCopy = $route;
$route = Route::where('created_at', '<=', Carbon::now()->subDays(3))->delete();
foreach ($routeCopy as $r) {
$routeArchive = new RouteArchive();
$routeArchive->id = $r->id;
$routeArchive->startLocation = $r->startLocation;
$routeArchive->endLocation = $r->endLocation;
$routeArchive->save();
}
}
Is there a way to avoid double querying in this case?
Btw Route and RouteArchive are not same. Route contains many other columns including id, startLocation, endLocation... RouteArchive contains only id, startLocation and endLocation.
Assuming that you have a primary key set up on the route table, you should be able to do something like this
public function handle() {
$route = Route::where('created_at', '<=', Carbon::now()->subDays(3))->get();
// $routes = $route;
// $route = Route::where('created_at', '<=', Carbon::now()->subDays(3))->delete();
foreach ($route as $r) {
$routeArchive = new RouteArchive();
$routeArchive->id = $r->id;
$routeArchive->startLocation = $r->startLocation;
$routeArchive->endLocation = $r->endLocation;
$routeArchive->save();
$r->delete();
}
}

How to pass the data while using two foreach loop in laravel controller

public function index2(){
$s_books = raws::orderBy('sequence', 'ASC')->where('type',"book")->get();
foreach ($s_books as $data) {
$menu="{$data->menu}";
$main_books = books::orderBy('id', 'DESC')->where("menu",$menu)->get();
}
return view('index')->with(['main_books'=>$main_books]);}
And this is Blade
#foreach($main_books as $data)
{{$data->id}}
#endforeach
But this is showing only the last data.
How can I show all tha data in $main_book in laravel blade???
The code you have, you will get only the last item, you need to put all in an array. Try this code in you controller.
$main_books = [];
foreach ($s_books as $data) {
$menu="{$data->menu}";
array_push($main_books, books::orderBy('id', 'DESC')->where("menu",$menu)->get());
}

Update multiple ids against single id (Laravel)

I want to update multiple Departments against one unit. I tried this method, but it's not correct.
How can I update multiple departments ids?
Form:
Request:
Controller Function:
$pre_data = UnitDepartment::where('unit_id', $request->id)->get();
if ($pre_data) {
foreach ($pre_data as $value) {
$value->delete();
}
$department = $request->department_id;
foreach ($department as $value) {
$unitDepart = new UnitDepartment();
$unitDepart->unit_id = $request->id;
$unitDepart->department_id = $value;
$unitDepart->save();
}
}
table:
I found that is the table related to departments and units.
So you can build the relationship many-to-many between them,
Create the relationship in your models,
In Unit model:
public function departments()
{
return $this->belongsToMany('App\Unit','unit_department','unit_id','department_id');
}
In Department Model:
public function units()
{
return $this->belongsToMany('App\Department','unit_department','department_id','unit_id');
}
Attach the new relationship, simply use:
Unit::find($request->unit_id)->departments()
->sync($request->department_id);
Unfortunately, you cannot use softDelete on sync().
And I don't think you need to soft delete with unit_departments. As a pivot then it should be irrelevant if it is deleted or not.
And if user update the relationship on the frequent, this table will grow fast.
If you really need to soft-delete, you can write it like this:
$department_ids = $request->department_id;
$unit_id = $request->unit_id
// soft delete the unit_departments not in request:
UnitDepartment::where('unit_id', $unit_id)->whereNotIn('department_id', $department_ids)->delete();
// insert the new department_id+unit_id relationship
$exist_department_ids = UnitDepartment::where('unit_id', $unit_id)->whereIn('department_id', $department_ids)->pluck('department_ids')->all();
$dept_ids = array_diff($exist_department_ids, $department_ids);
$depts = collect($dept_ids)->map(function($dept_id) use ($unit_id) {
return ['department_id' => $dept_id, 'unit_id' => $unit_id];
});
UnitDepartment::insert($depts);
the problem is you're sending unit_id in the request, however using $request->id in the query which is wrong.
Change every occurance of $request->id with $request->unit_id in the controller.
to select pre data correctly
use
$pre_data = UnitDepartment::where('unit_id', $request->id)->first();
i tried this
$unit = UnitDepartment::where('unit_id', $request->unit_id)->get();
foreach ($unit as $item) {
$existDepartment[] = $item->department_id;
}
$newDepartment = $request->department_id;
$result = array_diff($newDepartment, $existDepartment);
if ($result) {
foreach ($result as $item) {
$data = new UnitDepartment();
$data->unit_id = $request->unit_id;
$data->department_id = $item;
$data->save();
}
}

multiple records in json update laravel database

I have a problem when going through my array, it only updates me just one record and I would like to act everything since I want the order to change in my table
public function updateAll(Request $request)
{
$questionOption1 = QuestionOption::all();
foreach ($questionOption1 as $item) {
$id=$item->id;
foreach ($request->all() as $order1) {
if ($order1['id'] == $id) {
$item->update(['order' => $order1['order']]);
}
}
return response('Update Successful.', 200);
}
// vuejs
eupdate() {
this.questionOptionNew.map((item,index)=>{
item.order = index + 1 ;
});
var url = '/api/update';
axios.post(url,this.questionOptionNew)
.then(response=>{
this.getQuestion();
});
console.log("hola",this.questionOptionNew);
},

BadMethodCallException in Macroable.php line 81:Method update does not exist

I an developing a page to create, update, delete and view an event in which there is error while updating the event. There is a event table and a event_collection table. In that event_collection table there is event_id which is id of an event and a collection_id which is from other table collection.
When i create an event, all the data gets stored in event table except the collection one. in the collection table data gets stored in one by one manner like if I check 2 items in collection, it will generate 2 ids with same event_id and 2 collection_ids.
There is problem in update, when i try to update the code, it gives me error as
BadMethodCallException in Macroable.php line 81:
Method update does not exist.
Update method is:
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = $request->all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$query = $event->update($input);
$checkbox_selection = Input::get('agree');
$choosen_checkbox = $id;
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
// return $collection_event;
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
// $collection_id = $id;
foreach($collection_event as $k){
// return $k;
if($k->event_id == $choosen_checkbox){
$data = $request->all();
$data['event_id']= $choosen_checkbox;
$data['collection_id'] = $collection;
$collection_event->update($data);
}
}
}
}
My store method is:
public function store(Request $request)
{
$checkbox = Input::get('days_of_week');
$checkbox_selection = Input::get('agree');
// return $checkbox_collection;
$input = $request->all();
$input['days_of_week'] = serialize($checkbox);
$query = Event::create($input);
$event_id = $query->id;
$pro_id = $query->provider_org_id;
/*For the checkbox selection, if they are multiple store each separately*/
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
$collection_id = $query->id;
if($collection_id){
$data = $request->all();
$data['event_id']= $collection_id;
$data['collection_id'] = $collection;
$create_collection = EventCollection::create($data);
}
}
}
return view('event.pic_upload',compact('event_id','pro_id'));
}
Store method works properly! Can someone please tell any solution? I am badly stucked in this.
I do not think the 'update' method works on collections.
This line will return a collection
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
You do not want a collection, rather a query. As given in the docs:
`App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);`
Try removing the '->get()' from the statement.

Categories