John Davidson

php - Laravel update field based on select in a belongsTo relationship

0 comments
Message:


From this article found an interesting way to create dynamic selection boxes. I was able to adapt it for my case by adding to routes/web.php


Route::post('select-ajax', ['as'=>'select-ajax','uses'=>'SpecificController@myformAjax']);

and generate dynamic selection boxes which would have as values the organization_id as the following was in my controller


public function myformAjax(Request $request)
{
if($request->ajax()){
$campusorganizations = DB::table('campus_organizations')->where('campus_id',$request->campus_id)->pluck("organization_id","id")->all();
$data = view('ajax-select',compact('campusorganizations'))->render();
return response()->json(['options'=>$data]);
}
}

and this was the ajax-select.blade.php


@if(!empty($campusorganizations))
@foreach($campusorganizations as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
@endif

and the AJAX


<script type="text/javascript">
$("select[name='campus_id']").change(function(){
var campus_id = $(this).val();
var token = $("input[name='_token']").val();
$.ajax({
url: "<?php echo route('select-ajax') ?>",
method: 'POST',
data: {campus_id:campus_id, _token:token},
success: function(data) {
$("select[name='campus_organization_id'").html('');
$("select[name='campus_organization_id'").html(data.options);
}
});
});
</script>

this is the result





Thing is, I want to have the name of the organization instead of its ID.


Given that in my CampusOrganization model I have


/**
* Get the organization
*
* @return \Organization
*/
public function organization()
{
return $this->belongsTo(Organization::class);
}

from where I can get the name of the organization.


So, I've adapted the ajax-select.blade.php to


<option value=''>None</option>
@if(!empty($campusorganizations))
@foreach($campusorganizations as $campusorganization)
<option value="{{ $campusorganization->id }}">{{ $campusorganization->organization->name }}</option>
@endforeach
@endif

and the controller to


/**
* Get Ajax Request and return Data
*
* @return \Illuminate\Http\Response
*/
public function myformAjax(Request $request)
{
if($request->ajax()){

$campusorganizations = CampusOrganization::where('campus_id',$request->campus_id)->get();

$data = view('rooms.ajax-select', $campusorganizations)->render();

return response()->json(['options'=>$data]);
}
}

but now all I get is (with no options)



I've also tested with


$campusorganizations = CampusOrganization::with('organization')->where('campus_id',$request->campus_id)->get();

but no results were given as well.




Edit


If I return $campusorganizations


/**
* Get Ajax Request and return Data
*
* @return \Illuminate\Http\Response
*/
public function myformAjax(Request $request)
{
if($request->ajax()){

$campusorganizations = CampusOrganization::with('organization:id,name')
->where('campus_id',$request->campus_id)
->with('organization')
->get();

return $campusorganizations;

$data = view('rooms.ajax-select',$campusorganizations)->render();

return response()->json(['options'=>$data]);
}
}

then will see in the response


[
{
"id": 11,
"campus_add_users": 0,
"campus_id": 4,
"organization_id": 4,
"created_at": "2021-03-12T20:15:42.000000Z",
"updated_at": "2021-03-12T20:15:42.000000Z",
"organization": {
"id": 4,
"name": "Aldi",
"slug": "aldi",
"is_visible": 1,
"user_id": 2,
"created_at": "2021-03-12T20:15:38.000000Z",
"updated_at": "2021-03-12T20:15:38.000000Z"
}
}
]


After removing return $campusorganizations;, then the response will be


{"options":"<option value=''>None<\/option>\r\n"}




Note that by answering this question you'll also be able to answer (I can help with both the portuguese and spanish)




My Answer:
To display the organization name in the select options, you need to make sure that the relationship is loaded properly in your query. You were on the right track with using `with('organization')`, but you need to make sure to select the necessary fields from the organization table.

Here's how you can modify your controller method:

php
public function myformAjax(Request $request)
{
if($request->ajax()){

$campusorganizations = CampusOrganization::with('organization:id,name')
->where('campus_id', $request->campus_id)
->get();

$data = view('rooms.ajax-select', compact('campusorganizations'))->render();

return response()->json(['options' => $data]);
}
}


In your blade file, you can access the organization name like this:

html
@if(!empty($campusorganizations))
@foreach($campusorganizations as $campusorganization)

@endforeach
@endif


Make sure that the `organization` relationship is correctly defined in your `CampusOrganization` model.

If you are still not seeing any options, you can also check the network tab in your browser's developer tools to see the response from the AJAX request and debug any potential issues with the data being returned.

Rate this post

3 of 5 based on 3781 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us