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