Laravel 5.6 - 雄辩的创造赢得了不接受阵列

时间:2018-05-30 13:24:58

标签: laravel laravel-5

出于某种原因,当我尝试创建新用户时,创建方法不会接受传递的数组

我收到以下错误:

SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2018-05-30 13:24:33, 2018-05-30 13:24:33))

这是当前的代码:

public function storeUser($data, $request)
    {
        if($request->hasFile('avatar'))
            $data['avatar'] = $this->uploadAvatar($request->file('avatar'));

        $password = \Hash::make($data['temp_password']);

        $data['password']           = $password;
        $data['temporary_password'] = $password;
        $data['social']             = Strings::arrayToJson($data['social']);

        // Assign the clinic_id from the select menu (only super admin have the permission to do this)
        if(\Auth::user()->hasRole('super_admin') && isset($data['clinic_user']))
            $data['clinic_id'] = (int) $data['clinic_user'];

        // If the admin of the clinic is creating the user we wiil use his clinic_id for the new user
        if(\Auth::user()->hasRole('admin'))
            $data['clinic_id'] = \Auth::user()->clinic_id;

        if($this->create($data)){

            event(new NewUser($this, $data['temp_password']));

            if(\Auth::user()->hasRole('super_admin') && isset($data['clinic_owner']))
                event(new ClinicUpdate($this));

            if(\Auth::user()->hasRole('super_admin', 'admin') && \Auth::user()->id !== $this->id)
                $user->giveRole($data['user_role']);

            return true;
        }

        return false;

    }

用户模型:

<?php

namespace App;

use App\App\Permissions\HasPermissionsTrait;

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

    class User extends Authenticatable
    {
        use Notifiable, HasPermissionsTrait;

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'first_name', 'last_name', 'about', 'education', 'position', 'phone',
            'social', 'title', 'gender', 'avatar', 'location', 'email', 'password',
            'temporary_password', 'verified', 'clinic_id'
        ];

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

        public function clinic()
        {
           return $this->belongsTo(Clinic::class);
        }

        public function files()
        {
           return $this->hasMany('App/Media');
        }

        public function verifyUser()
        {
            return $this->hasOne('App\VerifyUser');
        }

    }

UserCreateRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UserCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];

        if(\Auth::user()->hasRole('super_admin', 'admin'))
            $rules = [
                'user_role'    => [Rule::in(['super_admin', 'admin', 'user'])],
                'clinic_owner' => 'integer',
                'clinic_user'  => 'integer',
            ];

        return array_merge($rules, [
            'first_name'    => 'required|string|max:255',
            'last_name'     => 'required|string|max:255',
            'email'         => "required|string|email|unique:users",
            'temp_password' => 'required|string|min:8',
            'title'         => 'integer',
            'gender'        => [Rule::in([0, 1])],
            'position'      => 'string|max:255|nullable',
            'phone'         => 'string|max:255|nullable',
            'location'      => 'string|max:255|nullable',
            'about'         => 'string|nullable',
            'social.*'      => 'url|nullable',
            'avatar'        => 'nullable|image|mimes:jpeg,jpg,png,gif',
        ]);
    }

    public function attributes()
    {
        return [
            'social.facebook'  => 'Facebook',
            'social.twitter'   => 'Twitter',
            'social.instagram' => 'Instagram',
            'social.linkedin'  => 'Linkedin',
            'temp_password'    => 'Temporary Password',
        ];
    }
}

NewUser事件:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class NewUser
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $_user;
    public $_tempPass;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user, $tempPass)
    {
        $this->_user     = $user;
        $this->_tempPass = $tempPass;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return [];
    }
}

UsersController(商店方法):

public function store(UserCreateRequest $request)
    {
        $validated = $request->validated();

        $model = new UserQuery;

        if(!$model->storeUser(XSS::clean($validated, ['avatar']), $request)){

            Session::flash('alert', [
                'message' => 'Something went wrong. Please try again',
                'type'    => 'danger'
            ]);

            return \Redirect::back();
        }

        return redirect('/admin/users');

    }

数据的 dd

array:17 [▼
  "user_role" => "admin"
  "clinic_user" => "1"
  "first_name" => "Sasha"
  "last_name" => "Miljkovic"
  "email" => "xxxxxx@xxxxx.com"
  "temp_password" => "12345678"
  "title" => "10"
  "gender" => "0"
  "position" => "Tester"
  "phone" => "1234"
  "location" => "Pirot"
  "about" => "Maecenas gravida tellus augue, sed mollis quam viverra at. Aenean sit amet dui non eros laoreet porta et nec nisi. Cras lectus justo, porttitor quis mattis nec, ▶"
  "social" => "[]"
  "avatar" => "avatar_5b0ea46d2af8d.jpg"
  "password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
  "temporary_password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
  "clinic_id" => 1
]

2 个答案:

答案 0 :(得分:1)

您应该实例化User,尝试:

(new User())->create($data)

如果有效,请替换:

$model = new UserQuery;

通过:

$model = new User;

答案 1 :(得分:-1)

您必须传递所有必需的文件

$data['first_name'] = 'some name';

错误告诉您first_name字段未通过您必须传递此字段,并且需要其他字段来防止此错误