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条)

报告相同问题?