Q: Search query through foreign key I'm trying to search through my comments on the username connected to the user_id Foreign Key
Tables: 
Comment- id, comment, user_id, timestamps
User - id, name, username 
Models: 
class Comment extends Model
{

    public function author()
    {
        return $this->belongsTo('App\User','user_id');
    }
}

Controller:
public function index(Request $request)
{
     $comments = Comment::query()->orderby('created_at', 'DESC');

    $id=$request->]


    return view('comments.index')->withComment($comment);
}

View: 
            <div class="panel-body">
                {!! Form::open(['route' => 'comments.index', 'method' => 'GET']) !!}
                <div class="col-md-5">
                    {!! Form::label('id', 'Search By ID:') !!}
                    {!! Form::text('id', null, array('class' => 'form-control')) !!}
                </div>
                <div class="col-md-5">
                    {!! Form::label('username', 'Search By Username:') !!}
                    {!! Form::text('username', null, array('class' => 'form-control')) !!}
                </div>
                <div class="col-md-2">
                    {!! Form::submit('Find Comments', array('class' => 'btn btn-send ')) !!}
                </div>
                {!!Form::close()!!}

            </div>
@foreach($comment as $comments)
//data
@endforeach


A: I believe it is failing at the builder level because you are trying to query on an object that is not yet known.  IE the author LIKE must be part of a sub query:
$comments = Comment::whereHas('author', function ($query) use($request) {
    $query->where(users.username, 'LIKE', $request->input('username') )
}])->orderby('created_at', 'DESC')->get();

I see you will have to write the query a second time, but I think this may be successful for you.
