Can you tell us what's not working?
I like to know what's not working before I even begin looking for the solution. :)
So, we are passing a $client model to the view. Now, based on the whether the model is empty or has a record , I want to be able to generate different post action links.
{{-- -----For creating new client------ --}}
@if(count($client) === 0)
{!! Form::open(array('url' => 'clients/save', "class" => "form-horizontal")) !!}
@include('clients.clientForm')
</br>
</br>
{!! Form::submit("Create", array("class"=>"btn btn-md btn-primary")) !!}
<a href="{{url('/clients')}}" class="btn btn-default btn-md">Cancel</a>
{!! Form::close() !!}
{{-- -----For editing existing client------ --}}
@else
{!! Form::open(array('url' => 'clients/update/'.$client->ClientId, "class" => "form-horizontal")) !!}
@include('clients.clientForm')
</br>
</br>
{!! Form::submit("Update", array("class"=>"btn btn-md btn-primary")) !!}
<a href="{{url('/clients')}}" class="btn btn-default btn-md">Cancel</a>
{!! Form::close() !!}
@endif
Now, when passing my empty model when creating a new client I get the first form action link. But, when i send the model with the record of a client, I want to generate second form action link which is not happening.
The controller methods are as follows :-
public function create()
{
return view('clients.create')->with(['client' => compact($this->client->model())]);
}
public function edit($id)
{
$client = $this->client->find($id);
return view('clients.create')->with(['client' => compact($client)]);
}
Okay. I think the first problem is how you passing your data. This is what you are doing right now:
return view('clients.create')->with(['client' => compact($client)]);
This doesn't work. The way you are doing it now essentially just passes an empty array. PHP's compact
function accepts either a string or an array of strings. If you do not pass either a string or array of strings, I believe it just generates an empty array so essentially, you are just saying with(['client' => []]);
. In addition to that, I wouldn't mix in with
, an array
, and compact
all in one.
You should change that to one of the following three methods below.
return view('clients.create', compact('client'));
return view('clients.create')->with('client', $client);
return view('clients.create')->with(['client' => $client]);
I would start with that and see how much debugging you can do from there.
Regarding your "edit" form, you might want to look into form model binding. I'm not sure which package you are pulling in, but the one that people tend to use is the laravel collective one. Here's the docs on that: http://laravelcollective.com/docs/5.1/html#form-model-binding
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community