从相关模型中查找ID Laravel

huangapple go评论62阅读模式
英文:

Finding ID from related model Laravel

问题

我正在使用三个模型:

用户模型

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * 可以批量赋值的属性。
     *
     * @var array<int, string>
     */
    protected $table = 'users';
    protected $fillable = [
        'name',
        'email',
        'password',
        'group'
    ];

...

    public function permission()
    {
        return $this->hasMany(Permission::class);
    }
}

档案模型

class Profile extends Model
{
    use HasFactory;
    protected $table = 'profiles';
    protected $fillable = [
        'profile_name',
        'first_name',
        'last_name',
        'profile_type',
        'TFN',
        'ABN',
        'ACN',
        'Address',
        'Email',
        'Phone',
        'established_date',
        'Notes',
        'activated'
    ];

...

    public function permission()
    {
        return $this->hasMany(Permission::class);
    }

}

权限模型

class Permission extends Model
{
    use HasFactory;

    protected $table = 'permissions';
    protected $fillable = [
        'profile_id',
        'user_id'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function profile()
    {
        return $this->belongsTo(Profile::class);
    }
}

任何用户都可以访问 0 个或多个配置文件,任何配置文件都可以授予 0 个或多个用户权限。

在用户编辑 blade 模板中,我正在创建切换按钮,用于为选定的用户添加/删除配置文件权限,使用 permissions.store 和 permissions.destroy 路由。创建新权限按预期工作,但我在访问权限 ID(在 blade 摘录中标记为****)以触发销毁功能方面遇到了问题。

用户控制器

public function edit(User $user, Profile $profile, Permission $permission)
{    

    $perm = Permission::where([
        ['user_id', '=', $user->id],
    ])->pluck('profile_id', 'id')->toArray();

    return view('users.edit', compact('perm'))
    ->with('user', $user)
    ->with('profiles', Profile::all());

}

权限控制器

public function destroy(Permission $permission, User $user, Profile $profile)
{      
    $permission = Permission::where('user_id', '=', $user->id)
    ->where('profile_id', '=', $profile->id)
    ->firstOrFail();

    $permission->delete();

    return redirect()->back();
}

Blade 摘录

@foreach($profiles as $key => $profile)
    
      @if(in_array($profile->id, $perm))
      <form action="{{ route('permissions.destroy', **** ) }}" method="POST" >
             @csrf 
             @method('DELETE')
             <input type="hidden" value="{{ $user->id }}" name="user_id" id="user_id" />
             <input type="hidden" value="{{ $profile->id }}" name="profile_id" id="profile_id" />
             <button class="btn btn-primary" type="submit">{{ $profile->profile_name }}</button>
          </form>
      
      @else
       <form action="{{ route('permissions.store') }}" method="POST" >
             @csrf 
             <input type="hidden" value="{{ $user->id }}" name="user_id" id="user_id" />
             <input type="hidden" value="{{ $profile->id }}" name="profile_id" id="profile_id" />
             <button class="btn btn-secondary" type="submit">{{ $profile->profile_name }}</button>
          </form>
       @endif

                   @endforeach

提前致谢。

英文:

I am working with three models:

User Model

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array&lt;int, string&gt;
     */
    protected $table = &#39;users&#39;;
    protected $fillable = [
        &#39;name&#39;,
        &#39;email&#39;,
        &#39;password&#39;,
        &#39;group&#39;
    ];
...

    public function permission()
    {
        return $this-&gt;hasMany(Permission::class);
    }
}

Profile Model

class Profile extends Model
{
    use HasFactory;
    protected $table = &#39;profiles&#39;;
    protected $fillable = [
        &#39;profile_name&#39;,
        &#39;first_name&#39;,
        &#39;last_name&#39;,
        &#39;profile_type&#39;,
        &#39;TFN&#39;,
        &#39;ABN&#39;,
        &#39;ACN&#39;,
        &#39;Address&#39;,
        &#39;Email&#39;,
        &#39;Phone&#39;,
        &#39;established_date&#39;,
        &#39;Notes&#39;,
        &#39;activated&#39;
    ];

   ...

    public function permission()
    {
        return $this-&gt;hasMany(Permission::class);
    }

}

Permission Model

class Permission extends Model
{
    use HasFactory;

