I create a simple app with users, articles and offers.
Now User can create article and offers to that article.
SO User hasMany Article and hasMany Offers, article belogsTo User and hasMany Offers and offers belongsTo User and Article...
so I write:
at User model:
public function articles(){
return $this->hasMany('App\Article');
}
public function offers(){
return $this->hasMany('App\Offer');
}
at Article model:
public function user(){
return $this->belongsTo('App\User');
}
public function offers(){
return $this->hasMany('App\Offer');
}
and at Offer model I write:
public function user(){
return $this->belongsTo('App\User');
}
public function article(){
return $this->belongsTo('App\Article');
}
But Now I have a probem how to create store() function in my OffersController - I try:
public function store(Requests\OfferRequest $request)
{
$offer = new Offer($request->all());
Auth::user()->articles()->offers()->save($offer);
Alert::success('Auction is succesfully added!','Good job!')->persistent("Close");
return redirect('offers');
}
but I get error...
also my form code is:
{!! Form::open(['url'=>'offers']) !!}
<div class="form-group">
{!! Form::label('price','Price') !!}
{!! Form::text('price', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Offer', ['class'=>'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
and my route is:
Route::resource('offers', 'OffersController');
How to add Offer and that offer need to have user_id of Auth::user and article_id ...