0
I am creating a system with Laravel, and using only his api.
API code:
Route::get('/teste', function (Request $request) {
$nick = '@mesa0'. rand(11, 50);
$number = '0'. rand(11, 50);
$password ='123456';
$password_confirmation ='123456';
$data = ['nick'=>$nick,'number'=>$number,'password'=>$password,'password_confirmation'=>$password_confirmation];
$validate = Validator::make($data, [
'nick' => 'required|string|max:255|unique:desks',
'number' => 'required|string|max:255|unique:desks',
'password' => 'required|string|min:6|confirmed',
]);
if ($validate->fails()) {
return ['status' => false, "validacao" => true, "erros" => $validate->errors()];
}
//return $data;
$desk = Desk::create([
'nick' => $nick,
'number' => $number,
'password' => bcrypt($password)
]);
$desk->token = $desk->createToken($desk->nick)->accessToken;
return ['status' => true, "mesa" => $desk];
// return Desk::all();
});
File Desk, in the App directory/
this is the file that determines the table, I am using sqlite
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class Desk extends Model
{
use HasApiTokens,
Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nick', 'number',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'remember_token','password'
];
}
and this is migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDesksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('desks', function (Blueprint $table) {
$table->increments('id');
$table->string('nick',255)->unique();
$table->string('number',255)->unique();
$table->string('password',255);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('desks');
}
}
the mistake is this:
"SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: desks.password (SQL: insert into "desks" ("nick", "number", "updated_at", "created_at") values (@mesa014, 029, 2019-02-19 01:22:03, 2019-02-19 01:22:03))"
when I do not define 'password' as protected attribute "protected $Hidden", it works normally, but when I protect it from that error. I couldn’t find a way to fix it.