lundi 28 septembre 2015

Laravel 5.1 eloquent orm relationship

I have following setup:

Profile.php model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}

User.php model:

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

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

    public function profile()
    {
        return $this->hasOne('App\Models\Profile');
    }
}

AuthController.php:

public function signup(Request $request)
    {
        $user = new User;
        $profile = new Profile;
        $user->email = $request->input('email');
        $user->first_name = $request->input('first_name');
        $user->last_name = $request->input('last_name');

        $user->password = Hash::make($request->input('password'));

        $user->save();
        $role = Role::whereName('user')->first();

        $profile->city = $request->input('city'); ---> profile model
        $profile->gender = $request->input('gender'); --> profile model
      } 

How can I save to profile based on relationship? I can't directly save to profile due to foreign key constraint.

Thanks!!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1iVOgwG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire