Kistlak's avatar

How To Solve This Image Insert

I'm creating a web site. And I have created a registration page. When I insert data, all the data successfully inserted into the database. But, images didn't insert into the database and not moved to the folder. How can I Fix this ??

Here is the AdminPanel.blade.php

<div class="panel-body">
 
  @if(session()->has('Msg'))
<h4 class="alert alert-success"> {{ session()->get('Msg') }} </h4>
@endif

@if(session()->has('OnlyImg'))
<h4 class="alert alert-success"> {{ session()->get('OnlyImg') }} </h4>
@endif  
    
    <form class="form-horizontal" method="POST" action="{{ route('adinsert') }}">

    {{ csrf_field() }}

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{ old('username') }}" placeholder="Enter Your Username" required>
  </div>
    
    <div class="form-group">
    <label>Email : *</label>
    <input type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Enter Your Username" required>
  </div>
  
  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{ old('password') }}" placeholder="Enter Your Password" required>
  </div>
  
  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>
  
  @section('btnName',"Insert") 
  <input type="submit" class="btn btn-primary" onclick="myFunction()" name="submit" value="@yield('btnName')">
                    
</form>
</div>

Here is the AdminPanelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Validator;
use Illuminate\Support\Facades\Input;

class AdminPanelController extends Controller
{
    public function index()
    {
      $data = User::all();
      //$data = login::orderBy('created_at', 'desc')->get();
      return view('AdminPanel', ['data' => $data]);
    }
    
public function adinsert(Request $request)
    {

        $username = $request->input('username');
        $email = $request->input('email');
        $password = $request->input('password');
        //$passen = bcrypt($password);
        
        $user = new User();
        $user->username = $username;
        $user->email = $email;
        $user->password = $password;
        
        $this->validate($request, [
            'email' => 'required'
        ]);
        
        if(Input::hasFile('file_img')){
            
            $file = Input::file('file_img');
            
            $rules = array(
                'file_img' => 'required|max:10000|mimes:doc,docx,jpeg,png,jpg'
            );
            
            $validator = Validator::make(Input::all(), $rules);
            
            if ($validator->fails()) {
                
                // redirect our user back with error messages
                
                // send back to the page with the input data and errors
                
                $request->session()->flash('OnlyImg', 'You Can Only Upload Images !!');
                return redirect('AdminPanel');
                
            }
            
            else if ($validator->passes()) {
                
                $fileimg = $file->getClientOriginalName();
                $destinationPath = 'img';
                $filemove = $file->move($destinationPath, $fileimg);
                
                $user->fileimg = $fileimg;
                $user->filemove = $filemove;
                
                $user->save();
                
                $request->session()->flash('Msg', 'Successfully Inserted !!');
                
                return redirect('AdminPanel');
                
            }
        }
        
        else
        {
            
            $user->save();
            
            $request->session()->flash('Msg', 'Successfully Inserted !!');
            
            return redirect('AdminPanel');
        }
  
    }    
    
}

Here is Route.

Route::post('adinsert',[
'uses'=> 'AdminPanelController@adinsert',
'as' => 'adinsert'
]);
0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

You know you can always dd($request) at the top of the controller and see what you are getting ?

You need to tell the form to use multipart encoding

<form class="form-horizontal" method="POST" action="{{ route('adinsert') }}" enctype="multipart/form-data" >

Otherwise file data is not recieved at the server

1 like
Cronix's avatar

Not to do with your question, but It's also really best to do your validation at the very top of your method (if doing it manually like you are), because if it fails there is no use continuing.

1 like

Please or to participate in this conversation.