conedevelopment / bazar

Bazar is an e-commerce package for Laravel applications.
https://root.conedevelopment.com
MIT License
421 stars 56 forks source link

User extend problem #185

Closed bobitza closed 2 years ago

bobitza commented 2 years ago

Description:

i extended user, and maked functions like order, to make a dropdown to select user Roles, like order to select statuses but props in vue what i created got undefined ? `<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Illuminate\Http\Request;

use Bazar\Models\User as BazarUser;

class User extends BazarUser { use HasApiTokens, Notifiable; // HasFactory,

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = [
    'avatar',
    'role_name'
];

/**
 * The attributes that should have default values.
 *
 * @var array
 */
protected $attributes = [
    'name' => null,
    'email' => null,
    'role' => 'user'
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'deleted_at' => 'datetime',
    'email_verified_at' => 'datetime',
];

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name',
    'email',
    'password',
    'role'
];

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

/**
 * Get the available order statuses.
 *
 * @return array
 */
public static function userRole(): array
{
    return [
        __('User') => 'user',
        __('Casier') => 'casier',
        __('Manager') => 'manager'
    ];
}
/**
 * Get the role name attribute.
 *
 * @return string
 */
public function getRoleNameAttribute(): string
{
    return array_search($this->role, static::userROle()) ?: $this->role;
}

/**
 * Set the role by the given value.
 *
 * @param  string  $role
 * @return void
 */
public function markAs(string $role): void
{
    if ($this->role !== $role) {
        $this->setAttribute('role', $role)->save();
    }
}

/**
 * Get the filter options for the model.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public static function filters(Request $request): array
{
    return array_merge(static::defaultFilters($request), [
        'role' => static::roles(),
        'user' => User::proxy()->newQuery()->pluck('id', 'name')->toArray(),
    ]);
}
/**
 * Scope a query to only include orders with the given status.
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @param  string  $role
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeStatus(Builder $query, string $role): Builder
{
    return $query->where($query->qualifyColumn('role'), $role);
}

} `

and VUE;

Steps To Reproduce:

bobitza commented 2 years ago

I found the problem in UsersControler, i need to pass the variable with Role, but how i can extend that controller ?

bobitza commented 2 years ago

I just want to extend UserController from your package to add dropdown with Roles, how can i do ?

bobitza commented 2 years ago

After few hours i made it somehow with bindings in AppServiceProvider

    public $bindings = [
        \Bazar\Http\Controllers\UsersController::class => \App\Http\Controllers\Bazar\UsersBazarController::class,
    ];
<?php

namespace App\Http\Controllers\Bazar;
use App\Models\User as CrrUser;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Support\Facades\Redirect;
use \Bazar\Http\Controllers\UsersController;

class UsersBazarController extends UsersController
{

    /**
     * Display the specified resource.
     *
     * @param  \Bazar\Models\User  $user
     * @return \Inertia\Response
     */
    public function show(\Bazar\Models\User $user): Response
    {
        return Inertia::render('Users/Show', [
            'user'      =>  $user,
            'userRole'  =>  CrrUser::userRole()
        ]);
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Bazar\Http\Requests\UserUpdateRequest  $request
     * @param  \Bazar\Models\User  $user
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(\Bazar\Http\Requests\UserUpdateRequest $request, \Bazar\Models\User $user): \Illuminate\Http\RedirectResponse
    {
        $userBazar = CrrUser::find($user->id);
        $requestGood = $request->rules();
        $requestGood = array_merge_recursive($requestGood, ['role'=>['required', 'string']]);
        $userBazar->update($request->validate($requestGood));
        //--
        return Redirect::route('bazar.users.show', $userBazar)->with('message', __('The user has been updated.'));       
    }
}