Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shami003's avatar

Insert primary key as foreign key in another table - Laravel-6

I have three tables bidding, auction, and products. I want to insert auction_id as foreign in the bidding table. The auction_id must be in relation to the product for which I am doing a bid.

Also, I want to show the bidding list on the product show view. (Only bidding done on that product).

BiddingsController

public function store(Request $request)
    {
        $bid = new Bidding();
        $bid->bidamount = request('bidamount');
        $bid->user_id = auth()->user()->id;
        $bid->auction_id = ???;
        $bid->save();

        return redirect('products.show')->with('success', 'You bid has been placed');
    }

Auction Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Auction extends Model
{
    protected $fillable = [
        'deadline',
    ];

    public function bidding(){
        return $this->hasMany('App\Bidding');
    }
}

Product Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name', 'price', 'description', 'image',
    ];

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

    public function auction()
    {
        return $this->hasOne('App\Auction');
    }

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

Product show blade

@extends('layouts.app')

@section('content')
<div class="container">
  <div class="my-3 border">
    <div class="row">
      <div class="col-md-7">
        <h2 class="mt-2">{{$products->name}}</h2>
        <p>{{$products->description}}</p>
        <span>Category: {{$products->category->name}}</span><br>
        <span class="text-right">Auction Ends: {{$products->auction->deadline}}</span>
        <div class="price">Initial Price: {{$products->price}}</div>

        {!! Form::open(['action' => 'BiddingsController@store', 'method' => 'POST']) !!}
          <div class="form-inline">
            {{Form::number('bidamount', '',['class' => 'form-control mr-1', 'placeholder' => 'Place your bid'])}}
            {{Form::submit('Place Bid', ['class' => 'btn btn-primary'])}}
          </div>
        {!! Form::close() !!}

        <!-- Bidders List -->
        <div class="table-wrapper">
          <table class="bidderlist">
            <tr>
              <th>Bidders</th>
              <th>Bid Amount</th>
              <th>Bid time</th>
            </tr>

            @if($biddings)
              @foreach ($biddings as $bidding)
                <tr>
                  <td>{{$bidding->user->name}}</td>
                  <td>{{$bidding->bidamount}}</td>
                  <td>{{$bidding->created_at}}</td>
                </tr> 
              @endforeach
            @endif
            
          </table>
        </div>
      </div>
    </div>
  </div>
</div> 
@endsection
0 likes
8 replies
MichalOravec's avatar

@shami003 Change your route that your store method will have parameter product then you can do it like this

public function store(Request $request, Product $product)
{
    $bid = new Bidding();
    $bid->bidamount = request('bidamount');
    $bid->user_id = auth()->user()->id;
    $bid->auction_id = $product->auction->id;
    $bid->save();

    return redirect('products.show')->with('success', 'You bid has been placed');
}
shami003's avatar

@michaloravec Hey thanks for the response.

I am getting the error

Trying to get property 'id' of non-object

Also, redirect page shows 404 error

shami003's avatar

@michaloravec Here is my routes:

<?php

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/', 'PagesController@index');
Route::get('/contact', 'PagesController@contact');
Route::get('/itemdetails', 'PagesController@itemdetails');
Route::get('/sellproduct', 'PagesController@createAuction');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/seller', 'PagesController@seller');

Route::resource('admin/users', 'AdminUsersController');

Route::resource('products', 'ProductsController');

Route::resource('biddings', 'BiddingsController');
MichalOravec's avatar
Level 75

@shami003 You can make it more simple like.

Add hidden input to your form like this.

{!! Form::open(['action' => 'BiddingsController@store', 'method' => 'POST']) !!}
    <div class="form-inline">
        {{Form::number('bidamount', '',['class' => 'form-control mr-1', 'placeholder' => 'Place your bid'])}}
        
        <input type="hidden" name="auction_id" value="{{ $products->auction->id }}">

        {{Form::submit('Place Bid', ['class' => 'btn btn-primary'])}}
    </div>
{!! Form::close() !!}

Then you have auction id in your controller.

public function store(Request $request)
{
    $auction = Auction::findOrFail($request->auction_id);

    $bid = new Bidding();
    $bid->bidamount = request('bidamount');
    $bid->user_id = auth()->user()->id;
    $bid->auction_id = $auction->id;
    $bid->save();

    return redirect('products.show')->with('success', 'You bid has been placed');
}
shami003's avatar

@michaloravec so cool. I am thinking about it for 2 days and didn't do that. Thank you so much.

How do I show bids only placed on that specific product?

 @if($biddings)
	@foreach ($biddings as $bidding)
                <tr>
                  <td>{{$bidding->user->name}}</td>
                  <td>{{$bidding->bidamount}}</td>
                  <td>{{$bidding->created_at}}</td>
                </tr> 
	@endforeach
@endif

Full code for show view is given above.

Also I want to redirect to same page when I place the bid

MichalOravec's avatar

@shami003 Also if it was correct answer before what I posted could you mark it as best reply? Thanks.

// in controller something like this

$product = Product::with('auction.biddings')->first();

// in view

@if ($product->auction->biddings->isNotEmpty())
    @foreach ($product->auction->biddings as $bidding)
        <tr>
            <td>{{$bidding->user->name}}</td>
            
            <td>{{$bidding->bidamount}}</td>
            
            <td>{{$bidding->created_at}}</td>
        </tr> 
    @endforeach
@endif

For return back from your store method

return back()->with('success', 'You bid has been placed');

Please or to participate in this conversation.