我正在尝试向Laravel 5.3 User Auth添加自定义字段“角色”。该角色将确定用户是否是其他角色中的admin,因此,由于Auth脚手架使用了可分配的属性,因此存在漏洞。
因此我将用户模型更改为:
protected $guarded = [
'role'
];
RegisterController我添加了自定义字段(默认用户将拥有role = customer);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'facebook_id' => $data['facebook_id'],
'linkedin_id' => $data['linkedin_id'],
'avatar' => $data['avatar'],
'token' => $data['token'],
'role' => 'customer',
我收到此错误消息:
SQLSTATE [HY000]:常规错误:1364字段'role'没有默认的>值(SQL:插入
users
(name
,password
,facebook_id
,linkedin_id
,avatar
,token
,>updated_at
)值(名称,名称@ domain.co.uk,哈希密码,,,,,> 2017-01-20 17:21:16,2017-01-20 17:21:16))
不能为我的生活弄清楚。
我在想,也许我不使用质量可分配输入,但我有一种感觉可能会弄乱Auth脚手架的其余部分。
感谢您的帮助
答案 0 :(得分:1)
设置默认角色的一种简单方法是在模型
中实际定义它class User extends Model
{
protected $attributes = [
'role' => 'customer',
];
protected $guarded = [
'role'
];
//...
现在,当您使用create方法并且不传递'role'
属性时,插入内容将使用值'customer'
答案 1 :(得分:0)
在您添加角色字段的users表的迁移中,您需要将该字段设置为可为空,或者为其指定一个默认值,以便在没有它的情况下进行批量分配时,数据库知道如何处理那个领域。
public void doConnect() {
try {
InetAddress addr = InetAddress.getByName(currentIPaddress);
serverSocket = new ServerSocket(4445, 50, addr);
isServerStarted = true;
socket = serverSocket.accept();
inputStream = new ObjectInputStream(socket.getInputStream());
outputStream = new ObjectOutputStream(socket.getOutputStream());
String command = inputStream.readUTF();
this.statusBox.setText("Received message from Client: " + command);
} catch (IOException e) {
e.printStackTrace();
}
}
或
public void sendFile() {
fileEvent = new FileEvent();
String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
fileEvent.setDestinationDirectory(destinationPath);
fileEvent.setFilename(fileName);
fileEvent.setSourceDirectory(sourceFilePath);
File file = new File(sourceFilePath);
if (file.isFile()) {
try {
DataInputStream diStream = new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0) {
read = read + numRead;
}
fileEvent.setFileSize(len);
fileEvent.setFileData(fileBytes);
fileEvent.setStatus("Success");
} catch (Exception e) {
e.printStackTrace();
fileEvent.setStatus("Error");
}
} else {
System.out.println("path specified is not pointing to a file");
fileEvent.setStatus("Error");
}
//Now writing the FileEvent object to socket
try {
outputStream.writeUTF("newfile");
outputStream.flush();
outputStream.writeObject(fileEvent);
String result = inputStream.readUTF();
System.out.println("client says: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
如果您的代码可以处理null角色值。