Tartar Tartar - 2 months ago 18x
PHP Question

Laravel 5 How to Pass 2 Models to a Form

I have form and i am using form model binding. My form is

{!! Form::model($vehicle,['url' => '/pages/store']) !!}
<table style="width:650px; margin-left: 4px;" >
<td>License Plate</td>
<td>{!! Form::text('id' ,null , ['readonly'], ['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td>{!! Form::text('model' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
{!! Form::select('brand_id', $brands, null, ['id'=>'brandBox', 'style' => 'width:150px;']) !!}
<td>{!! Form::text('licenseplate' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td colspan="2">Client</td>
<td colspan="2">{!! Form::select('representive_client_id', $clients, null, ['id'=>'clientSelectBox', 'class' => 'selectbox']) !!}</td>
<td colspan="2">Telephone Number</td>
<td colspan="2">{!! Form::text('tel_number' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td colspan="2">Adress</td>
<td colspan="2">{!! Form::textarea('address' ,null ,['class' =>'textboxlong form-control','style'=>'height:60px;']) !!}</td>
<div id="buttoncontainer">
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $first -> id ) }}"><<</a>&nbsp;
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $previous ) }}">PREVIOUS</a>&nbsp;
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $next ) }}">NEXT</a>&nbsp;
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $last -> id ) }}">>></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a class="btn btn-default" id="add">EKLE</a>&nbsp;
{!! Form::submit('EDIT', array('class'=>'btn btn-primary')) !!}&nbsp;
{!! Form::submit('NEW RECORD', array('class'=>'btn btn-primary')) !!}&nbsp;
{!! Form::close() !!}

I am passing the

$vehicle = Vehicle::where('vehicles.id',$id)->join('clients', 'vehicles.representive_client_id', '=', 'clients.id')->first();

Store Function

$client = new Client;
$client -> full_name = $client_id;
$client -> tel_number = $tel_number;
$client -> mobile_number = $mobile_number;
$client -> save();

$last_client_id = $client -> id;

$input = Request::except('client_id');
$vehicle = Vehicle::create($input);

$u_vehicle = Vehicle::orderBy('created_at', 'desc')->first();
$u_vehicle -> update(array('client_id' => $last_client_id));

I am able to see all values of these fields in my view but when it comes to store a new record to my database i am getting this error

Column not found Unknown column 'tel_number'

Guess i need to pass 2 models (Vehicle and Client) to the form but not sure how to make it. Any help would be appreciated.


In short you need to pass your view a instance of Vehicle with Client - I know you're trying to do that with the join but I'm guessing that isn't the right way to do it. I set up a clean install with Laravel and it appears to work when you set up the relationship on the model.

I'm unsure what your relationships are so let's assume a Client has one Vehicle so the Vehicle belongs to the Client. So we can put your relationship in your Vehicle.php

public function client() {
   return $this->belongsTo('App\Client'); // or whatever the namespace to your class is

See more here on defining relationships: http://laravel.com/docs/5.1/eloquent-relationships#defining-relationships

Then when you load the model in your view you can do this:

$vehicle = \App\Vehicle::with('client')->where('id', $id)->first();

The important part being: with('client')

Then any text fields with Client attributes should be populated with the existing data.

I hope that helps, if you need more help please let me know. I've got sample code of it working in a blank laravel project so I can throw that up on github if you need to see it.