Laravel 5函数值修改及HTML Collective表单经纬度赋值问题咨询
Hey there! Let's break down your Laravel questions step by step:
1. How to modify values via functions in Laravel 5
The approach depends on what kind of value you're modifying—here are the most common scenarios:
Modifying model attribute values
- Use Mutators (Setters): If you want values to automatically transform when saved to the database, define a mutator on your model. For example, to uppercase a
usernamefield:
// In your Eloquent model public function setUsernameAttribute($value) { $this->attributes['username'] = strtoupper($value); }
Now whenever you assign $user->username = 'jane_doe';, it'll be stored as JANE_DOE.
- Custom model methods: For explicit value updates, create a dedicated method on your model:
public function markAsCompleted() { $this->status = 'completed'; $this->completed_at = now(); $this->save(); }
Call it like $task->markAsCompleted(); to update the values and save in one go.
Modifying request/input values
If you need to tweak incoming form data in a controller, use the merge method:
public function update(Request $request, Post $post) { // Modify the slug before validation/saving $request->merge([ 'slug' => str_slug($request->title) ]); // Proceed with validation and saving $post->update($request->validated()); }
Modifying general variables
For regular variables in helper functions or controllers, manipulate them like standard PHP:
function formatDate($rawDate) { return Carbon::parse($rawDate)->format('M d, Y'); } $formattedDate = formatDate('2024-05-20'); // Returns "May 20, 2024"
2. Updating HTML Collective form inputs with Google Maps lat/lng
Let's complete your codeAddress function and add the logic to update your form fields. First, make sure your HTML Collective inputs have unique IDs so JavaScript can target them:
{!! Form::text('latitude', $yourModel->latitude, ['id' => 'lat-input']) !!} {!! Form::text('longitude', $yourModel->longitude, ['id' => 'lng-input']) !!}
Then update your codeAddress function to set the input values once you get the geocode results:
function codeAddress(address) { const geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': address }, function(results, status) { if (status == 'OK') { // Extract the lat/lng values (note these are methods, not properties!) const latitude = results[0].geometry.location.lat(); const longitude = results[0].geometry.location.lng(); // Update the form inputs document.getElementById('lat-input').value = latitude; document.getElementById('lng-input').value = longitude; // If you prefer jQuery, you can use this instead: // $('#lat-input').val(latitude); // $('#lng-input').val(longitude); } else { alert('Geocoding failed: ' + status); } }); }
Quick tips:
- Always confirm your input IDs match what you're targeting in JavaScript.
- Double-check that you're calling
lat()andlng()(with parentheses) on the Google Maps location object—they're not static properties! - If you don't want to use IDs, you can target inputs by name:
document.querySelector('input[name="latitude"]').value = latitude;
内容的提问来源于stack exchange,提问作者Yosua Michael

