altf's avatar
Level 1

on selecting drop down table list uploaded file go to the respected table

hi , i have an issue : i am using a drop down table lists from a database table on selecting the particular value from drop down list the uploaded file go to the respected table: my controller code is:

class DropDownController extends Controller { public function create() { $categories = Category::lists('category','id');

        return view('/dashboard', compact('categories')); 
        } 

}

my view code is:

{{ csrf_field() }}
                <input type="hidden" name="_token" value="{{csrf_token()}}">
            
                
            
                <select class="form-control" name="lastname" id="lastname" data-parsley-required="true">
                <option value="">--- Select Table Name ---</option>
                  @foreach ($categories as $category) 
                  {
                    <option value="{{ $category }}">{{ $category }}</option>
                  }
                  @endforeach
                </select><br>   
                
                <input type="file" name="question"><br>
                <input type="submit" class="btn btn-primary" value="Import"></input>
                </form>

i am not able to make it happen .. please help me. your help will be appreciated...

0 likes
25 replies
tomopongrac's avatar

What exactly is error ... do you get select menu with category names ...

you don't use category id in your select menu ...

you can try to see do you get categories

 public function create() { 
    $categories = Category::lists('category','id');
    dd($categories);

        return view('/dashboard', compact('categories')); 
}
altf's avatar
Level 1

thanks for quick response.... yes! i am getting category values i n my drop down but i want my drop down values as a table name for storing excel sheet in data base my excel controller is:

public function postImport(){

$rules = array( 'file' => 'required|mimes:xls,csv,pdf,xlsx', );

    if(Input::hasfile('question')){
   Excel::load(Input::file('question'), function ($reader) {

   $reader->each(function($sheet) {    
     foreach ($sheet->toArray() as $row) {
        question::firstOrCreate($row);
     }
 });
});
}
else
{
    echo "please select the file";
}   
return redirect('dashboard');

}

tomopongrac's avatar

I think that this line is not good

question::firstOrCreate($row);

because in place of question you need category name (which in your form is called lastname)

Here is example where you can call model name from string

$model_name = 'App\Model\User';
$model_name::where('id', $id)->first();
altf's avatar
Level 1

thanks for your suggestion!!!! I tried but still i am not getting appropriate solutions analyse my query: i have a categories table from der i am fetching the drop down list of column category , which stores the different category table. here i am uploading an excel file and this file should store in one of selected table from category..

tomopongrac's avatar

What you get in postImport method from dropdown

public function postImport(Request $request){
    dd($request->input('lastname'));
}
altf's avatar
Level 1

thanks bro...!!! now i am able to get the value of table name, now using this value i want to fill my database...table please suggest me

tomopongrac's avatar

Is this work

public function postImport(Request $request)
{

    $rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

    if (Input::hasfile('question'))
    {
        
        $model_name = "App\\Model\\"  .  $request->input('lastname');
        Excel::load(Input::file('question'), function ($reader)
        {

            $reader->each(function ($sheet)
            {
                foreach ($sheet->toArray() as $row)
                {
                    $model_name::firstOrCreate($row);
                }
            });
        });
    }
    else
    {
        echo "please select the file";
    }

    return redirect('dashboard');
}
altf's avatar
Level 1

yes ... this is working by using this code i can upload the files in question table not in the selected value from drop down (drop down values are table names on selecting the specific value/table name i want to upload file in that particular table instead of questions table)... public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    

    Excel::load(Input::file('question'), function ($reader)
    {

        $reader->each(function ($sheet)
        {
            foreach ($sheet->toArray() as $row)
            {
                question::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');

}

i am close to the answer help me i got stuck not able to find y it is not working...

altf's avatar
Level 1

yes off course but i am getting an error if i used exact code what you sent then the error is like this

ErrorException in ExcelController.php line 51: Undefined variable: model_name

tomopongrac's avatar

did you add this line

$model_name = "App\\Model\\"  .  $request->input('lastname');
altf's avatar
Level 1

yess!!! then also it showing the same error

altf's avatar
Level 1

this is my controller:

public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    
    $model_name = "App\\Model\\"  .  $request->input('category');
    Excel::load(Input::file('question'), function ($reader)
    {

        $reader->each(function ($sheet)
        {
            foreach ($sheet->toArray() as $row)
            {
                $model_name::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');

}

this my view:

{{ csrf_field() }}
                <input type="hidden" name="_token" value="{{csrf_token()}}">
            
                
            
                <select class="form-control" name="category" id="category" data-parsley-required="true">
                <option value="">--- Select Table Name ---</option>
                  @foreach ($categories as $category) 
                  {
                    <option value="{{ $category }}">{{ $category }}</option>
                  }
                  @endforeach
                </select><br>   
                
                <input type="file" name="question"><br>
                <p
                <input type="submit" class="btn btn-primary" value="Import"></input>
                </form>
tomopongrac's avatar

Sorry, you must use use statement inside closure

public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    
    $model_name = "App\\Model\\"  .  $request->input('category');
    Excel::load(Input::file('question'), function ($reader) use ($model_name) // <- add here
    {

        $reader->each(function ($sheet)
        {
            foreach ($sheet->toArray() as $row)
            {
                $model_name::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');
}
altf's avatar
Level 1

again i am getting the same error: ErrorException in ExcelController.php line 51: Undefined variable: model_name

tomopongrac's avatar

Now i see that you must use another use

public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    
    $model_name = "App\\Model\\"  .  $request->input('category');
    Excel::load(Input::file('question'), function ($reader) use ($model_name) // <- add here
    {

        $reader->each(function ($sheet) use ($model_name) // <- add here
        {
            foreach ($sheet->toArray() as $row)
            {
                $model_name::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');
}
altf's avatar
Level 1

yes i used that thing then salso i am getting an error

tomopongrac's avatar

can tou try this

public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    
    
    Excel::load(Input::file('question'), function ($reader)
    {

        $reader->each(function ($sheet)
        {
        $model_name = "App\\Model\\"  .  $request->input('category');
            foreach ($sheet->toArray() as $row)
            {
                $model_name::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');
}
altf's avatar
Level 1

yes i tried i am getting an error called undefined variable request

tomopongrac's avatar

How about this

public function postImport(Request $request) {

$rules = array('file' => 'required|mimes:xls,csv,pdf,xlsx',);

if (Input::hasfile('question'))
{
    
    
    Excel::load(Input::file('question'), function ($reader)
    {

        $reader->each(function ($sheet)
        {
        $model_name = "App\\Model\\"  .  \Request::input('category');
            foreach ($sheet->toArray() as $row)
            {
                $model_name::firstOrCreate($row);
            }
        });
    });
}
else
{
    echo "please select the file";
}

return redirect('dashboard');
}
altf's avatar
Level 1

error : i am getting this error

Class 'App\Model\kg_10_eng_c1' not found

tomopongrac's avatar

Thats the problem in category list ... check the html source and put in value model name which you want

<select class="form-control" name="lastname" id="lastname" data-parsley-required="true">
                <option value="">--- Select Table Name ---</option>
                  @foreach ($categories as $category) 
                  {
                    <option value="{{ $category }}">{{ $category }}</option>
                  }
                  @endforeach
                </select>
altf's avatar
Level 1

yes i define the model in app folder as whatever respective value is selecting in category list then also i am getting the same class not found error.... from morning onward i made you so tired by questioning one after the other i really appreciate your patience thank you very much if you got any idea please share with me if not no problem now u shown the full path i ll just find.....

tomopongrac's avatar

No problem, i hope that your code works...

i don't understand if now everything works or is it some error ... i suspect on category list which must have names of models

Please or to participate in this conversation.