我有一个表格,您可以在其中提交“项目”。其中一个字段是“创建者”,但用户不会更新此字段,它会在那一刻获取经过身份验证的用户的名称,并将其插入数据库。
我知道如何检索用户名,但是问题是当我与另一个用户登录时,所有名称都会更改,因为我每次都存储该名称。 这是我的项目负责人
<?php
namespace App\Http\Controllers;
use App\Project;
use App\Client;
use Auth;
use Illuminate\Http\Request;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('projects.index', [
'project' => Project::all()
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
if (Auth::user()->role != 1){
return redirect()->back();
}else{
return view('projects.create',[
'project' => new Project,
'client' => new Client
]);
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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;
$project = Project::create($r->all());
return redirect('/projects')->with('store','');
}
/**
* Display the specified resource.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function show(Project $project)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $r, Project $project)
{
$project->update($r->all());
return redirect('/projects')->with('update','');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect('/projects')->with('delete','');
}
}
这是我的索引
@section('content')
<div class="row">
<div class="col">
@if (Auth::user()->role == 1)
<a class="btn btn-success" href="/projects/create">Add Project</a>
@endif
</div>
<div class="col"></div>
<div class="col">
@if(session()->has('store'))
<div class="alert alert-success mt-2" role="alert">
<strong>Project created</strong>
</div>
@elseif(session()->has('update'))
<div class="alert alert-success mt-2" role="alert">
<strong>Project updated</strong>
</div>
@elseif(session()->has('delete'))
<div class="alert alert-success mt-2" role="alert">
<strong>Project deleted</strong>
</div>
@endif
</div>
</div>
<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>
@if (Auth::user()->role==1)
<th>Admin</th>
@endif
</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->Auth::user()->name}}</td>
<td>{{$project->created_at}}</td>
@if (Auth::user()->role==1)
<td>
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="{{route('projects.edit',$project)}}">Edit</a>
<form method="POST" action="{{route('projects.destroy',$project)}}" onsubmit="return confirm('Are you sure you want to delete this?')">
@method('DELETE')
@csrf
<button class="dropdown-item" type="submit">Delete</button>
</form>
</div>
</div>
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
@endsection
答案 0 :(得分:0)
我认为您的问题是您正在使用:
/
代替
{{ $project->Auth::user()->name }}