Khaitan

Khaitan

1548831453

Laravel get contents of Excel to insert in Database

I'm having a problem while uploading excel file and update data to my database using Laravel 5.6 and Laravel Excel(maatwebsite 3.1) i can't get the contents of each column to update the data in my database.

$rows return empty

Model:

namespace App;

use App\User;
use App\MyModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class DataImport implements ToCollection
{
/**
* @param array $rows
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function collection(Collection $rows)
{
dd($rows);
foreach ($rows as $row) {
$import = MyModel::where(‘user_id’,$row[1])->first();
$import->field1= $row[2];
$import->field2= $row[5];
$import->field3= $row[3];
$import->status = ‘PAID’;
$import->save();
}

}

}

Controller:

use Maatwebsite\Excel\Excel;

public function postImport(Request $request){
    if($request->hasFile('sample_file')){
        Excel::import(new DataImport ,request()->file('sample_file'));

        return redirect('/import');
    }

}

Excel File:

--------------------------------------------------------------------------------
| # | USER ID | FIELD ONE | FIELD TWO | FIELD THREE | FIELD FOUR | FIELD FIVE |

| 1 | 1 | 123-abc | abc-123 | 123123 | asdf | konjesh |


#php #excel #laravel

What is GEEK

Buddha Community

Tubo Man

1548832463

if($request->hasFile('sample_file')){
   $getPath = Request::file('sample_file');
   $data = Excel::load($getPath, function($reader){ })->get();
   if(!empty($data){
      Excel::import(new DataImport , $data);

      return redirect('/import');
   }
} 

Can you try this,

Lyly Sara

1548832526

Try storing the uploaded file to disk first:

$path = $request->file('sample_file')->store('sample_file');

Excel::import(new DataImport, $path);

How to Get Current URL in Laravel

In this small post we will see how to get current url in laravel, if you want to get current page url in laravel then we can use many method such type current(), full(), request(), url().

Here i will give you all example to get current page url in laravel, in this example i have used helper and function as well as so let’s start example of how to get current url id in laravel.

Read More : How to Get Current URL in Laravel

https://websolutionstuff.com/post/how-to-get-current-url-in-laravel


Read More : Laravel Signature Pad Example

https://websolutionstuff.com/post/laravel-signature-pad-example

#how to get current url in laravel #laravel get current url #get current page url in laravel #find current url in laravel #get full url in laravel #how to get current url id in laravel

Seamus  Quitzon

Seamus Quitzon

1595216280

Some of the most frequent how tos in Laravel

How to get relationship from relationship using With() in Laravel

Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following:

Account::with(['profiles','profiles.products'])->get();

How to create multiple where clauses in single where clause in Laravel

Sometimes you want to apply multiple where clauses in a single query like that:

$results = User::where('this', '=', 1)
    ->where('that', '=', 1)
    ->where('this_too', '=', 1)
    ->where('that_too', '=', 1)
    ->where('this_as_well', '=', 1)
    ->where('that_as_well', '=', 1)
    ->where('this_one_too', '=', 1)
    ->where('that_one_too', '=', 1)
    ->where('this_one_as_well', '=', 1)
    ->where('that_one_as_well', '=', 1)
    ->get();

This can be achieved using single where clause by following code:

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE]
])

How to get last inserted id in Laravel

The are situations, during bulk insertion or single row insertion you may want to retrieve last inserted id, can be achieved by following:

$post = Input::All();

    $data = new Company;
    $data->nombre = $post['name'];
    $data->direccion = $post['address'];
    $data->telefono = $post['phone'];
    $data->email = $post['email'];
    $data->giro = $post['type'];
    $data->fecha_registro = date("Y-m-d H:i:s");
    $data->fecha_modificacion = date("Y-m-d H:i:s");
   $data->save();
   //getting last inserted id
   $data->id;

How to add a custom attribute in Laravel during Model load

Sometimes you want to add a custom attribute in model during load, this can be achieved by following code:

class Book extends Eloquent {

    protected $table = 'books';

    public function toArray()
    {
        $array = parent::toArray();
        $array['upper'] = $this->upper;
        return $array;
    }

    public function getUpperAttribute()
    {
        return strtoupper($this->title);    
    }

}

#laravel #php #how to add a custom attribute in laravel during model load #how to create multiple where clauses in single where clause in laravel #how to get a specific column using with() in laravel #how to get last inserted id in laravel #how to get relationship from relationship laravel #laravelfaq

Gerhard  Brink

Gerhard Brink

1622622360

Data Validation in Excel

Data Validation in Excel

In this tutorial, let’s discuss what data validation is and how it can be implemented in MS-Excel. Let’s start!!!

What Is Data Validation in Excel?

Data Validation is one of the features in MS-Excel which helps in maintaining the consistency of the data in the spreadsheet. It controls the type of data that can enter in the data validated cells.

Data Validation in MS Excel

Now, let’s have a look at how data validation works and how to implement it in the worksheet:

To apply data validation for the cells, then follow the steps.

1: Choose to which all cells the validation of data should work.

2: Click on the DATA tab.

3: Go to the Data Validation option.

4: Choose the drop down option in it and click on the Data Validation.

data validation in Excel

Once you click on the data validation menu from the ribbon, a box appears with the list of data validation criteria, Input message and error message.

Let’s first understand, what is an input message and error message?

Once, the user clicks the cell, the input message appears in a small box near the cell.

If the user violates the condition of that particular cell, then the error message pops up in a box in the spreadsheet.

The advantage of both the messages is that the input and as well as the error message guide the user about how to fill the cells. Both the messages are customizable also.

Let us have a look at how to set it up and how it works with a sample

#ms excel tutorials #circle invalid data in excel #clear validation circles in excel #custom data validation in excel #data validation in excel #limitation in data validation in excel #setting up error message in excel #setting up input message in excel #troubleshooting formulas in excel #validate data in excel

Laravel 8 Database Seeder Example

Today we will see Laravel 8 Database Seeder Example, as we all know laravel framework provide many functionalities to user to reduce developer’s time for developing website, here we will see How to create database seeder in Laravel 8.

Many times you have requirement to add some default records or entry to login or add form details etc. there are no problem to add manual data in database one or two times but it is very challenging task to add data manually each and every time.

Laravel 8 Database Seeder Example

https://websolutionstuff.com/post/laravel-8-database-seeder-example


Read Also : Read Also : Laravel 8 CRUD Operation Example

https://websolutionstuff.com/post/laravel-8-crud-operation-example


Read Also : Read Also : Create Dummy Data Using Laravel Tinker

https://websolutionstuff.com/post/create-dummy-data-using-laravel-tinker

#laravel 8 database seeder example #laravel #database #seeder #database seeders in laravel 8 #laravel 8 seeder example

I am Developer

1597563325

Laravel 7/6 Image Upload Example Tutorial

Laravel image upload example tutorial. Here, i will show you how to upload image in laravel 7/6 with preview and validation.

Before store image into db and folder, you can validate uploaded image by using laravel validation rules. as well as you can show preview of uploaded image in laravel.

Image Upload In Laravel 7/6 with Validation

Image upload in laravel 7/6 with preview and validation. And storage image into folder and MySQL database by using the below steps:

Install Laravel Fresh App
Setup Database Details
Generate Image Migration & Model
Create Image Upload Route
Create Image Controller
Create Image Upload and Preview Blade View
Start Development Server

https://www.tutsmake.com/laravel-7-6-image-upload-with-preview-validation-tutorial/

#laravel 7 image upload example #laravel upload image to database #how to insert image into database in laravel #laravel upload image to storage #laravel image upload tutorial #image upload in laravel 7/6