mehmetanbaki's avatar

pathinfo()

Hey Laravelers

I have this issue:

ErrorException
pathinfo() expects parameter 1 to be string, object given 

this is the controller

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Session;
use Maatwebsite\Excel\Facades\Excel;
use File;

class PeopleController extends Controller
{
    public function index()
    {
    return view('add-person');
    }

    public function import(Request $request){
        //validate the xls file
        $this->validate($request, array(
        'file'      => 'required'
        ));

        if($request->hasFile('file')){
        $extension = File::extension($request->file->getClientOriginalName());

            if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

                        $path = $request->file->getRealPath();
                        $data = Excel::import($path, function($reader) {
                        })->get();
                        if(!empty($data) && $data->count()){

                        foreach ($data as $key => $value) {
                        $insert[] = [
                        'name' => $value->name,
                        'address' => $value->address,
                        'phone' => $value->phone,
                        ];
                        }

                if(!empty($insert)){

                    $insertData = DB::table('people')->insert($insert);
                    if ($insertData) {
                    Session::flash('success', 'Your Data has successfully imported');
                    }else {
                    Session::flash('error', 'Error inserting the data..');
                    return back();
                    }
                }
            }
                return back();
            }else {
                Session::flash('error', 'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
                return back();
            }
        }
    }

}

this is model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class People extends Model
{
    use Searchable;
    protected $guarded = [];
}

this is view:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{{ asset('css/app.css') }}">

    <title>Import</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

    <!-- Styles -->
    <style>
    html, body {
        background-color: #fff;
        color: #636b6f;
        font-family: 'Raleway', sans-serif;
        font-weight: 100;
        height: 100vh;
        margin: 0;
        padding: 5%
    }
</style>
</head>
<body>
    <div class="container">


        <h2 class="text-center">
            Laravel Excel/CSV Import
        </h2>

        @if ( Session::has('success') )
        <div class="alert alert-success alert-dismissible" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
        </button>
        <strong>{{ Session::get('success') }}</strong>
    </div>
    @endif

    @if ( Session::has('error') )
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
        </button>
        <strong>{{ Session::get('error') }}</strong>
    </div>
    @endif

    @if (count($errors) > 0)
    <div class="alert alert-danger">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <div>
        @foreach ($errors->all() as $error)
        <p>{{ $error }}</p>
        @endforeach
    </div>
</div>
@endif



<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}
    Choose your xls/csv File : <input type="file" name="file" class="form-control">

    <input type="submit" class="btn btn-primary btn-lg" style="margin-top: 3%">
</form>

</div>
</body>
</html>

I'm using laravel 6

0 likes
7 replies
ahmeddabak's avatar

Somewhere you are calling pathinfo and passing it an object, check your code, probably it will be in the web.php file

ahmeddabak's avatar

It is only a guess, it can be any where. try searching for pathinfo in the whole project, you should be able to find it in one of the files

Sinnbeck's avatar

Look at the error again. This time notice where it tells you the error occurred (below the error message)

flusha244's avatar

Is there a new update to this one? Same problem here tho. from " $data = Excel::load($path, function($reader) {})->get();"

to " $data = Excel::import($path, function($reader) {})->get();"

im trying to make this controller work on v6 in laravel. but when i change load to import i get the same error "ErrorException pathinfo() expects parameter 1 to be string, object given "...

can somebody tell me how to solve this one

Please or to participate in this conversation.