laravel - firstOrCreate 和 firstOrNew 在 laravel 中不起作用
问题描述
educationals
我在表中有一行
我的表格就像这个演示
当我单击添加其他学术记录时,它会添加新行,当我填写新数据时,在最后一步中,当我单击提交时,数据库中没有任何反应。
第一种方法:
foreach ($request->field as $key => $value) {
$educational = Educational::firstOrNew(['user_id' => auth()->id()]);
$educational->user_id = auth()->id();
$educational->grade_id = $request->grade_id[$key];
$educational->field = $request->field[$key];
$educational->institution_id = $request->institution_id[$key];
$educational->branch = $request->branch[$key];
$educational->institution_education = $request->institution_education[$key];
$educational->gpa = $request->gpa[$key];
$educational->nation_id = $request->nation_id[$key];
$educational->province_id = $request->province_id[$key];
$educational->town_id = $request->town_id[$key];
$educational->province_name = $request->province_name[$key];
$educational->town_name = $request->town_name[$key];
$educational->entrance = $request->entrance[$key];
$educational->graduate = $request->graduate[$key];
$educational->currently_studying = $request->has("currently_studying.$key");
$educational->save();
}
第二种方法:
foreach ($request->field as $key => $value) {
try {
$educational = new Educational;
$educational->user_id = auth()->id();
$educational->grade_id = $request->grade_id[$key];
$educational->field = $request->field[$key];
$educational->institution_id = $request->institution_id[$key];
$educational->branch = $request->branch[$key];
$educational->institution_education = $request->institution_education[$key];
$educational->gpa = $request->gpa[$key];
$educational->nation_id = $request->nation_id[$key];
$educational->province_id = $request->province_id[$key];
$educational->town_id = $request->town_id[$key];
$educational->province_name = $request->province_name[$key];
$educational->town_name = $request->town_name[$key];
$educational->entrance = $request->entrance[$key];
$educational->graduate = $request->graduate[$key];
$educational->currently_studying = $request->has("currently_studying.$key");
$educational->save();
} catch (Exception $e) {
dd($e->getMessage());
}
}
第三种方法
foreach ($request->field as $key => $value) {
Educational::firstOrCreate([
'user_id' => auth()->id(),
'grade_id' => $request->grade_id[$key],
'field' => $request->field[$key],
'institution_id' => $request->institution_id[$key],
'branch' => $request->branch[$key],
'institution_education' => $request->institution_education[$key],
'gpa' => $request->gpa[$key],
'nation_id' => $request->nation_id[$key],
'province_id' => $request->province_id[$key],
'town_id' => $request->town_id[$key],
'province_name' => $request->province_name[$key],
'town_name' => $request->town_name[$key],
'entrance' => $request->entrance[$key],
'graduate' => $request->graduate[$key],
'currently_studying' => $request->has("currently_studying.$key"),
]);
}
第四种方法
foreach ($request->field as $key => $value) {
try {
$educational = Educational::firstOrCreate(['user_id' => auth()->id()]);
$educational->user_id = auth()->id();
$educational->grade_id = $request->grade_id[$key];
$educational->field = $request->field[$key];
$educational->institution_id = $request->institution_id[$key];
$educational->branch = $request->branch[$key];
$educational->institution_education = $request->institution_education[$key];
$educational->gpa = $request->gpa[$key];
$educational->nation_id = $request->nation_id[$key];
$educational->province_id = $request->province_id[$key];
$educational->town_id = $request->town_id[$key];
$educational->province_name = $request->province_name[$key];
$educational->town_name = $request->town_name[$key];
$educational->entrance = $request->entrance[$key];
$educational->graduate = $request->graduate[$key];
$educational->currently_studying = $request->has("currently_studying.$key");
$educational->save();
} catch (Exception $e) {
dd($e->getMessage());
}
}
第五种方法
public function update(Request $request) {
$user = auth()->user();
$user->image = $path;
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->job_title = $request->job_title;
$user->gender = $request->gender;
$user->marital = $request->marital;
$user->soldier_id = $request->soldier_id;
$user->birth_date_day = $request->birth_date_day;
$user->month_id = $request->month_id;
$user->birth_date_year = $request->birth_date_year;
$user->email = $request->email;
$user->mobile = $request->mobile;
$user->telephone = $request->telephone;
$user->website = $request->website;
$user->save();
foreach ($request->field as $key => $value) {
if ($user->where('user_id', auth()->id())) {
$educational = $user->findOrFail('user_id');
} else {
$educational = new Educational;
}
$educational->user_id = auth()->id();
$educational->grade_id = $request->grade_id[$key];
$educational->field = $request->field[$key];
$educational->institution_id = $request->institution_id[$key];
$educational->branch = $request->branch[$key];
$educational->institution_education = $request->institution_education[$key];
$educational->gpa = $request->gpa[$key];
$educational->land_id = $request->land_id[$key];
$educational->province_id = $request->province_id[$key];
$educational->town_id = $request->town_id[$key];
$educational->province_name = $request->province_name[$key];
$educational->town_name = $request->town_name[$key];
$educational->entrance = $request->entrance[$key];
$educational->graduate = $request->graduate[$key];
$educational->currently_studying = $request->has("currently_studying.$key");
$educational->save();
}
}
我的刀片
<div class="container-fluid">
<b>سوابق تحصیلی</b>
<div class="mb-2 bg-white p-3 mb-5">
<div class="row p-3" id="showStudy">
</div>
<div class="w-100 text-center">
<p>سوابق تحصیلی دیگری را اضافه کنید.</p>
<a id="addStudy" class="bg-primary text-white pt-2 pb-2 pl-3 pr-3 rounded-circle cursor-pointer">
<i class="fas fa-plus"></i>
</a>
</div>
</div>
</div>
<script>
$(document).ready(function() {
let countStudy = 1;
$('#addStudy').click(function () {
countStudy++;
dynamicStudy(countStudy);
});
//dynamicStudy(countStudy);
function dynamicStudy(number) {
let html = '' +
'<div class="col-md-12 position-relative">\n' +
'<i class="fas fa-times text-danger position-absolute"></i>' +
'<div class="row">\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="grade_id">مقطع</label>\n' +
'<select id="grade_id" name="grade_id[]" class="form-control">\n' +
'@foreach($grades as $grade)\n' +
'<option value="{{ $grade->id }}">\n' +
'{{ $grade->name }}\n' +
'</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="field">رشته تحصیلی</label>\n' +
'<input id="field" name="field[]" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="branch">گرایش/تخصص</label>\n' +
'<input id="branch" name="branch[]" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="institution_id">نوع موسسه</label>\n' +
'<select id="institution_id" name="institution_id[]" class="form-control">\n' +
'@foreach($institutions as $institution)\n' +
'<option value="{{ $institution->id }}">{{ $institution->name }}</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="institution_education">عنوان موسسه آموزشی</label>\n' +
'<input id="institution_education" name="institution_education[]" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="gpa">معدل</label>\n' +
'<input id="gpa" name="gpa[]" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="nation_id">کشور</label>\n' +
'<select id="nation_id" name="nation_id[]" class="form-control">\n' +
'<option></option>\n' +
'@foreach($countries as $country)\n' +
'<option value="{{ $country->id }}">{{ $country->name }}</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="province_id">استان</label>\n' +
'<select id="province_id" name="province_id[]" class="form-control">\n' +
'</select>\n' +
'<input name="province_name[]" id="province_id" class="form-control d-none">\n'+
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="town_id">شهر</label>\n' +
'<select id="town_id" name="town_id[]" class="form-control">\n' +
'</select>\n' +
'<input name="town_name[]" id="town_id" class="form-control d-none">\n'+
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="entrance">ورود</label>\n' +
'<input name="entrance[]" id="entrance" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="graduate">فراغت از تحصیل</label>\n' +
'<input name="graduate[]" id="graduate" class="form-control">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<div class="custom-control custom-checkbox my-4 mr-sm-2">\n' +
'<input type="checkbox" class="custom-control-input" id="currently_studying" name="currently_studying[]">\n' +
'<label class="custom-control-label" for="currently_studying">در حال تحصیل</label>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>';
$('#showStudy').append(html);
}
showDynamicStudy();
function showDynamicStudy(){
let html = '' +
'@foreach(auth()->user()->educationals as $educational)\n'+
'<div class="col-md-12 position-relative">\n' +
'<i class="fas fa-times text-danger position-absolute"></i>' +
'<div class="row">\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="grade_id">مقطع</label>\n' +
'<select id="grade_id" name="grade_id[]" class="form-control">\n' +
'@foreach($grades as $grade)\n' +
'<option value="{{ $grade->id }}" {{ $educational->grade_id == $grade->id ? 'selected' : '' }}>\n' +
'{{ $grade->name }}\n' +
'</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="field">رشته تحصیلی</label>\n' +
'<input id="field" name="field[]" class="form-control" value="{{ $educational->field }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="branch">گرایش/تخصص</label>\n' +
'<input id="branch" name="branch[]" class="form-control" value="{{ $educational->branch }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="institution_id">نوع موسسه</label>\n' +
'<select id="institution_id" name="institution_id[]" class="form-control">\n' +
'@foreach($institutions as $institution)\n' +
'<option value="{{ $institution->id }}" {{ $educational->institution_id == $institution->id ? 'selected' : '' }}>{{ $institution->name }}</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="institution_education">عنوان موسسه آموزشی</label>\n' +
'<input id="institution_education" name="institution_education[]" class="form-control" value="{{ $educational->institution_education }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="gpa">معدل</label>\n' +
'<input id="gpa" name="gpa[]" class="form-control" value="{{ $educational->gpa }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="nation_id">کشور</label>\n' +
'<select id="nation_id" name="nation_id[]" class="form-control">\n' +
'<option></option>\n' +
'@foreach($countries as $country)\n' +
'<option value="{{ $country->id }}" {{ $educational->nation_id == $country->id ? 'selected' : '' }}>\n' +
'{{ $country->name }}\n'+
'</option>\n' +
'@endforeach\n' +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="province_id">استان</label>\n' +
'<select id="province_id" name="province_id[]" class="form-control">\n' +
'@foreach($states as $state)\n' +
'<option value="{{ $state->id }}" {{ $educational->province_id == $state->id ? 'selected' : '' }}>\n' +
'{{ $state->name }}\n' +
'</option>\n' +
'@endforeach\n' +
'</select>\n' +
'<input name="province_name[]" id="province_id" class="form-control d-none" value="{{ $educational->province_name }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="town_id">شهر</label>\n' +
'<select id="town_id" name="town_id[]" class="form-control">\n' +
'@foreach($cities as $city)\n' +
'<option value="{{ $city->id }}" {{ $educational->town_id == $city->id ? 'selected' : '' }}>\n' +
'{{ $city->name }}\n' +
'</option>\n' +
'@endforeach\n' +
'</select>\n' +
'<input name="town_name[]" id="town_id" class="form-control d-none" value="{{ $educational->town_name }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="entrance">ورود</label>\n' +
'<input name="entrance[]" id="entrance" class="form-control" value="{{ $educational->entrance }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<label for="graduate">فراغت از تحصیل</label>\n' +
'<input name="graduate[]" id="graduate" class="form-control" value="{{ $educational->graduate }}">\n' +
'</div>\n' +
'</div>\n' +
'<div class="col-md-2">\n' +
'<div class="form-group">\n' +
'<div class="custom-control custom-checkbox my-4 mr-sm-2">\n' +
'<input type="checkbox" class="custom-control-input" id="currently_studying" name="currently_studying[]" {{ $educational->currently_studying == 1 ? 'checked' : '' }}>\n' +
'<label class="custom-control-label" for="currently_studying">در حال تحصیل</label>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'@endforeach';
$('#showStudy').append(html);
}
});
</script>
表迁移
public function up()
{
Schema::create('educationals', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->bigInteger('grade_id')->unsigned()->nullable();
$table->foreign('grade_id')->references('id')->on('grades')->onDelete('cascade');
$table->string('field')->nullable();
$table->bigInteger('institution_id')->unsigned()->nullable();
$table->foreign('institution_id')->references('id')->on('institutions')->onDelete('cascade');
$table->string('branch')->nullable();
$table->string('institution_education')->nullable();
$table->string('gpa')->nullable();
$table->bigInteger('country_id')->unsigned()->nullable();
$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
$table->bigInteger('state_id')->unsigned()->nullable();
$table->foreign('state_id')->references('id')->on('states')->onDelete('cascade');
$table->bigInteger('city_id')->unsigned()->nullable();
$table->foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
$table->string('state_name')->nullable();
$table->string('city_name')->nullable();
$table->string('entrance')->nullable();
$table->string('graduate')->nullable();
$table->boolean('currently_studying')->nullable();
$table->timestamps();
});
}
我使用了所有五种方法,但都没有奏效。并且不存储在数据库中。
解决方案
对于第一种和第三种方法,请确保您已在模型中设置了受保护的属性:
protected $guarded = [];
https://laravel.com/docs/8.x/eloquent#mass-assignment
在第三种方法的情况下,你用错了,它需要2个数组,假设你要搜索user_id,那么:
Educational::firstOrCreate(['user_id' => auth()->id()], [
'grade_id' => $request->grade_id[$key],
'field' => $request->field[$key],
'institution_id' => $request->institution_id[$key],
'branch' => $request->branch[$key],
'institution_education' => $request->institution_education[$key],
'gpa' => $request->gpa[$key],
'nation_id' => $request->nation_id[$key],
'province_id' => $request->province_id[$key],
'town_id' => $request->town_id[$key],
'province_name' => $request->province_name[$key],
'town_name' => $request->town_name[$key],
'entrance' => $request->entrance[$key],
'graduate' => $request->graduate[$key],
'currently_studying' => $request->has("currently_studying.$key"),
]);
https://laravel.com/docs/8.x/eloquent#other-creation-methods
推荐阅读
- mysql - Zabbix安装过程中的问题
- python - 在 google colab 中使用 bing_image_downloader 并行处理图像抓取
- java - 在java中注册一个帐户并登录
- simulation - 什么时候不重建轮子?
- sql - 根据唯一 id 从连接的多个表中汇总 SQL Server 中的某些列
- swift - 从 AVCapturePhotoOutput 裁剪 CGRect (resizeAspectFill)
- c# - C# ImageMagick 使用自定义字体
- c++ - 您应该使用包装类还是继承来扩展用户的类
- scala - 无法与 Scala 中的 avro 模式演变保持向后兼容性
- python - 'user_image' 属性没有与之关联的文件 | 姜戈