skip to Main Content

I have the following code in FreelancerControler:

 public function search(Request $request)
    {
        if ($request->ajax()) {
            $data = User::where('name','LIKE',$request->name.'%')->where('role','freelancer')->get();
            $output = '';
            if (count($data)>0) {
                foreach ($data as $user) {
                    $output .= ' <div class="row freelancer p-1 m-1">
                    <div class="col-sm-3">
                        <img src="'.$user->avatar.'" height="100px" width="100px">
                    </div>
                    <div class="col-sm-9">
                        <h3><a href={{route("profile",["id"=>'.$user->id.'])}}>'.$user->name.'}}</a></h3>
                        <p>'.$user->profile->title.'</p>
                        <p>'.$user->profile->about.'</p>
                    </div>
                </div>';
                }
            }else {
                $output .= 'No Data Found';
            }

            return $output;
        }

        return view('autosearch');  
    }

My problem is in this line:

<h3><a href={{route("profile",["id"=>'.$user->id.'])}}>'.$user->name.'}}</a></h3>

its output is a broken links with this text: "3])}}>Karam}}" , as the user_id is 3 and user_name is Karam.

2

Answers


  1. Syntax Error:

    <h3><a href={{route("profile",["id"=>'.$user->id.'])}}>'.$user->name.'}}</a></h3>
    
    

    you may use this one:

    <h3><a href="'.route('profile', ['id' => $user->id]).'">'.$user->name.'</a></h3>
    
    

    This will put the value instead of blade format to perform additional step in ajax to render.

    Login or Signup to reply.
  2. You can use the route function in controller but using a concatenations
    something like this

                   foreach ($data as $user) {
                        $output .= ' <div class="row freelancer p-1 m-1">
                        <div class="col-sm-3">
                            <img src="'.$user->avatar.'" height="100px" width="100px">
                        </div>
                        <div class="col-sm-9">
                            <h3><a href='.route("profile",["id"=>$user->id]).'>'.$user->name.'</a></h3>
                            <p>'.$user->profile->title.'</p>
                            <p>'.$user->profile->about.'</p>
                        </div>
                    </div>';
                    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search