1
votes

I am trying to upload users photo during registration, but I am getting this error, I searched online and founded possible solutions did not work for me. I am using sentinel package for advanced authentication. Please anyone help me . Here is my controller-

 public function postRegister(Request $request){
    $request->validate([
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'user_name' => 'unique:users|max:255',
        'email' => 'required|unique:users|email|max:255',
        'password' => 'required|min:6|confirmed',
        'phone' => 'required|numeric',
    ]);
    //upload image
    if ($file = $request->file('photo')) {
        $fileName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension() ?: 'png';
        $folderName = '/uploads/users/';
        $destinationPath = public_path() . $folderName;
        $safeName = str_random(10) . '.' . $extension;
        $file->move($destinationPath, $safeName);
        $request['photo'] = $safeName;
    }

    // $user = Sentinel::registerAndActivate($request->all());
    $user = Sentinel::registerAndActivate($request->all());
    // dd($user);
    //$activation = Activation::create($user);

    $role = Sentinel::findRoleBySlug('member');

    $role->users()->attach($user);
    //$this->sendEmail($user, $activation->code);
    //return redirect('/login');
    return ('You have seccsessfully registered. Now login to start');
}

And form input-(with enctype="multipart/form-data" top of the form)

 <div class="row">
    <div class="col-md-6">
      <div class="sponsor-row">
        <label for="sponsor_input"><i class="fa fa-tags"></i></label>
        <input type="text" name="sponsor" id="sponsor_input" class="sponsor-input" placeholder="Sponsor"></input>
      </div>
    </div>
    <div class="col-md-6">
      <div class="photo-row">
        <label for="photo_input"></label>
        <input type="file" name="photo" id="photo" class="photo-input"></input>
      </div>
    </div>
  </div>
</div>

My User Model is:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable { use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name', 'last_name', 'user_name','email', 'password', 'phone', 'location', 'sponsor', 'photo',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

My Migration Code is:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('user_name');
        $table->string('email');
        $table->string('password');
        $table->string('phone');
        $table->string('location');
        $table->string('sponsor');
        $table->binary('photo')->nullable();
        $table->text('permissions')->nullable();
        $table->timestamp('last_login')->nullable();
        $table->timestamps();

        $table->engine = 'InnoDB';
        $table->unique('email');
    });
4
Database table column photo need a default value like nullShanu k k
just set a default value for your photo column in your table typically nullRAUSHAN KUMAR
Post Your Sentinel Model & Migration Code..Zedex7
Here is my User Model- protected $fillable = [ 'first_name', 'last_name', 'user_name','email', 'password', 'phone', 'location', 'sponsor', 'photo', ];Rashed Hasan
And here is migration - Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('first_name')->nullable(); $table->string('last_name')->nullable(); $table->string('user_name'); $table->string('email'); $table->string('password'); $table->string('phone'); $table->string('location'); $table->string('sponsor'); $table->binary('photo')->nullable();Rashed Hasan

4 Answers

3
votes

if you are using latest version of laravel go to config/databse.php and in connection there is mysql array:-

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false, 
        'engine' => null,
    ],

By default strict is true and set to make it false.(2nd last key in mysql array) Hope it helps!

2
votes

General error: 1364 Field 'photo' doesn't have a default value

This error means that there is a column photo in the associated table that doesn't have a default value and you are not including that column in your insert query.

To solve it, either make that column NULLABLE or pass some value to insert in that column.

0
votes

Your problem is here

$request['photo'] = $safeName

You can not directly put values like associative array in Request Class Object And that's why there is no value for photo, and it is giving your error for SQL Insert.

You should do something like this

$request->request->add(['photo' => $safeName]);

or Better way

$user = Sentinel::registerAndActivate(array_merge($request->all(),['photo' => $safeName]));
0
votes

Please check you mysql version, if you have greater than 5.6.39 then. please go to variables and find sql mode and remove STRICT_TRANS_TABLESenter image description here