我在laravel上遇到了好几天的问题 因为我无法将图像上传到与徽标对应的注册表单 已经尝试以不同方式验证徽标字段,但laravel不接受它们 laravel总是回答我试图上传的图像不是图像 这是我的控制器
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Role;
use App\Actserv;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Webcraft\Random\RandomFacade;
class RegisterController extends Controller
{
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
//'name' => 'required|string|max:255',
'email' => 'required|string|email|min:6|max:128|unique:users',
'password' => 'required|string||min:6|max:128|confirmed',
'nombre_empresa' => 'required|string|unique:users|min:1|max:255',
'pais' => 'required|string|not_in:Opción no seleccionada|max:128',
'tipo' => 'required|string|not_in:Opción no seleccionada|max:128',
'ciudad' => 'required|string|min:1|max:200',
'direccion' => 'required|string|min:1|max:255',
'telefono' => 'nullable|numeric',
'celular_e' => 'required|unique:users|numeric',
'postal' => 'nullable|string|min:1|max:128',
'nit' => 'nullable|unique:users|numeric|min:1',
'web' => 'nullable|string|min:1|max:255',
'facebook' => 'nullable|string|min:1|max:255',
'oferta' => 'required|string|min:1|max:255',
'demanda' => 'required|string|min:1|max:255',
'nombre_r' => 'required|string|min:1|max:128',
'ci' => 'required|unique:users|numeric',
'email_r' => 'required|unique:users|string|email|min:1|max:128',
'cargo' => 'required|string|min:1|max:128',
'celular_r' => 'required|numeric|unique:users',
//--------------------------------------------
//field that validates the logo---------------
'logo' => 'nullable|image|mimes:jpg,bmp,png',
//--------------------------------------------
'actv' => 'required|array|min:1',
'actv.0' => 'required|string|not_in:Opción no seleccionada|max:255',
'actv.1' => 'string|max:255',
'actv.2' => 'string|max:255',
'srv' => 'required|array|min:1',
'srv.0' => 'required|string|not_in:Opción no seleccionada|max:255',
'srv.1' => 'string|max:255',
'srv.2' => 'string|max:255',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$cod = RandomFacade::generateString(6, '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //se genera un unico codigo por cada usuario registrado
$user = User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
'nombre_empresa' => $data['nombre_empresa'],
'pais' => $data['pais'],
'tipo' => $data['tipo'],
'ciudad' => $data['ciudad'],
'direccion' => $data['direccion'],
'telefono' => $data['telefono'],
'celular_e' => $data['celular_e'],
'postal' => $data['postal'],
'nit' => $data['nit'],
'web' => $data['web'],
'facebook' => $data['facebook'],
'oferta' => $data['oferta'],
'demanda' => $data['demanda'],
'nombre_r' => $data['nombre_r'],
'ci' => $data['ci'],
'email_r' => $data['email_r'],
'cargo' => $data['cargo'],
'celular_r' => $data['celular_r'],
'estado' => '0', //indica si el usuario esta activo(1) o inactivo(0)
'tipo_user' => 'user',//indica que tipo de usuario es user=usuario normal admin=usuario administrador local=usuario designado por el admin
'codigo' => $cod,
]);
$actividad = $data['actv'];
$servicios = $data['srv'];
$actserv = Actserv::create([
'emp_id' => $user->id,
'actividad' => $actividad[0],
'servicios' => $servicios[0],
]);
$actserv = Actserv::create([
'emp_id' => $user->id,
'actividad' => $actividad[1],
'servicios' => $servicios[1],
]);
$actserv = Actserv::create([
'emp_id' => $user->id,
'actividad' => $actividad[2],
'servicios' => $servicios[2],
]);
$user
->roles()
->attach(Role::where('name', 'user')->first());
return $user;
}
}
这是注册表格:
<form id="needs-validation" novalidate method="post" enctype=”multipart/form-data”>
...
<label for="logo">Logo</label>
<input type="file" class="form-control-file" name="logo" id="logo">
...
</form>
我尝试上传图片时出现此错误
logo must be an image.
logo must be a file with format: jpeg, bmp, png.
可能是什么错误? 谢谢
答案 0 :(得分:0)
首先,查看你的“在enctype上他们是不同的。第二次从规则中删除图像并尝试dd($ data ['image']);如果有不同的数据,请尝试使用名称徽标的另一种输入类型的检查表单通过某种方式。