2

This is the first time that I tried to work with the Maatwebsite/Laravel-Excel package using Laravel 8.

I'm trying to import data from excel file to database table.

My table:

enter image description here

But I have some difficulties or questions:

  1. In my database table I have a foreign keys, that means when I will import data I should store the foreign key instead of the value, for example: I have id_type_facturation column in my datatable so I should store the id and not the value like "Conrat".

  2. I have another constraint that from my excel file, the column date_of_birth, I should calculate the age, and if the age is less than 18 I should store on anothe table the information of the parents and if the age is grather than 18 It shouldn't store the information in the table parents.

  3. I have also an image column, and I should insert it in the database table.

I did all the process, but I get data from a form by using the following code:

public function store(Request $request)

{
       $representants = new Representant();
       $castings = new Casting();


       $id_filiale= Auth::user()->id_filiale;
  
       $datee = $request['date_naissance'];

       $time = strtotime($datee);

       $date = date('Y-m-d',$time);
     
      

     $age = Carbon::parse($date)->diff(Carbon::now())->y;

    
       $rules = array(

         'nom' => 'required',
         'prenom' => 'required',
         'cine' => 'required|unique:castings',
         'date_naissance' => 'required',
         'lieu_naissance' => 'required',
         'id_type_facturation' => 'required',
         'artiste' => 'required',
         'fonction' => 'required',
         'id_type_casting' => 'required',
         'tel1' => 'required',
         'tel2' => 'required',
         'email' => 'required',
         'qualification' => 'required',
         'adresse' => 'required',
         'ville' => 'required',
         'pays' => 'required'
      
            
             );


       $rules_message = array(

         'nom.required' => 'Le nom est requis',
         'prenom.required' => 'Le prénom est requis',
         'cine.required' => 'Le CINE est requis',
         'cine.unique' => 'Le CINE a déjà été pris',
         'date_naissance.required' => 'La date de naissance est requise',
         'lieu_naissance.required' => 'Le lieu de naissance est requis',
         'id_type_facturation.required' => 'Le type de facturation est requis',
         'artiste.required' => 'Est ce que c\'est un artiste ? ',
         'fonction.required' => 'La fonction est requise',
         'id_type_casting.required' => 'Le type de facturation est requis',
         'tel1.required' => 'Le numéro de téléphone 1 est requis',
         'tel2.required' => 'Le numéro de téléphone 2 est requis',
         'email.required' => 'L\'adresse email est requise',
         'qualification.required' => 'La qualification est requise',
         'adresse.required' => 'L\'adresse est requise',
         'ville.required' => 'La ville est requise',
         'pays.required' => 'La pays est requis',
      
            
             );


          $error = Validator::make($request->all(), $rules,$rules_message);

                    if($error->fails())
                    {
                        return response()->json(['errors' => $error->errors()->all()]);
                    }

      if ($age < 18) {

            

       $image = $request->file('photo');

       $new_name = rand() . '.' . $image->getClientOriginalExtension();

       $image->move(public_path('castingimages'), $new_name);

             $representants->nom_prenom = $request['nom_prenom'];
             $representants->cine = $request['cine_representant'];
             $representants->lien_casting = $request['lien_casting'];
             $representants->save();

             
             $castings->nom = $request['nom'];
             $castings->prenom = $request['prenom'];
             $castings->cine = $request['cine'];
             $castings->date_naissance = $date  ;
             $castings->lieu_naissance = $request['lieu_naissance'];
             $castings->mineur = 1;
             $castings->id_representant = $representants->id;

             $castings->id_type_facturation = $request['id_type_facturation'];
             $castings->artiste = $request['artiste'];
             $castings->fonction = $request['fonction'];
             $castings->id_type_casting = $request['id_type_casting'];
             $castings->tel1 = $request['tel1'];
             $castings->tel2 = $request['tel2'];
             $castings->email = $request['email'];
             $castings->photo = $new_name;

             $castings->qualification = $request['qualification'];
             $castings->adresse = $request['adresse'];
             $castings->ville = $request['ville'];
             $castings->pays =  $request['pays'];

             $castings->id_filiale =  $id_filiale;

             $castings->save();

            return response()->json(['success' => 'Casting/Influenceur ajouté avec succès.']);  
 
  }else

  {
     
            $image = $request->file('photo');

            $new_name = rand() . '.' . $image->getClientOriginalExtension();

            $image->move(public_path('castingimages'), $new_name);

           

             $castings->nom = $request['nom'];
             $castings->prenom = $request['prenom'];
             $castings->cine = $request['cine'];
             $castings->date_naissance = $date  ;
             $castings->lieu_naissance = $request['lieu_naissance'];
             $castings->mineur = 0;
           

             $castings->id_type_facturation = $request['id_type_facturation'];
             $castings->artiste = $request['artiste'];
             $castings->fonction = $request['fonction'];
             $castings->id_type_casting = $request['id_type_casting'];
             $castings->tel1 = $request['tel1'];
             $castings->tel2 = $request['tel2'];
             $castings->email = $request['email'];
             $castings->photo = $new_name;

             $castings->qualification = $request['qualification'];
             $castings->adresse = $request['adresse'];
             $castings->ville = $request['ville'];
             $castings->pays =  $request['pays'];
             $castings->id_filiale =  $id_filiale;


             $castings->save();

                 return response()->json(['success' => 'Casting/Influenceur ajouté avec succès.']); 
  }
    
    }

How can I do the same thing but getting the data from a excel file? If you have any ideas about that, or if you know if I can do thoes things with this package let me know.

2
  • To answer your first question, there are multiple possible solutions depending on what works best for you: a) The Excel column value is unique and can be queried using an indexed database column b) Let Excel store the ID in a hidden (using VLOOKUP or something the like) Commented Aug 3, 2021 at 8:11
  • @shaedrich , thank you for your comment , if you can explain more how can I do that Commented Aug 3, 2021 at 10:16

1 Answer 1

0

If you only have the value but not the ID but the value is unique in your database table, you can do this:

$model = new YourModel;
$model->some_id = OtherModel::where('unique_column', $request->input('some_value'))->sole()->id;
$model->save();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that , $casting->id_type_facturation = Type_Facturation::where('type_facturation',$row['type_facturation'] )->sole()->id ; But I get NULL value in database
Hard to say, why this happens. You know your database schema and contents better than I do. Maybe TypeFacturation::dd()->where... helps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.