【问题标题】:Laravel update field based on select in a belongsTo relationshipLaravel 基于 belongsTo 关系中的选择更新字段
【发布时间】:2021-06-13 14:25:10
【问题描述】:

来自this article 发现了一种创建动态选择框的有趣方法。我可以通过添加到 routes/web.php 来适应我的情况

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

并生成动态选择框,其值为organization_id,如下所示在我的控制器中

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]);
    }
}

这是ajax-select.blade.php

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

和 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>

这是结果


问题是,我想要的是组织的名称而不是其 ID。

鉴于在我的 CampusOrganization 模型中我有

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

我可以从那里得到组织的名称。

所以,我已将 ajax-select.blade.php 改编为

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

和控制器到

/**
 * 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]);
    }
}

但现在我得到的只是(没有选择)

我也测试过

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

但也没有给出任何结果。


编辑

如果我返回$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]);
    }
}

然后会在响应中看到

[
  {
    "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"
    }
  }
]

删除return $campusorganizations;后,响应将是

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


请注意,通过回答这个问题,您也可以回答(我可以帮助葡萄牙语和西班牙语)

【问题讨论】:

    标签: php ajax laravel eloquent selection


    【解决方案1】:

    您正在使用 AJAX,因此您需要在 myformAjax 函数中预先加载 organization 关系。

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

    还有,而不是

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

    你应该这样命名变量

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

    那就够了

    【讨论】:

    • 它仍然不会显示任何结果
    • 您在CampusOrganizationOrganization 之间设置了正确的关系,名为organization
    • 是的,我已经设置好了。在同一页面中,我之前使用的是带有组织名称的非动态选择框
    • 在您的问题中包含 $data 的转储结果。这可能会有所帮助。
    • 刚刚在问题中添加了更多信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2019-05-26
    • 2021-12-21
    • 2019-03-04
    • 2022-10-05
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多