I use Maatwebsite/Laravel-Excel package to upload CSV to DB. Before entering data to DB i wanna do some additional checks with the database. Something like Project_id available in the Project table. Where do i write the required checking function. Is it a proper way to write it in the same controller. I have seen some people create folder In app\Example and write function file in it
UPDATE :
please let me know is this a proper way to do
https://www.youtube.com/watch?v=fS8q_sD1sZM
This the sample CSV format
This the controller function
public function uploadDealerCSV()
{
if(Input::hasFile('file')){
$path = Input::file('file')->getRealPath();
$data = Excel::load($path, function ($reader) {
$reader->ignoreEmpty();
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'project_id' => $value->project_id,
'dealer_bl_code' => $value->bl_code,
'reporgroup' => 104,
'added_by' => 73,
'updated_at' => '2017-08-08 10:34:54',
];
}
if(!empty($insert)){
DB::table('cts_project_list')->insert($insert);
//dd('Insert Record successfully.');
}
}
}
return back();
}
public function testFunction(){
}
Related
Trying to figure out why I am getting the following error:
Undefined index Plugin ID
I am using Maatwebsite\Excel for my import and tried using the guide here:
https://appdividend.com/2017/06/12/import-export-data-csv-excel-laravel-5-4/
I think I have everything in the right place, but I am getting the above error from this code:
public function import(Request $request)
{
if($request->file('imported-file'))
{
$path = $request->file('imported-file')->getRealPath();
$data = Excel::load($path, function($reader)
{
})->get();
if(!empty($data) && $data->count())
{
foreach ($data->toArray() as $row)
{
if(!empty($row))
{
$dataArray[] =
[
'plugin_id' => $row['Plugin ID'],
'cve' => $row['CVE'],
'cvss' => $row['CVSS'],
'risk' => $row['Risk'],
'host' => $row['Host'],
'protocol' => $row['Protocol'],
'port' => $row['Port'],
'name' => $row['Name'],
'synopsis' => $row['Synopsis'],
'description' => $row['Description'],
'solution' => $row['Solution'],
'see_also' => $row['See Also'],
'plugino_utput' => $row['Plugin Output']
];
}
}
if(!empty($dataArray))
{
Shipping::insert($dataArray);
return back();
}
}
}
}
This is in my controller file and is trying to account for the headers being different in the CSV compared to in my database.
Any idea why it would be complaining about index on a column from the csv side of things?
I ended up with this for now from another post. The post still had an extra section in it, but doing a var_dump on $value (which I left in, but commented out) I could see that $value was already an array, so instead of passing it into another array, I tried just inserting with it and that seems to be working.
Still working on placing the error and success messages.
Thanks to ljubadr for helping me learning how to put some print type statements in with the code to see what was getting output at various places.
public function importExcel(Request $request)
{
if($request->hasFile('import_file')){
$path = $request->file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {})->get();
if(!empty($data) && $data->count()){
foreach ($data->toArray() as $key => $value) {
if(!empty($value)){
#var_dump($value);
Item::insert($value);
}
}
return back()->with('success','Insert Record successfully.');
}
}
#return back()->with('error','Please Check your file, Something is wrong there.');
}
}
I'm trying to create an Api using cakephp.
I generate a json on server and it works fine , but I tired to use pagination and I got a problem.
in the first case I take the image's path and I encode it to base64 and I generate json => works
in the second case I defined the pagination by the limits and the max and I kept the same code but as a result the image field is still the path from the database and it's not encoded
this my code in my controller :
class PilotsController extends AppController {
public $paginate = [
'page' => 1,
'limit' => 5,
'maxLimit' => 5
];
public function initialize() {
parent::initialize();
$this->loadComponent('Paginator');
$this->Auth->allow(['add','edit','delete','view','count']);
}
public function view($id) {
$pilot = $this->Pilots->find()->where(['Pilots.account_id' => $id], [
'contain' => ['Accounts', 'Pilotlogs']
]);
foreach ($pilot as $obj) {
if ($obj->image_pilot!= NULL) {
$image1 = file_get_contents(WWW_ROOT.$obj->image_pilot);
$obj->image_pilot = base64_encode($image1);
}
}
$this->set('pilot', $this->paginate($pilot));
$this->set('_serialize', ['pilot']);
}
}
If I remove the pagination from the code it works fine . Any idea how to fix it ??
I'd suggest to use a result formatter instead, ie Query::formatResults().
So you'll have something like this :
public function view($id) {
$pilot = $this->Pilots->find()
->where(['Pilots.account_id' => $id], [
'contain' => ['Accounts', 'Pilotlogs']]);
->formatResults(function($results) {
return $results->map(function($row) {
$image1 = file_get_contents(WWW_ROOT.$row['image_pilot']);
$row['image_pilot'] = base64_encode($image1);
return $row;
});
});
}
You can simply first paginate the data and then get the array values and after that modify that data as you want. Check this
public function view($id) {
$pilot = $this->Pilots->find()->where(['Pilots.account_id' => $id], [
'contain' => ['Accounts', 'Pilotlogs']
]);
$pilot = $this->paginate($pilot);
$pilot = $pilot->toArray();
foreach ($pilot as $obj) {
if ($obj->image_pilot!= NULL) {
$image1 = file_get_contents(WWW_ROOT.$obj->image_pilot);
$obj->image_pilot = base64_encode($image1);
}
}
$this->set('pilot', $pilot);
$this->set('_serialize', ['pilot']);
}
I am inserting the data to the rows one by one, but I have heard somewhere that it requires much time if there are many data to insert. So what are the ways of inserting them all at once?
public function add(Request $request)
{
if ($request->ajax()) {
$books = $request->books;
foreach ($books as $book) {
if (!empty($book)) {
$add = new Book;
$add->name = $book;
$add->user_id = Auth::user()->id;
$add->save();
}
}
}
}
public function add(Request $request)
{
if($request->ajax())
{
$books=$request->books;
$data = array();
foreach($books as $book)
{
if(!empty($book))
{
$data[] =[
'name' => $book,
'user_id' => Auth::id(),
];
}}
Book::insert($data);
<!--DB::table('books')->insert($data);-->
}}
make sure imported use Illuminate\Support\Facades\Auth;
Insert multiple records using the Model
As others have pointed out, using the Query Builder is the only way to insert multiple records at a time. Fortunately Laravel and the Eloquent ORM are coupled in many useful ways. This coupling allows you to use a Model to get a Query Builder instance that is set for that Model.
// use Auth;
// use Carbon;
// use App\Book;
public function add(Request $request)
{
if($request->ajax())
{
// Submitted books
$books = $request->books;
// Book records to be saved
$book_records = [];
// Add needed information to book records
foreach($books as $book)
{
if(! empty($book))
{
// Get the current time
$now = Carbon::now();
// Formulate record that will be saved
$book_records[] = [
'name' => $book,
'user_id' => Auth::user()->id,
'updated_at' => $now, // remove if not using timestamps
'created_at' => $now // remove if not using timestamps
];
}
}
// Insert book records
Book::insert($book_records);
}
}
You should be able to do something like below:
DB::table('users')->insert([
['email' => 'taylor#example.com', 'votes' => 0],
['email' => 'dayle#example.com', 'votes' => 0]
]);
Put all the values you want to insert in to an array and then pass it to the insert function.
Source: https://laravel.com/docs/5.1/queries#inserts
If you need Eloquent model events - there is no other way to insert multiple models. In other way - check Anushan W answer
Is there a better way of organizing or writing the below controller method in Laravel 5.1?
I want to keep my Controllers short and sweet. I am using a repository setup as i'm building quite a large application and I want to keep everything organised.
Please advise on the best way to organise the below code.
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create(CreateTimesheetRequest $request)
{
$data = $request->only('user_id', 'template_id');
$data['submitted_by'] = Auth::user()->id;
$timesheetId = $this->timesheet->createTimesheet($data);
foreach($request->get('row') as $key => $row)
{
foreach($row as $field => $value)
{
$this->timesheet->saveTimesheetRows([
'timesheet_id' => $timesheetId,
'field_id' => $this->timesheetFields->where('name', $field)->first()->id,
'field_name' => $field,
'field_value' => $value,
'field_key' => $key
]);
}
}
return Redirect::back()->withMessage('The timesheet was successfully created.');
}
All I can suggest - move this:
$data = $request->only('user_id', 'template_id');
$data['submitted_by'] = Auth::user()->id;
... into yours request class. For example, into some data() method:
class CreateTimesheetRequest ... {
...
public function data() {
return array_merge(
$this->only('user_id', 'template_id'),
['submitted_by' => Auth::user()->id]
);
}
}
Also, $this->timesheet->saveTimesheetRows(array) looks more like $this->timesheet->saveTimesheetRow(array) for me - name intends to save multiple rows, but you feed that method only with one row per call.
Maybe, you can refactor that method to smth. like this:
function saveTimesheetRows($timesheetId, $key, $rows, $fieldIds) {
foreach($rows as $field => $value) {
$this->saveTimesheetRow([
'timesheet_id' => $timesheetId,
'field_id' => $fieldIds[$field],
'field_name' => $field,
'field_value' => $value,
'field_key' => $key
]);
}
}
function saveTimesheetRow(array $row) {
// old saveTimesheetRows implementation
}
Upd.
And another tip: use Eloquent's keyBy() method like so:
$keyIDs = $this->timesheetFields->whereIn('name', $fields)->get(["name", "id"])->keyBy("name");
So, finally:
public function create(CreateTimesheetRequest $request) {
$data = $request->data();
$timesheetId = $this->timesheet->createTimesheet($data);
foreach($request->get('row') as $key => $row) {
$this->timesheet->saveTimesheetRows(
$timesheetId,
$key,
$row,
$this->timesheetFields
->whereIn('name', array_keys($row))
->get(["name", "id"])
->keyBy("name") // probably, can be moved into $this->timesheetFields implementation
);
}
return Redirect::back()->withMessage('The timesheet was successfully created.');
}
I am using MySQL as the database connection adapter for all my models. I have a downloads model and controller with an index function that renders either an HTML table or a CSV file depending on the type passed from the request. I also have a CSV media type to handle an array of data, which is working as expected (outputs array keys as headers then array values for each row of data).
I wish to do the same find query but then remove ID fields from the record set if a CSV file is going to be rendered. You'll notice that the download ID is being fetched even though it is not in the fields array, so simply changing the fields array based on the request type will not work.
I have tried the following in the index action of my downloads controller:
<?php
namespace app\controllers;
use app\models\Downloads;
class DownloadsController extends \lithium\action\Controller {
public function index() {
// Dynamic conditions
$conditions = array(...);
$downloads = Downloads::find('all', array(
'fields' => array('user_id', 'Surveys.name'),
'conditions' => $conditions,
'with' => 'Surveys',
'order' => array('created' => 'desc')
));
if ($this->request->params['type'] == 'csv') {
$downloads->each(function ($download) {
// THIS DOES NOT WORK
unset($download->id, $download->user_id);
// I HAVE TRIED THIS HERE AND THE ID FIELDS STILL EXIST
// var_dump($download->data());
// exit;
return $download;
});
return $this->render(array('csv' => $downloads->to('array')));
}
return compact('downloads');
}
}
?>
I thought there was an __unset() magic method on the entity object that would be called when you call the standard PHP unset() function on an entity's field.
It would be great if there was a $recordSet->removeField('field') function, but I can not find one.
Any help would be greatly appreciated.
Perhaps you should do $downloads = $downloads->to('array');, iterate the array with a for loop, remove those fields from each row, then return that array. If you have to do this same thing for a lot of actions, you could setup a custom Media handler that could alter the data without needing logic for it in your controller.
Take a look at this example in the Lithium Media class unit test.
You can also avoid having much logic for it in your controller at all through the use of a custom handler. This example also auto-generates a header row from the keys in your data.
In config/bootstrap/media.php:
Media::type('csv', 'application/csv', array(
'encode' => function($data, $handler, $response) {
$request = $handler['request'];
$privateKeys = null;
if ($request->privateKeys) {
$privateKeys = array_fill_keys($request->privateKeys, true);
}
// assuming your csv data is the first key in
// the template data and the first row keys names
// can be used as headers
$data = current($data);
$row = (array) current($data);
if ($privateKeys) {
$row = array_diff_key($row, $privateKeys);
}
$headers = array_keys($row);
ob_start();
$out = fopen('php://output', 'w');
fputcsv($out, $headers);
foreach ($data as $record) {
if (!is_array($record)) {
$record = (array) $record;
}
if ($privateKeys) {
$record = array_diff_key($record, $privateKeys);
}
fputcsv($out, $record);
}
fclose($out);
return ob_get_clean();
}
));
Your controller:
<?php
namespace app\controllers;
use app\models\Downloads;
class DownloadsController extends \lithium\action\Controller {
public function index() {
$this->request->privateKeys = array('id', 'user_id');
// Dynamic conditions
$conditions = array(...);
$downloads = Downloads::find('all', array(
'fields' => array('user_id', 'Surveys.name'),
'conditions' => $conditions,
'with' => 'Surveys',
'order' => array('created' => 'desc')
));
return compact('downloads');
}
}
?>
Why not then just dynamically set your $fields array?
public function index() {
$type = $this->request->params['type'];
//Exclude `user_id` if request type is CSV
$fields = $type == 'csv' ? array('Surveys.name') : array('user_id', 'Surveys.name');
$conditions = array(...);
$with = array('Surveys');
$order = array('created' => 'desc');
$downloads = Downloads::find('all', compact('conditions', 'fields', 'with', 'order'));
//Return different render type if CSV
return $type == 'csv' ? $this->render(array('csv' => $downloads->data())) : compact('downloads');
}
You can see in this example how I send the array for your CSV handler, otherwise it's the $downloads RecordSet object that goes to the view.