I have a form payments
with 4 fields -> date_paying, type_sitting, number_seance, fk_student
.
In my form trainings
I have 3 fields -> date_sitting, fk_payment, fk_student
.
My problem is that when I want to add a recording, I retrieve 2 times the same value in my drop down list.
I really want to keep only an item
In my create.blade(trainings) I have this:
<div class="form-group {{ $errors->has('fk_payment') ? 'has-error' : '' }}">
<label for="company-content">Payment </label>
<select name="fk_payment" id="fk_payment" class="form-control" required="required" value="{{ old('fk_payment')}}"/>
<option value="">Choice Payment</option>
@foreach($payments as $payment)
<option value="{{$payment->id}}" {{ old('fk_payment') == $payment->id ? 'selected' : '' }} >
{{$payment->type_sitting}}
</option>
@endforeach
{!! $errors->first('fk_payment', '<span class="help-block">:message</span>') !!}
</select>
Here is my Controller Training
class TrainingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$trainings = Training::oldest()->paginate(5);
return view('admin.trainings.index', compact('trainings'));
with('i', (request()->input('page', 1) -1) *5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$payments = Payment::all();
$students = Student::all();
return view('admin.trainings.create', compact('payments', 'students','trainings'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'date_sitting' => 'required',
'fk_payment' => 'required',
'fk_student' => 'required'
]);
$exists = Training::where('date_sitting', $request->get('date_sitting'))->where('fk_payment', $request->get('fk_payment'))->where('fk_student', $request->get('fk_student'))->count();
if (!$exists){
//$payment = Payment::where('fk_student', $request->get('fk_student'))->first();
$payment = Payment::where('fk_student', $request->get('fk_student'))
->take(1)
->get();
if(!isset($payment)){
return redirect()->route('trainings.index')
->with('error', 'No Payment, no training for you!');
}
else{
Training::create($request->all());
return redirect()->route('trainings.index')
->with('success', 'new data created successfully');
}
}
}
Controller Payment
class PaymentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$payments = Payment::oldest()->paginate(5);
return view('admin.payments.index', compact('payments'));
with('i', (request()->input('page', 1) -1) *5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$students = Student::all();
return view('admin.payments.create', compact('students', 'payments'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'date_payment' => 'required',
'type_sitting' => 'required',
'number_seance' => 'required',
'fk_student' => 'required'
]);
$exists = Payment::where('date_payment', $request->get('date_payment'))->where('type_sitting', $request->get('type_sitting'))->where('number_seance', $request->get('number_seance'))->where('fk_student', $request->get('fk_student'))->count();
if (!$exists){
Payment::create($request->all());
return redirect()->route('payments.index')
->with('success', 'new data created successfully');
}
else{
return redirect()->route('payments.index')
->with('error', 'doublon');
}
}
Model - Payment
class Payment extends Model
{
//
protected $fillable = ['date_payment', 'type_sitting', 'number_seance', 'fk_student'];
protected $dates = ['date_payment'];
public function students(){
return $this->belongsTo('App\Student', 'fk_student');
}
public function trainings(){
return $this->hasMany('App\Training', 'fk_payment');
}
}
Model - Training
class Training extends Model
{
//
protected $fillable = ['date_sitting', 'fk_payment', 'fk_student',];
protected $dates = ['date_sitting'];
public function students(){
return $this->belongsTo('App\Student', 'fk_student');
}
public function payments(){
return $this->belongsTo('App\Payment', 'fk_payment');
}
}