获取经过身份验证的用户的名称并将其存储

时间:2019-04-15 09:05:00

标签: php laravel

我有一个表格,您可以在其中提交“项目”。其中一个字段是“创建者”,但用户不会更新此字段,它会在那一刻获取经过身份验证的用户的名称,并将其插入数据库。

我知道如何检索用户名,但是问题是当我与另一个用户登录时,所有名称都会更改,因为我每次都存储该名称。 这是我的项目负责人

<?ignore.*\?>

这是我的索引

<?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','');
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您正在使用:

{{ $project->Auth::user()->name }}

代替

{{ $project->created_by }}