Ad

Store Offers() Which Have User_id And Article_id - Laravel

- 1 answer

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 offer(){
        return $this->hasMany('App\Offer');
    }

at Article model:

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

    public function offer(){

        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()->offer()->save($offer);

        Alert::success('Offer is succesfully added!','Good job!')->persistent("Close");

        return redirect('offers');
    }

but I get error:

BadMethodCallException in Builder.php line 2071: Call to undefined method Illuminate\Database\Query\Builder::article() enter image description here

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 ...

When I use: Auth::user()->offer()->save($offer); then store offer fine but just ith user_id and I dont know how I can get article_id to store for offer.

Also I know that I can put {{$article->id}} at blade template and fetch them from form but it is security high risk...

One more thing - just users (admins) can create article but all users can put offers to an article, so becouse that I need user_id at offers table...

Please help!

Ad

Answer

I think the problem is your not selecting an Article to attach the Offer to. Try either selecting an Article or save it directly on the User.

in your controller, you can do something like this to save the offer to a specific Article...

Auth::user()->articles()->find($articleID)->offer()->save($offer);

or save it to the User like this...

Auth::user()->offer()->save($offer);

not sure how you're sharing offers between articles and users but cool if you are.

Hope that helps...

quick minor suggestion: pluralize the offer methods because it is a hasMany relationship.

Ad
source: stackoverflow.com
Ad