Home > OS >  How to insert record in 2 tables using single form (Laravel)?
How to insert record in 2 tables using single form (Laravel)?

Time:10-06

CONTROLLER

public function store_resto(Request $request){
        // dd($request->all());
        $restaurant = new Restaurant();
        $restaurant->name = $request->input('name');
        $restaurant->email = $request->input('email');
        $restaurant->address = $request->input('address');
        $restaurant->save();
        $image = $request->hasfile('image');
            $photo = rand(1,9999).'.'.$image;
            $path = public_path().'/files/';
            $image->move($path, $photo);
                 RestoImage::create([
                     'image'=>$image,
                     'resto_id'=>$restaurant->id,
                      ]);

        $request->session()->flash('status', 'Restaurant added successfully');  
        return redirect('list');
    }

VIEW FILE

<form method="post" action="{{route('store_resto')}}" enctype="multipart/form-data">
@csrf
<div class="form-group">
    <label>Resto Name</label>
    <input type="name" name="name" class="form-control">
</div>
<div class="form-group">
    <label>Email</label>
    <input type="email" name="email" class="form-control">
  </div>
  <div class="form-group">
    <label>Address</label>
    <input type="text" name="address" class="form-control">
  </div>
  <div class="form-group">
    <label>Image</label>
    <input type="file" name="image" class="form-control">
  </div><br>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

RestoImage Model

class RestoImage extends Model
{
    
    use HasFactory;
    protected $fillable = ['image','resto_id'];
    public function restaurants(){
        $this->belongsTo(Restaurant::class, 'resto_id');
    }

}

Restaurant Model

class Restaurant extends Model
{
    use HasFactory;
    public $timestamps = false;

    public function menus(){
        $this->hasMany(Menu::class);
    }
    public function restoimage(){
        $this->hasOne(RestoImage::class, 'resto_id');
    }
}

Each restaurant will have 1 image. When an admin submits the form, 1 record should be inserted in both tables i.e. restaurants and resto_images. I tried this way but when I submit the form, It shows error "Call to a member function move() on bool". Please correct me if I am doing wrong. Thanks in advance.

CodePudding user response:

Here i Worked on your code to explain how these things works.This is an example can help you. Not for two you can add so many tables from one function of controller. Approve my answer if you find solution or reason for getting error.

You have error because code doesn't find your image format or mine:type(png, jpeg)

$photo = rand(1,9999).'.'.$image;

Solution- you have to get image format or extention by this code

$extention          = $emp_image_file->getClientOriginalExtension();

Your solution should be like this

  $path1 = 'assets/img/emp/'; 
              $destinationPath1    = $path1;
              $photo_file         = $request->file('image');
              $photo='';
              if($photo_file){
                $file_size = $photo_file->getSize();
                    
                    $image_name         = $photo_file->getClientOriginalName();

                    $extention          = $photo_file->getClientOriginalExtension();

                    $photo = value(function() use ($photo_file){
                    $filename = time().'.'. $photo_file->getClientOriginalExtension();
                    return strtolower($filename);
                    });
                    $photo_file->move($destinationPath1, $photo);

                  
                }

Put js in your view file

<script type="text/javascript">


  function readURL(input) {
  if (input.image && input.image[0]) {
    var reader = new FileReader();
    
    reader.onload = function(e) {
      $('#imagePreview').attr('src', e.target.result);
    }
    
    reader.readAsDataURL(input.files[0]);
  }
}
</script>

This is you input

<input type="file" class="form-control" name="image"  >

I Also Worked For Other Visitors See Once


public function store_resto(Request $request){

       <!-- validation code begins -->
        $this->validate($request, [
            'name'=>'required|max:120',
            'email'=>'required|email|unique:users',
            
        ]);
        <!-- validation code ends -->

 $data = $request->all(); 

        $table1 =  Required_Model1::create([
       
        'name'     =>$data['emp_name'],
        'email'    =>$data['email'],
             
        ]);

        $table2 =  Required_Model2::create([
        

        'name'                 => $data['emp_name'],
        'code'                 => $data['emp_code'],
        'status'               => $data['emp_status'],
        'email'               => $data['email'],
        'gender'               => $data['gender'],
        

        'table1_id'              => $table1->id,
        ]);

        $table3 =  Required_Model3::create([
      
        'role' => $data['role'],
        'table1_id' => $table1->id,
        'table2_id' => $table2->id,


          if(isset($table1,  $table2, $table3)) {
            $request->session()->flash('status', 'Restaurant added successfully');
              return redirect()->route('employee-manager');
              }else{
                  return redirect()->back();
              }

        
    }

Comment or delete this part of code if you doesn't want to validate or mandatory.

$this->validate($request, [
        'name'=>'required|max:120',
        'email'=>'required,
        
    ]);

Above code explains

  1. column name must be filled with 120 characters or not be blank.
  2. column email must be filled. if these two doesn't satisfy it will redirect back.

This below code If validation is set like above code this will check and work as defined. If validation is set they check two fields name and email, if they filled or not blank it will proceed further. If validation is set fields are not filled or blank they redirect back. If validation is not set it will proceed further.

if(isset($table1,  $table2, $table3)) {
        $request->session()->flash('status', 'Restaurant added successfully');
          return redirect()->route('employee-manager');
          }else{
              return redirect()->back();
          }

Change these two lines

<input type="name" name="name" class="form-control" required="true" />
 <input type="email" name="email" class="form-control" required="true" />

Model 1 should be like this

class Required_Model1 extends Model
   {
    
    
    protected $fillable = ['name','email'];
   

  }

Model 2 should be like this

class Required_Model2 extends Model
   {
    
    
    protected $fillable = ['name','code', 'status', 'email', 'gender', 'table1_id'];
   

  }

Model 3 should be like this

class Required_Model3 extends Model
   {
    
   
    protected $fillable = ['role','table1_id', 'table2_id'];
   

  }

Let's talk on your error as you posted You have face error because you want to move your image name in form of boolean. Here is gave you an standard code you can use it

$path1 = 'assets/img/emp/'; 
              $destinationPath1    = $path1;
              $emp_image_file         = $request->file('employee_images');
              $emp_image='';
              if($emp_image_file){
                $file_size = $emp_image_file->getSize();
                    
                    $image_name         = $emp_image_file->getClientOriginalName();
                    $extention          = $emp_image_file->getClientOriginalExtension();
                    $emp_image = value(function() use ($emp_image_file){
                    $filename = time().'.'. $emp_image_file->getClientOriginalExtension();
                    return strtolower($filename);
                    });
                    $emp_image_file->move($destinationPath1, $emp_image);

                  
                }

Put this in which table you wanted to save

 'photo'                => $emp_image,

Add this in your view make sure you edit like your requirement

<script type="text/javascript">


  function readURL(input) {
  if (input.employee_images && input.employee_images[0]) {
    var reader = new FileReader();
    
    reader.onload = function(e) {
      $('#employee_imagesPreview').attr('src', e.target.result);
    }
    
    reader.readAsDataURL(input.files[0]);
  }
}
</script>

This is input

<input type="file" class="form-control" name="employee_images"  >

CodePudding user response:

$image = $request->hasfile('image');

This method is a boolean method. It will return true/false. Instead use

$request->file('image');

CodePudding user response:

So first, here:

$image = $request->hasfile('image');

You are setting $image to a boolean by checking if it has that file and then later you want to run move on a that boolean which is not possible. Rather do:

if($request->hasfile('image'))
{
   $image = $request->file('image');
   $image->move($path, $photo);
}
  • Related