如何从laravel中的另一个表获取用户名

zvms9eto  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(158)

你好,我想得到我的回复表上的用户名,但它只显示我的用户ID,当我键入$reply-〉user_id-〉name时,它向我显示错误:尝试读取int的属性"名称"。
我该怎么修呢?
这是我的密码

票证型号

public function replies() {

    return $this->hasMany(Reply::class);
    
}

回复模型

public function tickets() {

        return $this->belongsTo(Tickets::class);
    }

票证管理员

public function show($id) {

       $user = Auth::user();
       
        $tickets = Tickets::with('companies')->get();

        $ticketscomp = Companies::with('tickets')->get();

        $severities = Severities::with('tickets')->get();

        $users = User::with('tickets')->get();

        //$replies = DB::table('replies')->where('ticket_id', $id)->get();

        
        // $articles = Reply::where('user_id', $id)->with('User')->get();

    
       $ticketspage = Tickets::with('severities')->with('companies')->with('user')->with('replies')->findOrFail($id);
 
      
        return view('tickets.ticket', compact('ticketspage'))->
        with(['tickets'=> $tickets])->
        with(['ticketscomp'=>$ticketscomp])->
        with(['severities'=>$severities])->
        with(['ticketspage'=>$ticketspage])->
        with(['replies'=>$replies]);
 
         //dd($reply_author->toArray());
        
    }

刀片

@foreach ($replies as $reply)

            <div class="NjBSH">
                <div id="AuthorAndDate-1">
                    
                    <span class="author">{{ $reply->user_id->name }}</span>
                    
                    <span class="date"><span style="color: rgb(32, 33, 36);">{{ $reply->created_at }}</span><br></span>
                </div>
                <div class="kmSodw">
                    <span>{{ $reply->text }}</span>
                </div>
            </div>
        
        @endforeach

文本,创建于同时显示$reply-〉user_id,但不显示此$reply-〉user_id-〉名称。

dsekswqp

dsekswqp1#

以下是使用ELOQUENT的完整解决方案:

票证型号

public function replies() {
    return $this->hasMany(Reply::class);    
}

回复模型

public function ticket() {
    return $this->belongsTo(Ticket::class);
}

public function user() {
    return $this->belongsTo(User::class);
}

票务管理员:

public function show(Ticket $ticket) {  
    //Load all of the ticket relationships that we will be using
    $ticket->load('replies.articles', 'replies.user', 'companies', 'severities');
    
    //Assign the loaded ticket companies
    $companies = $ticket->companies;
    
    //Assign the loaded ticket severities
    $severities = $ticket->severities;
    
    //Assign the loaded ticket replies
    $replies = $ticket->replies;
    
    /* 
        You can acess the replies -> articles && user in a foreach loop, in php or blade    
        foreach($replies->articles as $article){
           //$article->id   
        }
    */  
    
    return view('tickets.ticket', compact('ticket', 'companies', 'severities', 'replies'));
 }

"刀锋"

@foreach ($replies as $reply)
    <div class="NjBSH">
        <div id="AuthorAndDate-1">
            <span class="author">{{ $reply->user->name }}</span>
            <span class="date"><span style="color: rgb(32, 33, 36);">{{ $reply->created_at }}</span><br></span>
        </div>
        
        <div class="kmSodw">
            <span>{{ $reply->text }}</span>
        </div>
    </div>
@endforeach

相关问题