Home > Net >  How to get user name from another table in laravel
How to get user name from another table in laravel

Time:12-26

Hello I want to get user names on my reply table, but there its shows me only user id, when I type $reply->user_id->name it shows me that error: "Attempt to read property "name" on int".

How can I fix it ?

there are my codes:

Ticket Model

public function replies() {

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

Reply Model

 public function tickets() {

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

Tickets Controller

 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());
        
    }

Blade

  @foreach ($replies as $reply)

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

Text, Created at showing also $reply->user_id but not this $reply->user_id->name.

CodePudding user response:

Here is a complete solution using ELOQUENT:

Ticket Model

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

Reply Model

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

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

Tickets Controller:

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

Blade

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

CodePudding user response:

Thank you all for answer.

I solve this Like this.

Reply Model

 public function tickets() {

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

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

Tickets Model

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

User Model

 public function reply(){

        return $this->HasMany(Reply::class, 'name');


    }

Tickets Controller

I just added this code in my tickets controller show function

 $replies = Reply::with('tickets', 'user')->where('ticket_id', $id)->get();

Blade

{{ $reply->user->name }}

This is correct code.

Thank you all who supports me.

  • Related