Php Laravel 5未定义变量控制器2视图

Php Laravel 5未定义变量控制器2视图,php,laravel,Php,Laravel,我对laravel中未定义的变量错误感到疯狂,希望有人能指出我做错了什么,已经尝试了几种方法将变量从我的控制器传递到我的视图,但似乎没有任何效果。 提前谢谢 控制器: public function busquedaGet() { return view('frontend.user.busquedaBoxeadores',compact('posts')); } public function busquedaPost(Request $request, Busqueda

我对laravel中未定义的变量错误感到疯狂,希望有人能指出我做错了什么,已经尝试了几种方法将变量从我的控制器传递到我的视图,但似乎没有任何效果。 提前谢谢

控制器:

   public function busquedaGet()
{
   return view('frontend.user.busquedaBoxeadores',compact('posts'));

}




public function busquedaPost(Request $request, BusquedaRepository $userRepo)

{
     $validator = Validator::make($request->all(), [
        'gender' => 'required|not_in:null',
        'weight' => 'required|not_in:null',
        'country' => 'required|not_in:null'
    ]);


    if ($validator->fails()) {
        return redirect(route('busquedaPostA'))->withErrors($validator);
    }

     $posts=$userRepo->busquedaBoxers($request->all());
      //dd($posts);
    return view('frontend.user.busquedaBoxeadores',compact('posts'));

}
视图:

存储库

    public function busquedaBoxers(array $data, $source = 'site')
{
 $query = DB::table('users')
        ->join('user_profiles', 'users.id', '=', 'user_profiles.user_id')
        ->join('boxings', 'users.id', '=', 'boxings.user_id')
        ->join('countries', 'users.id', '=', 'countries.user_id')           
        ->select('users.name', 'users.username', 'users.id')
        ->where('boxings.weight_id', '=', $data['weight'])
        ->where('user_profiles.gender', '=', $data['gender'])
        ->where('countries.country', '=', $data['country'])
        ->get();

        return $query;
}

您对这两种方法使用相同的视图:
busqedaget
busqedapost
,但在
busqedaget
中,您是
compact
-ing未定义的变量
posts
。因此,如果在get方法中未使用空字符串,则可以添加空字符串:

public function busquedaGet()
{
    $posts = [];
    return view('frontend.user.busquedaBoxeadores',compact('posts'));
}
并修改视图。替换:

<p> Hello, {{ $posts }} </p>
你好,{{$posts}

与:

你好, @foreach($posts作为$post) {{$post->username} @endforeach


您试图在blade中将帖子显示为字符串,但在
BusQuedGet
BusQuedPost
中未定义它是一个集合

你好,{{$posts}}


首先,您需要在
BusQuedGet
中为其定义一个默认值,然后尝试在集合中循环以显示其内容。

添加错误描述确切的错误消息是什么?这是我得到的:未定义变量:posts(视图:C:\Users\Bof3ss37\Desktop\socialwall\u v1.0.3\resources\views\frontend\user\busquedaBoxeadores.blade.php)busque‌​Daboxes崇拜者。刀片‌​hp在此视图中出错..添加有关此文件的更多信息
BusQuedGet
方法在控制器中没有
$posts
变量Hanks Alex Ok我尝试过,但现在我在执行搜索时得到了此信息。Htmlenties()预期参数1为字符串,数组给定(视图:C:\Users\Bof3ss37\Desktop\socialwall\u v1.0.3\resources\views\frontend\user\busquedaBoxeadores.blade.php)@CesarGarcia然后你必须决定$posts应该是什么?如果它是一个数组-将它定义为数组并用foreach语句显示。在AnswerThank Man中添加了示例!!它正在运行!!Oohh花了几个小时在这个lol上,应该在之前问一下哦好吧。。
public function busquedaGet()
{
    $posts = [];
    return view('frontend.user.busquedaBoxeadores',compact('posts'));
}
<p> Hello, {{ $posts }} </p>
<p> Hello,
@foreach($posts as $post)
    {{ $post->username }}
@endforeach
</p>