Akshay Vaghasiya
Akshay Vaghasiya

Reputation: 1637

How to auto fill foreign key column value in Laravel 5.2?

I'm working on laravel 5.2 and also beginner of laravel. I have users and clients table in the database. user field of clients table stores the value of id column(primary key) of users table. i.e, one user have many clients and one client belongs to one user. Now come to problem. When I'm going to insert new client of the logged in user, I'm getting error. No any error message. I searched and come to know that perhaps it happens because of foreign key column name is user, not user_id. So I updated my Client model from return $this->belongsTo('App\User'); to return $this->belongsTo('App\User', 'user');. But still failed. If any one knows the answer, answer will be appreciated. Here is my code.

ClientController (Try 1)

$request->user()->clients()->create([
    'user' => $request->user()->id,
    'email' => $request->email,
    'age' => $request->age
]);

ClientController (Try 2)

$request->user()->clients()->create([
    'email' => $request->email,
    'age' => $request->age
]);

Client Model

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Ak\Scopes\AgeScope;

class Client extends Model
{
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new AgeScope);
    }

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

}

User Model

namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable = [
        'name', 'email', 'password', 'country'
    ];

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

    protected  $date = ['deleted_at'];

    public function client()
    {
        return $this->hasMany('App\Client', 'user');
    }
}

Upvotes: 1

Views: 4452

Answers (3)

Akshay Vaghasiya
Akshay Vaghasiya

Reputation: 1637

Problem is Solved

Problem is solved as I have to write public $timestamps = false; in Client Mode. After it, I don't need to insert user value manually. Because laravel is doing that task itself as I have write foreign key in User and Client Models relationship. i.e, return $this->hasMany('App\Client', 'user') and return $this->belongsTo('App\User', 'user') manually.

I can't understand that what should I do for this feature of laravel. Should I cry or laugh?

Upvotes: 0

DevK
DevK

Reputation: 9942

Try adding protected $fillable = ['email', 'age']; to your Client model.

More on mass assignement

Edit: try this

Routes:

Route::post('user/{user}/client', 'YourController@store');

Controller:

public function store(Request $request, User, $user){
    $user->client()->create([
       'user' => $request->user()->id,
       'email' => $request->email,
       'age' => $request->age
    ]);
}

Upvotes: 0

oseintow
oseintow

Reputation: 7371

sorry your relationship is wrong. and moreover you are using

 $request->user()->clients()

instead of

  $request->user()->client() 

the client in your relationship is singular. And the client function body should be

  return $this->hasMany('App\Client', 'user');

and in your client model add

 $fillable = ['email', 'age', 'user'];

and change the user function in your client model to

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

the orm should be like this

$request->user()->client()->create([
   'user' => $request->user()->id,
   'email' => $request->email,
   'age' => $request->age
]);

Upvotes: 3

Related Questions