Estou criando um sistema com laravel, e usando só a api dele.
Codigo da API:
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();
});
Arquivo Desk, no diretório App/
esse é o arquivo que determina a tabela, estou usando 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'
];
}
e essa é a migração
<?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');
}
}
o erro é esse:
"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))"
quando eu não defino 'password' como atributo protegido "protected $hidden", funciona normalmente, mas quando protejo ele da ess erro. Não achei como resolver.