jeudi 3 septembre 2015

laravel 5 get ID from relationship

I have a Project Model and in it I have

protected $table = 'projects';
protected $guarded = [];

public function docOne()
{
    return $this->hasOne('App\DocOne', 'id');
}

In the Model DocOne, I have

protected $table = 'doc_one';
protected $guarded = [];

public function project()
{
    return $this->belongsTo('App\Project', 'id');
}

So it is a one to one relationship. Now I have a form which collects information to fill in the doc_one table. In the controller, I do

public function store(Request $request)
{
    $input = Input::all();
    DocOne::create( $input );

    return Redirect::route('projects.index')->with('message', 'Document saved');
}

This fails because the database also expects the project ID. How can I get the id for the project that is creating doc_one?

Thanks



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

Aucun commentaire:

Enregistrer un commentaire