    protected $table = &#39;permissions&#39;;
    protected $fillable = [
        &#39;profile_id&#39;,
        &#39;user_id&#39;
    ];

    public function user()
    {
        return $this-&gt;belongsTo(User::class);
    }

    public function profile()
    {
        return $this-&gt;belongsTo(Profile::class);
    }
}

Any user can have access to 0 or many profiles
Any profile can have permission given to 0 or many users

On the users.edit blade template I am creating toggle buttons to add/remove profile permissions for the selected user, using the permissions.store and permissions.destroy routes. Creating a new permission is working as expected, however I am having trouble accessing the permission id (marked **** in blade snippet) in order to trigger the destroy function.

User Controller

 public function edit(User $user, Profile $profile, Permission $permission)
    {    
        
        $perm = Permission::where([
            [&#39;user_id&#39;, &#39;=&#39;, $user-&gt;id],
        ])-&gt;pluck(&#39;profile_id&#39;, &#39;id&#39;)-&gt;toArray();
               
        return view(&#39;users.edit&#39;, compact(&#39;perm&#39;))
        -&gt;with(&#39;user&#39;, $user)
        -&gt;with(&#39;profiles&#39;, Profile::all());

    }

Permission Controller

public function destroy(Permission $permission, User $user, Profile $profile)
    {      
        $permission = Permission::where(&#39;user_id&#39;, &#39;=&#39;, $user-&gt;id)
        -&gt;where(&#39;profile_id&#39;, &#39;=&#39;, $profile-&gt;id)
        -&gt;firstOrFail();

        $permission-&gt;delete();

        return redirect()-&gt;back();
    }

Blade snippet

 @foreach($profiles as $key =&gt; $profile)
            
              @if(in_array($profile-&gt;id, $perm))
              &lt;form action=&quot;{{ route(&#39;permissions.destroy&#39;, **** ) }}&quot; method=&quot;POST&quot; &gt;
                     @csrf 
                     @method(&#39;DELETE&#39;)
                     &lt;input type=&quot;hidden&quot; value=&quot;{{ $user-&gt;id }}&quot; name=&quot;user_id&quot; id=&quot;user_id&quot; /&gt;
                     &lt;input type=&quot;hidden&quot; value=&quot;{{ $profile-&gt;id }}&quot; name=&quot;profile_id&quot; id=&quot;profile_id&quot; /&gt;
                     &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;{{ $profile-&gt;profile_name }}&lt;/button&gt;
                  &lt;/form&gt;
              
              @else
               &lt;form action=&quot;{{ route(&#39;permissions.store&#39;) }}&quot; method=&quot;POST&quot; &gt;
                     @csrf 
                     &lt;input type=&quot;hidden&quot; value=&quot;{{ $user-&gt;id }}&quot; name=&quot;user_id&quot; id=&quot;user_id&quot; /&gt;
                     &lt;input type=&quot;hidden&quot; value=&quot;{{ $profile-&gt;id }}&quot; name=&quot;profile_id&quot; id=&quot;profile_id&quot; /&gt;
                     &lt;button class=&quot;btn btn-secondary&quot; type=&quot;submit&quot;&gt;{{ $profile-&gt;profile_name }}&lt;/button&gt;
                  &lt;/form&gt;
               @endif

                           @endforeach

Thanks in advance

The permission ID is included in the array $perm created in the User controller, but I haven't found a way to call that value and associate it with the permission record so that I can send it to the permission.destroy route.

答案1

得分: 0

你可以将权限 ID 作为参数传递到表单的 action 属性中的路由中。

要做到这一点,尝试使用 [&#39;permission&#39; =&gt; array_search($profile-&gt;id, $perm)] 替换 ****。

这里,array_search() 函数用于在 $perm 数组中查找配置文件 ID 的索引,并使用该索引访问相应的权限 ID。然后,将该数值作为名为 permission 的参数传递到表单 action 属性中的路由中。

英文:

You can pass the permission ID as a parameter to the route in the action attribute of the form.

To do this, try replacing **** with [&#39;permission&#39; =&gt; array_search($profile-&gt;id, $perm)].

Here, the array_search() function is used to find the index of the profile ID in the $perm array, and the corresponding permission ID is accessed using that index. This value is then passed as a parameter named permission to the route in the form's action attribute.

huangapple
  • 本文由 发表于 2023年2月18日 14:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定