Monday, March 6, 2017

laravel pluck returns id value "0"

Im learning laravel. And now i am facing a problem that i have searched a solution for and havent found. The Pluck method is returning the first id value from the DB as "0". Shouldnt it be "1" ? Because of that, i cant use the first id value (it gives the error: "SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" ) and have to pick from the second choise for it to work. I wonder if am doing something wrong. Iam using laravel 5.4. This is the code:

controller

function orders(Request $request) {
    $modelesc = $request->modelesc;
    $robots = DB::table('robots')->where('Model', $modelesc)->get();
    $colours = DB::table('colours')->get()->pluck('Colour');
    return view('orders', compact('robots', 'colours'));
}

function storeorders(Request $request) {
    order::create($request->all());
}

view

@foreach($robots as $robot)
  {!! Form::open(['method' => 'POST']) !!}
    Model: {!! Form::text('Model', $robot->Model) !!}<br><br>
    {!! Form::hidden('users_id', Auth::user()->id) !!}
    {!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
    Choose colour: {!! Form::select('Colour_id', $colours) !!}<br><br>
    {!! Form::hidden('Order_status_id', '1') !!}
    {!! Form::submit('Order') !!}
  {!! Form::close() !!}
@endforeach



via Adato

Advertisement