I am trying to var_dump data from an excel sheet but it is taking longer than normal. In fact, the server times. I am using maatwebsite/excel to import this excel file. How do I resolve this.
This is what I am doing:
$path = Input::file('file')->getRealPath();
$data = Excel::load($path, function ($reader) {})->get();
dd($data);
I am using Laravel 5.5 and Maatwebsite 2.1.
$path = Input::file('file_import')->getRealPath();
$inserts = [];
Excel::load($path,function($reader) use (&$inserts)
Excel::load($path,function($reader) use (&$inserts)
{
foreach ($reader->toArray() as $rows){
foreach($rows as $row){
$inserts[] = ['email' => $row['email'], 'username' => $row
['username'], 'password' => $row['password'], 'first_name' => $row['first_name'],'middle_name' => $row['middle_name], 'last_name' => $row['last_name]];
}
}
Try this example bro just change the field that you need.
Related
I'm making an edit page for users using Vue + Laravel rest-api and I'm having a hard time linking an image to the image field of the users table.
The first issue is that it's not recognizing the image as a file despite adding enctype="multipart/form-data" to the form. I looked up some solutions, but haven't found something useful.
The console.log(this.form.newimage) results in newimage: "data:image/jpeg;base64,/9j/4AAQS... so I pressume the format of it is good.
Backend UserController:
public function update(Request $request, $id) {
$validatedData = $request->validate([
'name' => 'nullable|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$id,
'phone' => 'nullable|numeric|digits_between:5,15',
'address' => 'nullable|string|max:255',
'postal_code' => 'nullable|numeric|digits_between:3,200',
'country_id' => 'nullable|string|max:255',
'image' => 'nullable',
'newimage' => 'nullable|file',
]);
$data = array();
(...)
if($request->hasFile('newimage')) {
$destination_path = 'public/images';
$avatar = $request->file('newimage');
$imagename = $avatar->getClientOriginalName();
$path = $request->file('newimage')->storeAs($destination_path, $imagename);
$data['image'] = $filename;
}
User::where('id', $id)->update($data);
}
use store file fun
if($request->image)
{
$request->file('image')->store('image','public');
}
I'm trying to save data that is in an excel file to my database and I would like to check if any of those has a comma or spaces in it and if it does I would like to remove it.
I'm using phpoffice/phpspreadsheet and laravel and if possible I would like to use phpoffice/phpspreadsheet if it can do it.
Here is my code
$xl = IOFactory::load($path);
$sheet = $xl->getSheetByName('Products');
$c = $sheet->getHighestDataColumn();
$r = $sheet->getHighestDataRow();
$data = $sheet->rangeToArray("A1:{$c}{$r}", null, false);
$headers = array_shift($data);
$projects = [];
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => $entry['Name'],
'description' => $entry['description'],
'price' => $entry['price'],
];
}
return $projects;
try to create a function :
function cleanData($data) {
$data= str_replace( ',', '', $data);
$data= preg_replace('/\s+/', '', $data);
return $data;
}
and use it:
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => cleanData($entry['Name']),
'description' => cleanData($entry['description'],
'price' => cleanData($entry['price']),
];
}
I'm trying to upload an image using the API. Even though, after running the first test, the database just stored the directory and not the file-name, worst part, it stored all directories that come before the one that is needed to store the image.
The code I'm using for the store is the following one:
public function store(Request $request)
{
$image = $request->file('image');
if($image == null){
$imagesDir = 'subjectImgs/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$fileDir = $images[array_rand($images)];
$path = $fileDir;
} else {
$path = $image->storeAs('uploads/images/store/', $image->getClientOriginalName(), 'public');
}
$subject = new Homework([
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$subject->save();
return response()->json([
"code" => 200,
"message" => "Homework added successfully"
]);
}
I'm uploading from a mobile device, I believe this info is also important (?) Same issue when in the simulator.
You are saving $path into $subject->image that is equal to public_path() . '/uploads/images/store/';. You should add $file->getClientOriginalName() if you want the filename :
$subject = new Homework([
...
'image' => $path . $file->getClientOriginalName(),
...
]);
You are settings 'image' => $path, and before that $path = public_path() . '/uploads/images/store/'; which yields the expected results.
Now if you want to only save from a form input, you can use the following code:
$image = $request->file('image');
$path = $image->storeAs('uploads/images/store/', $image->getClientOriginalName(), 'public');
Where 'public' is the default public filesystem as described in the doc here: https://laravel.com/docs/7.x/filesystem#the-public-disk
I’m working on laravel excel import. The data can be loaded using
$data = Excel::load($path)->get(); command. But, when i try to loop through $data object and put it in $insert[], some fields remaining empty.
my import function look like
public function import(request $request) {
$path = $request->file('select_file')->getRealPath();
$data = Excel::load($path)->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'Item_name' => $value->Item_name,
'Manufacturer' => $value->Manufacturer,
'Serial_no' => $value->Serial_no,
'Model_no' => $value->Model_no,
'status' => $value->status,
'Price' => $value->Price,
'photo' => $value->photo,
'user_id' => $value->user_id,
'deletedBy' => $value->deletedBy,
'created_at' => $value->created_at,
'updated_at' => $value->updated_at,
];
}
if(!empty($insert)){
$insertData = DB::table('inventories')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return redirect()->back();
}
}
}
return redirect()->back();
}
when I dd($data); the result looks as
and the result of dd($insert); looks as
if any friend can help me that why some fields like Item_name, Manufacturer, and Serial_no remain null, would be appreciated.
solved!
all value->item_names must write in lowercase; I thought that it should match with the database table column names.
I'm trying to insert data to Database, but got the error "Creating default object from empty value" and still don't understand, this error come from?
in My Controller
public function store(Request $request)
{
$request->validate([
'amount' => 'required',
'method' => 'required',
]);
$payrolls = new Payroll();
$payrolls->employee_id = auth()->user()->id;
$payrolls->account_id = auth()->user()->id;
$payrolls->employee_name = $request->get('employee_name');
$payrolls->description = $request->get('description');
$payrolls->account = $request->get('account');
$payrolls->amount = $request->get('amount');
$payrolls->method = $request->get('method');
$payrolls->save();
return response()->json(['created' => true]);
}
Any help? Thanks......
As #arm stated, you're having a typo in your code, which leads to the error you're getting.
The code below should make it working.
I would also suggest you to use $request->input('****'); instead of dynamic variables like you're doing $request->account;
public function store(Request $request)
{
$request->validate([
'amount' => 'required',
'method' => 'required',
]);
$payrolls = new Payroll();
$payrolls->employee_id = auth()->user()->id;
$payrolls->account_id = auth()->user()->id;
$payrolls->employee_name = $request->input('employee_name');
$payrolls->description = $request->input('description');
$payrolls->account = $request->input('account');
$payrolls->amount = $request->input('amount');
$payrolls->method = $request->input('method');
$payrolls->save();
return response()->json(['created' => true]);
}
You could also have a look at the Payroll::create(), which makes it easier to create a entry in the DB, instead of making a new Payroll(), and saving at the end. https://laravel.com/docs/5.7/eloquent#mass-assignment