Laravel - 一对多关系

时间:2016-03-04 15:12:02

标签: php laravel laravel-5.1

我的关系出现问题,返回以下错误:

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);
    }
}

2 个答案:

答案 0 :(得分:0)

是的,你在这里处理像对象这样的Request对象。

您需要创建新对象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();

答案 1 :(得分:0)

您可以这样做:

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


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

}

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