如何从控制器传递变量以在刀片文件中显示它们的值

时间:2017-12-16 11:02:37

标签: php laravel-5.5

我想在刀片文件中显示变量值。以下是控制器代码: -

public function ClientListReviews()
{
    // to show client list get data from users booking table

      $select_clientlist = 
      DB::table('users_booking')>where('service_provider', '=', 1)->get();

      if(count($select_clientlist) > 0)
      {
         return view('client-database')->with($select_clientlist);
      }
      else
      {
         return view('client-database')->withMessage('No Details Found');
      }
 }

我想显示$ select_clientlist变量中的值。以下是我的刀片文件中的代码: -

@foreach($select_clientlist as $clientlist)

  {{$clientlist->firstname}}
@endforeach

以下是路线文件代码: -

Route::post('client_list_ajax','ClientDatabase\ClientdatabaseController@ClientListReviews');

我收到错误。 enter image description here

我做错了什么?

5 个答案:

答案 0 :(得分:0)

使用紧凑方法传递变量

 return View::make('myviewfolder.myview', compact('view1','view2','view3'));

view1,view2,view3是变量名

答案 1 :(得分:0)

使用

return view('client-database')->with('select_clientlist',$select_clientlist);

或者

return view('client-database',compact('select_clientlist'));

同时添加select_clientlist其他部分以防止未定义的错误

public function ClientListReviews()
{
    // to show client list get data from users booking table

      $select_clientlist = 
      DB::table('users_booking')>where('service_provider', '=', 1)->get();

      if(count($select_clientlist) > 0)
      {
         return view('client-database')->with('select_clientlist',$select_clientlist);
      }
      else
      {
         $select_clientlist = [];
         return view('client-database')->with('select_clientlist',$select_clientlist)->withMessage('No Details Found');
      }
 }

或{刀片文件中的isset($select_clientlist)检查

$__currentLoopData = isset($select_clientlist)?$select_clientlist:[];

答案 2 :(得分:0)

因为只有在有记录包装你的每个内部isset

时才传递变量
if (isset($select_clientlist)) {
   foreach($select_clientlist as $clientlist) {

   }
}

答案 3 :(得分:0)

你的query应该是这样的。你可能会忘记SELECT陈述

$select_clientlist = DB::table('users_booking')->select('*')->where('service_provider', '=', 1)->get();

答案 4 :(得分:0)

将该变量以任何方式传递给您的视图..它应该是一个集合。如果没有记录,它就是空的。如果foreach为空,则public function ClientListReviews() { $select_clientlist = DB::table('users_booking')->where('service_provider', 1)->get(); $view = view('client-database', compact('select_clientlist')); if ($select_clientlist->isEmpty()) { $view->with('message', 'No Details Found'); } return $view; } 不会运行。就这么简单。无需检查是否有任何设置或是空的等等......只是总是传递该集合。

enter code here

private String foreame = null;
private String surname = null;

public FullName(String forename, String surname) 
{
   this.forename = forename;
   this.surname = surname;
}