lundi 27 mai 2019

laravel, how to use controller store Action to save objects in cascade

I'm working on an app that requires multi-authentication, I used creative-tim preset for the dashboard (providing that creative-tim uses the "User" name for storing users in the dashboard) I created on top of that an Admin table that has a one-to-one relationship with the User table, and created the appropriate middleware to restrict access to my "Admin-only" views and used tinker to create an Admin and link it to a test everything. the problem now is that I want to automate the creation of the admin relationship with the user through a registration form, only problem is, the creative-tim template I'm using doesn't follow the laravel docs so I couldn't understand it and I don't know how to link an "Admin" model to the crated "User".

Here's a part of the database/model: User(id,name,email,email_verified_at,password,rememberToken,timestamps) Admin(id,user_id,role,timestamps)

the way they used is (obviously store) but they used a UserRequest and User Object, I tried doing this:

        $Admin = new Admin();
        $Admin->role="Admin";
        $Admin->user_id = $model->id;
        $Admin->save();

here's the original function I found

    public function store(UserRequest $request, User $model)
    {
        $model->create($request->merge(['password' =>Hash::make($request->get('password'))])->all());
  return redirect()->route('user.index')->withStatus(__('User successfully created.'));
    }



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2W5uLcJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire