php - 未正确传递数据
问题描述
所以我有一个看起来像这样的表格:
@csrf
<div class="row">
@if ($errors->any())
<div class="alert alert-danger mt-2" role="alert">
<strong>{{ implode('', $errors->all(':message')) }}</strong>
</div>
@endif
</div>
<div class="form-group">
<label for="projectTitle">Project title</label>
<input type="text" class="form-control" name="proj_title" value="{{old('proj_title',$project->proj_title)}}">
</div>
<div class="form-group">
<label for="projectDesc">Description</label>
<textarea class="form-control" name="proj_desc" value="{{old('proj_desc',$project->proj_desc)}}"></textarea>
</div>
<div class="form-group">
<label for="clientId">Client Id</label>
<select name="client_id" class="form-control">
<option>1</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Create</button>
</div>
如您所见,用户需要输入项目名称、项目描述和客户 ID。然后我有一个索引页面,你可以在其中看到项目,看起来像这样
@extends('layouts.app')
@section('content')
@if (Auth::user()->role == 1)
<a class="btn btn-secondary" href="/projects/create">Add Project</a>
@endif
<br><br>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Project Id</th>
<th>Title</th>
<th>Description</th>
<th>Client Id</th>
<th>Created by</th>
<th>Created on</th>
</tr>
</thead>
<tbody class="">
@foreach ($project as $project)
<tr>
<td>{{$project->proj_id}}</td>
<td>{{$project->proj_title}}</td>
<td>{{$project->proj_desc}}</td>
<td>{{$project->client_id}}</td>
<td>{{$project->created_by}}</td>
<td>{{$project->created_at}}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
我的意图是它会自动获取登录用户的名称并将其放在“创建者”字段中,我制作的代码是这样的
public function store(Request $r)
{
$validatedData = $r->validate([
'proj_title' => 'required|max:100',
'client_id' => 'required',
'proj_desc' => 'required',
]);
$currentUser = Auth::user()->name;
$r['created_by'] = $currentUser;
(new Project($r->all()))->save();
return redirect('/projects')->with('store','');
}
我测试了返回 $r 数组,它在数组的正确索引中有正确的名称,但我不知道为什么它没有进入数据库。
提前致谢
解决方案
首先要做的事:
不要忘记正确设置您的模型:
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
];
/**
* The attributes that should be hidden for arrays.
*
* @var string[]
*/
protected $hidden = [
];
您的created_by
字段必须是 a 的外键,User
因此您可以通过在迁移中执行此操作:
$table->integer('created_by');
$table->foreign('created_by')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
在你的Project
模型中加入一个belongsTo
关系:
/**
* Get the user that owns the phone.
*/
public function creator()
{
return $this->belongsTo(User::class);
}
请参阅: laravel.com/docs/5.8/eloquent-relationships#one-to-many
第二件事
代替:
(new Project($r->all()))->save();
经过:
$project = Project::create($r->all());
第二步
更改您的退货:
return redirect('/projects')->with('store','');
经过:
return redirect('/projects')->with(['project' => $project]);
第三步
当您要检索项目的创建者时,请执行以下操作:
<tr>
<th>Created by: {{ $project->creator->name }}</th>
</tr>
推荐阅读
- python - Gensim n_similarity 单词不在词汇表中
- symfony - Composer & Symfony - 致命错误:内存不足
- android - 如何在mpandroidchart上设置自定义值axisLeft
- ios - UITest 在框架中不起作用
- android - 设置时间表布局
- javascript - 我被复选框属性卡住了
- python - 使用 csv 文件中的批量数据创建资源的 POST 请求
- javascript - 在一个范围内“交换”值
- html - 我的 Indexeddb 只创建对象存储名称,但不显示我添加的数据;我怎样才能解决这个问题?
- angularjs - 在angularjs中绑定href和click事件