未定义的数组键“ school_name”使用Flatmap

发布于 2025-01-27 15:33:30 字数 7080 浏览 3 评论 0原文

我正在使用flatmap将键和值相关的值 但这给了我一个错误未定义的数组键“ school_name”我不知道为什么 尽管我确实返回$设置,但它给我带来了这样的

{
setting: {
current_session: "2021-2022",
school_title: "MS",
school_name: "Mora Soft International Schools",
end_first_term: "01-12-2021",
end_second_term: "01-03-2022",
phone: "0123456789",
address: "القاهرة",
school_email: "[email protected]",
logo: "1.jpg"
    }

}

表,这是我的表设置表

Schema::create('settings', function (Blueprint $table) {
        $table->id();
        $table->string('key');
        $table->string('value');
        $table->timestamps();
    });

,这是我的web.php路由

Route::resource('settings', 'SettingController');

,这是我的设置controller

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $collection = setting::all();
    $setting['setting'] = $collection->flatMap(function ($collection){
        return [$collection->key => $collection->value];
    });
    // return $setting;
    return view('pages.settings.index',compact('setting'));
}

,这是我的刀片,可查看表单

<form enctype="multipart/form-data" method="post" action="{{route('settings.update','test')}}">
                @csrf @method('PUT')
                <div class="row">
                    <div class="col-md-6 border-right-2 border-right-blue-400">
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">اسم المدرسة<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <input name="school_name" value="{{ $setting['school_name'] }}" required type="text" class="form-control" placeholder="Name of School">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="current_session" class="col-lg-2 col-form-label font-weight-semibold">العام الحالي<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <select data-placeholder="Choose..." required name="current_session" id="current_session" class="select-search form-control">
                                    <option value=""></option>
                                    @for($y=date('Y', strtotime('- 3 years')); $y<=date('Y', strtotime('+ 1 years')); $y++)
                                        <option {{ ($setting['current_session'] == (($y-=1).'-'.($y+=1))) ? 'selected' : '' }}>{{ ($y-=1).'-'.($y+=1) }}</option>
                                    @endfor
                                </select>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">اسم المدرسة المختصر</label>
                            <div class="col-lg-9">
                                <input name="school_title" value="{{ $setting['school_title'] }}" type="text" class="form-control" placeholder="School Acronym">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">الهاتف</label>
                            <div class="col-lg-9">
                                <input name="phone" value="{{ $setting['phone'] }}" type="text" class="form-control" placeholder="Phone">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">البريد الالكتروني</label>
                            <div class="col-lg-9">
                                <input name="school_email" value="{{ $setting['school_email'] }}" type="email" class="form-control" placeholder="School Email">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">عنوان المدرسة<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <input required name="address" value="{{ $setting['address'] }}" type="text" class="form-control" placeholder="School Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">نهاية الترم الاول </label>
                            <div class="col-lg-9">
                                <input name="end_first_term" value="{{ $setting['end_first_term'] }}" type="text" class="form-control date-pick" placeholder="Date Term Ends">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">نهاية الترم الثاني</label>
                            <div class="col-lg-9">
                                <input name="end_second_term" value="{{ $setting['end_second_term'] }}" type="text" class="form-control date-pick" placeholder="Date Term Ends">
                            </div>
                        </div>

                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">شعار المدرسة</label>
                            <div class="col-lg-9">
                                <div class="mb-3">
                                    <img style="width: 100px" height="100px" src="{{ URL::asset('attachments/logo/'.$setting['logo']) }}" alt="">
                                </div>
                                <input name="logo" accept="image/*" type="file" class="file-input" data-show-caption="false" data-show-upload="false" data-fouc>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <button class="btn btn-success btn-sm nextBtn btn-lg pull-right" type="submit">{{trans('Students_trans.submit')}}</button>
</form>

i am using flatMap to get the keys and the value related together
but it gives me an error Undefined array key "school_name" i dont know why
although i do return $setting it gives me like this

{
setting: {
current_session: "2021-2022",
school_title: "MS",
school_name: "Mora Soft International Schools",
end_first_term: "01-12-2021",
end_second_term: "01-03-2022",
phone: "0123456789",
address: "القاهرة",
school_email: "[email protected]",
logo: "1.jpg"
    }

}

this is my table settings table

Schema::create('settings', function (Blueprint $table) {
        $table->id();
        $table->string('key');
        $table->string('value');
        $table->timestamps();
    });

and this is my web.php route

Route::resource('settings', 'SettingController');

and here is my settingcontroller

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $collection = setting::all();
    $setting['setting'] = $collection->flatMap(function ($collection){
        return [$collection->key => $collection->value];
    });
    // return $setting;
    return view('pages.settings.index',compact('setting'));
}

and here is my blade that views the form

<form enctype="multipart/form-data" method="post" action="{{route('settings.update','test')}}">
                @csrf @method('PUT')
                <div class="row">
                    <div class="col-md-6 border-right-2 border-right-blue-400">
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">اسم المدرسة<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <input name="school_name" value="{{ $setting['school_name'] }}" required type="text" class="form-control" placeholder="Name of School">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="current_session" class="col-lg-2 col-form-label font-weight-semibold">العام الحالي<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <select data-placeholder="Choose..." required name="current_session" id="current_session" class="select-search form-control">
                                    <option value=""></option>
                                    @for($y=date('Y', strtotime('- 3 years')); $y<=date('Y', strtotime('+ 1 years')); $y++)
                                        <option {{ ($setting['current_session'] == (($y-=1).'-'.($y+=1))) ? 'selected' : '' }}>{{ ($y-=1).'-'.($y+=1) }}</option>
                                    @endfor
                                </select>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">اسم المدرسة المختصر</label>
                            <div class="col-lg-9">
                                <input name="school_title" value="{{ $setting['school_title'] }}" type="text" class="form-control" placeholder="School Acronym">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">الهاتف</label>
                            <div class="col-lg-9">
                                <input name="phone" value="{{ $setting['phone'] }}" type="text" class="form-control" placeholder="Phone">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">البريد الالكتروني</label>
                            <div class="col-lg-9">
                                <input name="school_email" value="{{ $setting['school_email'] }}" type="email" class="form-control" placeholder="School Email">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">عنوان المدرسة<span class="text-danger">*</span></label>
                            <div class="col-lg-9">
                                <input required name="address" value="{{ $setting['address'] }}" type="text" class="form-control" placeholder="School Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">نهاية الترم الاول </label>
                            <div class="col-lg-9">
                                <input name="end_first_term" value="{{ $setting['end_first_term'] }}" type="text" class="form-control date-pick" placeholder="Date Term Ends">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">نهاية الترم الثاني</label>
                            <div class="col-lg-9">
                                <input name="end_second_term" value="{{ $setting['end_second_term'] }}" type="text" class="form-control date-pick" placeholder="Date Term Ends">
                            </div>
                        </div>

                        <div class="form-group row">
                            <label class="col-lg-2 col-form-label font-weight-semibold">شعار المدرسة</label>
                            <div class="col-lg-9">
                                <div class="mb-3">
                                    <img style="width: 100px" height="100px" src="{{ URL::asset('attachments/logo/'.$setting['logo']) }}" alt="">
                                </div>
                                <input name="logo" accept="image/*" type="file" class="file-input" data-show-caption="false" data-show-upload="false" data-fouc>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <button class="btn btn-success btn-sm nextBtn btn-lg pull-right" type="submit">{{trans('Students_trans.submit')}}</button>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文