Tuesday, February 28, 2017

Laravel - query returns null

Im learning laravel. And now i have a problem. I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc). This form sends the data to a "orders" table. With the code that i have now, im not getting any data that i send from catalog. I choose the model and get nothing at orders. I have checked with $cars->count() on orders. it gets 0. Im using Laravel 5.4. Can anyone help me out?

This is the code

Web.php

Route::get('catalog', 'carController@catalog');
Route::get('orders/', 'CarController@orders')->name('orders');

CarController

function catalog() {
    $cars = DB::table('cars')->get();
    return view('catalog', compact('cars'));
}

function orders($modelesc=null) {
    $cars = DB::table('cars')->where('Model', $modelesc)->get();
    $colours = DB::table('colours')->get()->pluck('Colour');
    $status = DB::select('select * from order_status where id = ?', [1])->get();
    return view('orders', compact('cars', 'colours'));
}

Catalog.blade.php

@foreach($cars as $car)
{!! Form::open(array('action' => 'CarController@orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}   
@endforeach

Orders.blade.php

@foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', $status) !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
@endforeach




via Adato

Advertisement