【问题标题】:Laravel - One to many relationshipLaravel - 一对多关系
【发布时间】:2016-03-04 15:12:02
【问题描述】:

我的关系有些问题,返回以下错误:

Call to undefined method Illuminate\Database\Query\Builder::ftpAccounts()

这是我的模型:

客户模型

class Customer extends Model
{

    protected $table = 'customers';

    protected $fillable = [
        'name',
        'email',
        'phone',
        'status',
    ];

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

    public function ftpAccounts()
    {
        return $this->hasMany(FTPAccounts::class);
    }
}

FTP 帐户模型

class FTPAccounts extends Model
{
    protected $table = 'ftp-accounts';

    protected $fillable = [
        'host',
        'user',
        'password',
        'status',
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

我的控制器:

class AdminFTPAccountsController extends AdminBaseController
{
    private $repository;

    public function __construct(FTPAccountsRepository $repository)
    {
        parent::__construct();
        $this->repository = $repository;
    }

    public function index()
    {
        return view('admin.pages.admin.clientes.ftp.index', [
            'customers' => $this->repository->allFTPAccounts(),
            'title' => 'TESTE'
        ]);
    }

    public function getCreate()
    {
        return view('admin.pages.admin.clientes.ftp.create', [
            'title' => 'Nova conta FTP'
        ]);
    }

    public function postCreate(CustomerFTPAccountCreateRequest $request)
    {
        $request->user()->customers()->ftpAccounts()->create([
            'host' => $request->host,
            'user' => $request->user,
            'password' => $request->password,
        ]);

        return redirect('admin/clientes');
    }
}

会是什么?我是不是做错了什么?

## 编辑 01 ##

用户模型:

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'phone', 'password', 'status'];

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

//    protected $casts = ['status' => 'boolean'];

    public function articles()
    {
        return $this->hasMany(Article::class);
    }

    public function customers()
    {
        return $this->hasMany(Customer::class);
    }
}

【问题讨论】:

  • 您的User 模型没有任何名为ftpAccounts() 的方法。
  • @JilsonThomas 我想将 FTP 帐户与客户而非用户相关联,我不知道该怎么做

标签: php laravel laravel-5.1


【解决方案1】:

你可以这样做:

$customers = $request->user()->customers;


foreach($customers as $customer)
{
    $customer->ftpAccounts()->create([
            'host' => $request->host,
            'user' => $request->user,
            'password' => $request->password,
        ]);

}

这将为您提供一个良好的开端。

【讨论】:

  • 但是,等等。您还可以在您的问题中发布User 模型吗?
  • 好的,你试过我给你的那个了吗?
  • 是的,什么都没发生
  • dd($request->user()->customers); 会发生什么?
  • 返回一个空数组
【解决方案2】:

是的,您在这里将 Request 对象视为 Model 对象。

您需要创建新对象 Customer 对象并在其中使用 $request->get('user') 作为变量。

我无法测试您的代码,但您可以尝试以下方法:

$customer = Customer::where('user', $request->get('user');

$ftpAccount = new FTPAccount;
$ftpAccount->host = $request->get('host');
$ftpAccount->user = $request->get('user');
$ftpAccount->password = $request->get('password');

$customer->ftpAccounts()->save();

【讨论】:

  • 如何进行?我不知道该怎么做,我是 Laravel 的新手 =/
  • 查看更新的代码。我无法测试它,但我想你明白了。
  • 是的,但我说过我无法测试您的代码。查看代码,尝试了解它是如何工作的并使用它。例如,我不知道您在$request->user 中得到了什么(id 或名称或其他),我不知道它应该是user 还是user_id 甚至id。所以,只要试着理解它是如何工作的,我相信你会通过一些小修复来让我的代码工作。
  • $request->user() 始终返回 Auth 用户实例。
  • 如果$requestRequest 类的实例,那么$request->user() 将返回当前经过身份验证的用户实例。如果您想从表单请求中访问名称为 user 的字段,则该字段将是 $request->input('user')。我在我的代码中使用了$request->user(),它运行良好。 :)
猜你喜欢
  • 2018-06-15
  • 2018-09-27
  • 2017-12-24
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多