gpt4 book ai didi

php - create() 函数不起作用且不输出错误 (Laravel)

转载 作者:搜寻专家 更新时间:2023-10-30 23:25:46 25 4
gpt4 key购买 nike

我想将数据插入数据库,但是当我运行函数时不起作用并且不输出错误,数据没有插入数据库但没有显示错误?我为什么要这样做?

public function index(){
$data_subgroup = \App\Subgroup::all();
$data_category = \App\Module::all();
return view('/dashboard/subgroup', ['data_subgroup' => $data_subgroup,
'data_category' => $data_category,
]);
}

public function create(Request $request){

$this->validate($request, [
'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
'subgroup_name' => 'required',
'module_id' => 'required'
]);

$subgroup_logo = $request->file('subgroup_logo');
$filename ='subgroups/' . time() . '.' . $subgroup_logo->getClientOriginalExtension(); //Memecah Filename
Image::make($subgroup_logo)->resize(200, 200)->save( public_path('/uploads/' . $filename)); //Resize dan Save Avatar ke Database

Subgroup::create([
'subgroup_logo' => $filename,
'subgroup_name' => $request->subgroup_name,
'module_id' => $request->module_id
]);
return redirect()->back();
}

这是我的路线代码:

    Route::get('/dashboard/subgroup', 'Dashboard\SubgroupController@index');
Route::post('/dashboard/subgroup/create', 'Dashboard\SubgroupController@create');

这是我的模型代码:

class Subgroup extends Model{
protected $table = 'subgroup';
public $timestamps = false;
protected $primaryKey = 'subgroup_id';

protected $fillable = ['subgroup_id', 'subgroup_name', 'subgroup_logo', 'module_id'];

public function module(){
return $this->belongsTo('App\Module' , 'module_id');
}

更新:我认为错误来自从选择选项插入数据,

我改变了:

$this->validate($request, [
'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
'subgroup_name' => 'required',
'module_id' => 'required'
]);

到:

Validator::make($request->all(), [
'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
'subgroup_name' => 'required',
'module_id' => 'integer|required'
]);

我有选择选项 View :

<label for="category">Select Category</label>
<select class="form-control form-control-sm">
@foreach($data_category as $category)
<option id="module_id" name="module_id" value="{{$category->module_id}}">
{{$category->module_name}}</option>
@endforeach
</select>

我有错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module_id' cannot be null (SQL: insert into subgroup (subgroup_logo, subgroup_name, module_id) values (subgroups/1570166504.png, UNSIKA, ?))

最佳答案

将名称属性设置为您的选择输入。您在 option 标签中设置了错误

<select class="form-control form-control-sm" name="module_id">
@foreach($data_category as $category)
<option id="module_id" name="module_id" value="{{$category->module_id}}">
{{$category->module_name}}</option>
@endforeach
</select>

关于php - create() 函数不起作用且不输出错误 (Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58229882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com