Cannot display values when editing form in laravel 5 - laravel

There is no output in the form when clicking a value. But I can see the values when using the return function. Here is my code:
ReportController
public function edit($id)
{
$crime_edit = CrimeReport::findOrFail($id);
$victim_info = VictimProfile::findOrFail($id);
return $victim_info;
//return $victim_info->firstname;
//return $victim_info;
$display_crime_type = CrimeType::lists('crime_type','id');
$display_crime_name = CrimeName::lists('crime_description','id');
return view('crimereports.edit',compact('crime_edit','victim_info','display_crime_name,'display_crime_type'));
}
edit view page
{!! Form::model($crime_edit,['method' =>'PATCH','route'=>'crime_reports.update',$crime_edit->id],'class'=>'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('victim_name', 'Victim Name',['class'=>'col-md-3 control-label']) !!}
<div class="col-md-3">
{!! Form::text('victim_name', null, ['class' => 'form-control','placeholder' => 'Firstname']) !!}
</div>
<div class="col-md-2">
{!! Form::text('v_middle_name', null, ['class' => 'form-control','placeholder' => 'Middlename']) !!}
</div>
<div class="col-md-3">
{!! Form::text('v_last_name', null, ['class' => 'form-control','placeholder' => 'Last Name']) !!}
</div>
</div>
{!! Form::close() !!}
Am I missing something?

The null is the default value. I think Form::model is suppose to bind the values, but have you tried removing the null?
I don't use Form::model, but as a default value I always put old('field_name', $model->field_name). The old function will look into both GET and POST and if a key matches that'll be shown. If that doesn't exist, it'll use the second value.

Based on your debug output statements ("return $victim_info;"), it looks like you are trying to bind the form to the $crime_edit model while accessing the values from the $victim_info model. You can not bind a form to two different models at once, so if the blank victim name fields are not attributes of the $crime_edit model, your current implementation will not work.
You will need to either explicitly add $victim_info->victim_name, etc. in place of the 'null' values, or bind the form to the $victim_info model instead of the $crime_edit model.
If victim_name, v_middle_name, and v_last_name are attributes of the $crime_edit model, then I have no idea.

Related

Add parameter to submit form laravel

here you can see my form where I put in a username and have a hidden idgroup field.
{!! Form::open(array('route'=>'create.invitation')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
<input type="hidden" name="idgroup" value="{{$group}}"/>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
After that this route leads me to my controller function
Route::post('invitation/show', 'InvitationController#create')->name('create.invitation');
How can I add the username and the idgroup to my url?
My problem is now when I click submit I get back this url http://127.0.0.1:8000/invitation/create and when I click enter to the url line I get an error no message because no parameter will pass to the function.
Add. Here is the function
public function create(Request $request)
{
$request->validate([
'username' => [
'required', 'alpha_num', new ExistingUser, new UserNotAdmin
]
]);
$username = $request->username;
$iduser = User::where('name', $username)->select('id')->first();
$group = $request->idgroup;
return view('invitation.overview')->with('group', $group)->with('iduser', $iduser);
}
You cannot pass parameter inside POST body without submitting a form.
But you can try to allow both GET or POST by using any() for the route, so you can test the page around.
Route::any('invitation/show', 'InvitationController#create')->name('create.invitation');
And then, you can try pass variable through queries inside URL
http://127.0.0.1:8000/invitation/create?username=something&idgroup=1

Laravel error "Missing required parameters for Route" when I use a form with a foreach loop

Missing required parameters for [Route: templates.answers.store] [URI: templates/{template}/answers]. (View: D:\Applications\xampp\htdocs\clientpad\resources\views\templates\answers.blade.php)
I am having the above error when I try and use a form with my foreach loop. I am not even sure why this is happening, maybe because I am new at Laravel. But this error goes away once I get rid of the AnswerController#store from the Eloquent form. It is possible I am doing this whole form wrong.
Here is what I want to do: A user made a template with questions, on the click of use button which goes to this url: http://clientpad.test/templates/{id}/answers they see their made questions which are shown with a foreach loop. Around it a Form is made so a user can answer the questions made. The form and answer field shows when I delete the action AnswerController#store, otherwise I get the above error.
Here is the code:
AnswerController:
public function index(Template $template, Question $question)
{
$questions = $template->questions->mapWithKeys(function($question){
return [$question->id => $question->question];
});
return view('templates.answers')->with('template', $template)->with('questions',$questions);
}
public function store(Request $request, Question $question, Answer $answer)
{
$answers = new Answer;
$answers->answer = $request->input('answer');
$answers->question_id = $request->input('question_id'); //current template id
$question->answers()->save($answers);
dd($question);
return redirect('/dashboard')->with('success', 'Your Question Was Successfully Created');
}
answers.blade.php
{!! Form::open(['action' => 'AnswersController#store', 'method' => 'POST']) !!}
#foreach ($questions as $question) <!-- Index counts the questions shown -->
<div class="panel panel-default">
<div class="panel-body">
<p class="pull-left question2"> {{$question}}</p>
<div class="form-group answer">
{{Form::label('', '')}}
{{Form::text('answer', '', ['class' => 'form-control', 'placeholder' => 'Type in your answer'])}}
</div>
</div>
</div>
#endforeach
<hr>
{{Form::submit('Save', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
And I am just using the resource in routes.
Your are calling a route templates/{id}/answers in your Blade view that is missing the {id} parameter. Reading the error thoroughly will help you understand.
Instead of writing:
Form::open(['action' => 'AnswersController#store', 'method' => 'POST'])
You write:
Form::open(['action' => ['AnswersController#store', $template_id], 'method' => 'POST'])
The $template_id will fill the {id} in your route URL templates/{id}/answers.

Laravel 5.4: Using eloquent and fillable field to update multiple model with multiple table

I have two models Driver and DriverCar
I need to display the field values by model let me show you my code of my form header
Note that Driver are related to DriverCar by driver_id
{!! Form::model($driver, [
'route'=>['drivers.update', $driver],
'method'=>'PATCH',
'class'=>'form-horizontal'
]) !!}
now in my form fields I have
<div class="form-group">
{!! Form::label('name', trans('interface.DriverName'), ['class'=>'col-sm-2 control-label']) !!}
<div class="col-sm-10">
{!! Form::text('name', null, ['class'=>'form-control', 'placeholder'=>trans('interface.DriverName')]) !!}
</div>
</div>
which updated successfully when I update. but the DriverCar fields didn't get the fillable values if it's set to null I had to get it by relations like $driver->driverCar->car_model
<div class="form-group">
{!! Form::label('car_model', trans('interface.carModel'), ['class'=>'col-sm-2 control-label']) !!}
<div class="col-sm-10">
{!! Form::text('car_model', $driver->driverCar->car_model, ['class'=>'form-control', 'placeholder'=>trans('interface.carModel')]) !!}
</div>
</div>
is there is any way to get it fillable without giving it relations like that $driver->driverCar->car_model?
second
here is my controller I try to updated the two tables at once
public function update( Request $request, Driver $driver, DriverCar $driverCar ) {
//dd( $driverCar );
$input = $request->all();
$driver->fill( $input )->save();
$driverCar->fill( $input )->save();
return redirect()->route( 'drivers.edit', $driver );
}
in controller too I can't update the two tables it just update the Driver but not touching the DriverCar
any Guide please.
You're not passing driverCar ID to the update controller method, so change code to something like this:
public function update( Request $request, Driver $driver)
{
$input = $request->all();
$driver->fill($input)->save();
$driver->driverCar()->first()->fill($input)->save();
return redirect()->route('drivers.edit', $driver);
}

how can I fill a form with information I want to edit in laravel 5.5?

I'm new in laravel and I want to make a form to edit data from my database so the user just have to change some fields, I would be glad if somebody could give me an example.
I'm using laravel 5.5 and mysql
Please check out this edit example in laravel -
In Routes
Route::get('PartnerType/edit/{id}', 'PartnerTypeController#edit');
Route::post('PartnerType/update', 'PartnerTypeController#update');
In controller,
public function edit($id){
$data['propertyType'] = PropertyType::where('id', $id)->first();
return view('propertyType.edit', $data);
}
public function update(Request $request){
//Validate user inputs
$validator = \Validator::make($request->all(), ['name' => 'required']);
//Check whether validation is failed or passed
if($validator->fails()){
//Redirect back with validation errors
return redirect()->back()->withErrors($validator->errors())->withInput();
}
//Save Details
$propertyType = PartnerType::where('id', $request->id)->first();
$propertyType->name = $request->name;
$propertyType->save();
//Redirect with success message
return redirect()->to('manage/PartnerType/show')->with('success', 'PartnerType updated successfully');
}
In view ,
{!! Form::model($propertyType, array('url'=>array('manage/propertyType/update'), 'method' => 'POST', 'id' => 'edit_propertyType_form')) !!}
<div class="form-body pb0">
<div class="form-group">
<input type="hidden" name="id" value="{{$propertyType->id}}">
{!! Form::label('name', 'Name*') !!}
<div class="input-group">
{!! Form::text('name', $propertyType->name, array('class' => 'form-control','id' => 'name', 'placeholder' => 'Name')) !!}
</div>
</div>
<div class="status-label">
{!! Form::submit('Submit',array('class' => 'btn blue')) !!}
</div>
{!! Form::close() !!}
The question is kind of vague as what you intend to do. Have you tried something out? if so, what is it?
Let's start from the beginning:
As far as I can tell right now (need more details) you need:
A view file (example: edit.blade.php) which will be inside the folder resources/views and inside the folder you have to create with the proper name (example: /resources/views/something/edit.blade.php)
Then create the form where the user is going to edit the information from the database.
Go to your routes folder and web.php file to set the routes you will need in order to PUT/PATCH the new information from the form to de DB.
Do you have your database configured already?
Please send more information, and I will answer more in detail on what you need or you can go to the official documentation for further information as well.

How to save post form in Laravel-4

I am creating parent email form in this form parent insert his email id.
I facing problem is when I submit form that showing error ,Method [save] does not exist
and my code is
registration form
<div class="form">
{{ Form::open(array('url' => '/api/v1/parents/registration_step_2', 'class' => "worldoo-form form form-horizontal", 'id' => "signupForm", 'method' => "post" )) }}
<div class="form-group ">
{{ Form::label('cemail', 'Please enter your parents e-mail:', array('class' => "control-label"));}}
<div class="">
{{ Form::email('cemail', '', array('class' => "form-control", 'id' => "cemail"));}}
<i class="sprite success form-control-feedback"></i> </div>
<h5 class="regular-font text-left">Your parents will need to activate your account before you can access worldoo.</h5>
</div>
<div class="form-group">
<div class="text-center">
{{Form::submit('Next', array('class' => "btn btn-primary"));}}
</div>
</div>
{{ Form::close() }}
</div>
my controller
public function registrationStepTwo()
{
$cemail = $_REQUEST['cemail'];
if($cemail != '')
{
$parent = new Parent;
$parent->email = $cemail;
$parent->save();
}
}
I had the save issues, with the exact same class name "Parent" at it also throws
Method [save] does not exist
The reason you can save the $parent model data is because of the name that you have given to the model as
Parent
Change the model name to something else and it will work as expected. Looks like the class Parent must be laravel's system class and we cannot give that name to our custom classes.
your Model is not set up correctly you find these in app/models/ there needs to be a Parent.php file that defines how laravel should use your information in the database. Eloquent Models - Laravel 4 Documentation
I have tested, this code is working properly. May be try composer update.
composer update

Resources