首页 > 解决方案 > 未正确传递数据

问题描述

所以我有一个看起来像这样的表格:

    @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 数组,它在数组的正确索引中有正确的名称,但我不知道为什么它没有进入数据库。

提前致谢

标签: phplaravel

解决方案


首先要做的事:

不要忘记正确设置您的模型:

/**
* 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>

推荐阅读