how to make a update page with hasMany relatioins in laravel - php

I'm trying to create a form to update a model which has one to many relations. The structure is like
item => {[arg1, arg2], [arg1, arg2], [arg1, arg2]}
Each related model has two fields to be updated. I used the code below but I don't know how to code the controller side.
#extends('app')
#section('content')
{!! Form::open(array('route' => array('Topic.update', $topic->id), 'method' => 'PATCH')) !!}
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', $topic->title, array('maxlength' => 250)) !!}
#foreach($topic->pairs as $key=>$value)
<div>
{!! Form::label('title', 'Pair ' . $key . ': ') !!}
{!! Form::text('a' + $key, $value->arg1) !!}
{!! Form::text('b' + $key, $value->arg2) !!}
</div>
#endforeach
{!! Form::submit() !!}
{!! Form::close() !!}
#endsection
When I dd($response->all()), it shows below:
array:9 [▼
"_method" => "PATCH"
"_token" => "dInpiSa6O2KIMftFnICQtVM873nUF2Zp2HlzeN4S"
"title" => "Actors and Movies"
0 => "Rain Man"
1 => "Rush Hour"
2 => "Edward Scissorhands"
3 => "Titanic"
4 => "The Devil Wears Prada"
5 => "Fast & Furious"
]
The related items are not pairs but only arg1. So how should I code hasMany model? Thanks

The + sign in PHP is not used for concatenation of strings. It is math operator. To concatenate strings you should use dot (.).
So change your code like this:
Form::text('a' . $key, $value->arg1)
Form::text('b' . $key, $value->arg2)
Because previously you were calculating the sum of a and $key, thats why you get numerical keys in the request.
If you wish to access your pairs in array, from the form request you can change the inputs to be arrays, like this:
Form::text('items[' . $key . '][arg1]', $value->arg1)
Form::text('items[' . $key . '][arg2]', $value->arg2)
With last example the request will look like this:
items => [
[ arg1, arg2 ],
[ arg1, arg2 ],
....
]

Related

How to have multiple products that depend on user input Laravel

I am using the srmklive/laravel-paypal package for my paypal API.
Currently, in my view I have multiple instances of this code block with varying product ids
{!! Form::open(['action' => 'PaypalController#expressCheckout']) !!}
{{ Form::hidden('item', 1) }}
{{ Form::hidden('name', 'm') }}
{{ Form::hidden('price', 25) }}
{{Form::submit('Pay via Paypal', array('class' => 'btn-info btn'))}}
{!! Form::close() !!}
However, when I use this method with my getCart function,
if($product == 1) {
return [
'items' => [
[
'name' => 'Mystery Core',
'price' => 25,
'qty' => 1,
],
],
'return_url' => url('/paypal/express-checkout-success'),
'invoice_id' => config('paypal.invoice_prefix') . '_' . $invoice_id,
'invoice_description' => "Order #" . $invoice_id . " Invoice",
'cancel_url' => url('/'),
'total' => 25,
];
}
}
the variables return null since they're coming from paypal's end, giving me a null error.
"Trying to access array offset on value of type null"
How can I go about having multiple products? I have heard of the addOptions function but I cannot understand it.
Even a different package suggestion is welcome!

Laravel - Select input not keeping old value

I have a piece of code:
{!! Form::select('option_employee_review', old('option_employee_review', $employeeReviews), $employeeReviews, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
It saves the value to the database correct. When i go to edit the item again the select input does not keep the old value that's in the database. How do i make it so that the select input does keep its old value.
$employeeReviews:
[
2843 => "Medewerker review 1"
2849 => "Medewerker review 2"
]
I am not using your syntax, but something like this will do I think.
<option value="{{$channel->id}}" {{ (old("channel_id") == $channel->id ? "selected" : "" ) }}>{{$channel->title}}</option>
Second parameter to select function must be array of options.
Try changing it like this
{!! Form::select('option_employee_review', $employeeReviews, old('option_employee_review', $employeeReviews), ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
Or based on your parent object let's say employee you can try
{!! Form::select('option_employee_review', $employeeReviews, $employee->option_employee_review, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}

Drop down select form in Laravel

may I know what is wrong with my codes? I have three user type name registered in my database however my codes will result to three drop down menus with individual user type name on each.
#foreach($user_types as $usertype)
<div class="form-group">
{!! Form::select('chap_user_type_name', array('chap_user_name' => $usertype), null, ['class' => 'form-control']) !!}
</div>
#endforeach
remove foreach and pass array into select like this
{!! Form::select('chap_user_type_name', $user_types, null, ['class' => 'form-control']) !!}
If $user_types is a collection, you need to use pluck() to build correct array for ::select:
$user_types = UserTypes::pluck('name', 'id');
Then just build select element like this:
{!! Form::select('chap_user_type_name', $usertype, null, ['class' => 'form-control']) !!}
You don't need the foreach above form::select will take care of it

Laravel : Form Data Binding

I need to bind the data to the related field using Form data binding , but the data does not showed on the fields at all.I don't know what the problem exactly that prevent the data to appear.
Controller:
public function edit($id){
$datum = RatingDatum::findorfail(1);
return view('rating.index',compact('datum'));
}
View:
<div class="plan bg-plan">
{!! Form::model($datums,['method' => 'PATCH','action'=>'RatingDataController#update'],$datums->id]) !!}
<div class="radio">
<label>
{!! Form::radio('customize_question_id')!!}{{$datums->value}}
</label>
</div>
<div class="form-group">
{!! Form::label('comment','Comment :') !!}
{!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}
{!! Form::label('reference','Reference:') !!}
{!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
</div>
{!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
{!! Form::close() !!}
</div>
dd($datum);
#attributes: array:15 [▼
"id" => 1
"organisation_id" => 8
"sector_id" => 1
"country_id" => 1
"dimension_id" => 12
"question_angle_id" => 1
"customize_criteria_id" => 33
"customize_question_id" => 7591
"question_weight" => 20
"actual_score" => 75
"value" => "The company has made a formal commitment to promoting voluntary community initiatives and has set up quantitative targets in this regard."
"comment" => ""
"reference" => ""
"created_at" => "2015-12-21 11:28:38"
"updated_at" => "2015-12-21 12:22:25"
]
The data is not showing up in the form is because you are referencing the wrong variable.
You are passing $datum and in the view form, you are accessing $datums. That is the reason why your form is empty.
Solution:
Do either of the following:
Update controller method's $datum to $datums
OR
Update form model $datums to $datum

Laravel not letting me add styles to my Form::text()

This is my code for the form builder, and when I remove the array from the text() everything is fine, but when I add it I get this error
ErrorException in helpers.php line 454: htmlentities() expects
parameter 1 to be string, array given (View:
/Users/samir/Sites/elicant/resources/views/pages/classes.blade.php)
{!! Form::open(array("url" => 'addClass', "method" => 'post')) !!}
{!! Form::text('classname', array('style' => 'width:80%;')) !!}
{!! Form::text('classcolour') !!}
{!! Form::submit("Add") !!}
{!! Form::close() !!}
What have I done wrong?
Form::text has 3 parameters: $name, $value, array $options = array()
So to make your code working:
{!! Form::text('classname', null, array('style' => 'width:80%;')) !!}

Categories