duanjuhuo8772 2018-08-17 10:44
浏览 37
已采纳

将第二个用户模型数据传递给Laravel中的控制器

So I have 2 User models, one is User and other is OrgUser for company users. I'm trying to pass OrgUser to a OrgUserController and display single company user in show method. But I always get an empty object.

User Model

namespace App;

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

class User extends Authenticatable
{
use Notifiable;

protected $fillable = [
    'name', 'email', 'password', 'verified', 'verification_token',
];

protected $hidden = [
    'password', 'remember_token', 'verification_token',
];

OrgUser Model

namespace App;

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

class OrgUser extends Authenticatable
{
use Notifiable;

protected $fillable = [
    'name', 'email', 'password', 'verified', 'verification_token', 'admin'
];

protected $hidden = [
    'password', 'remember_token', 'verification_token',
];

OrgUserController

<?php

namespace App\Http\Controllers\OrgUser;

use App\Http\Resources\OrgUserResource;
use App\OrgUser;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

class OrgUserController extends ApiController
{
public function show(OrgUser $orgUser)
{
//        OrgUserResource::withoutWrapping();
//
//        return new OrgUserResource($orgUser);

    return $orgUser;
}

Routes

Route::resource('orgusers', 'OrgUser\OrgUserController', ['only' => ['index', 'show']]);

Is there something that I would need to add to OrgUser model so I could pass data to controller like that? Because it works if I pass $id.

  • 写回答

2条回答 默认 最新

  • duanpaxin3531 2018-08-17 13:31
    关注

    So It seems the problem was with naming in show method. I need to pass $orguser and not $orgUser. Because in my route it was /orgusers/{orguser}.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组