Laravel - 使用基于参数

时间:2017-07-04 03:54:57

标签: css laravel blade

我试图根据我的参数更改表格行的颜色。这是我的代码:

AdminController.php

public function table_row(){

    if ($walk->is_paid && $walk->is_completed) {
        $highlightClass = 'bg-success';
    }
    elseif (empty($walk->is_paid) && $walk->is_completed) {
        $highlightClass = 'bg-warning';
    }
    elseif ($walk->is_cancelled) {
        $highlightClass = 'bg-danger';
    }
    return view('walks', compact('$highlightClass'));
} 

walks.blade.php

<tr class="{{ $highlightClass }}">

它没有用.. :(

这是我视图中表格的图像:

enter image description here

4 个答案:

答案 0 :(得分:0)

这应该是向视图发送数据的方式。在紧凑方法中删除$

public function table_row(){
    $highlightClass = "";

    if ($walk->is_paid && $walk->is_completed) {
        $highlightClass = 'bg-success';
    }
    elseif (empty($walk->is_paid) && $walk->is_completed) {
        $highlightClass = 'bg-warning';
    }
    elseif ($walk->is_cancelled) {
        $highlightClass = 'bg-danger';
    }

    return view('walks', compact('highlightClass')); // not $highlightClass
}

参考:https://www.w3schools.com/php/func_array_compact.asp

// walks.blade.php

<tr class="{{$highlightClass or ''}}">

答案 1 :(得分:0)

只需传递功能:

public function table_row(){
    $highlightClass = "";

    if ($walk->is_paid && $walk->is_completed) {
        $highlightClass = 'bg-success';
    }
    elseif (empty($walk->is_paid) && $walk->is_completed) {
        $highlightClass = 'bg-warning';
    }
    elseif ($walk->is_cancelled) {
        $highlightClass = 'bg-danger';
    }

   return View::make('walks')
                        ->with('highlightClass', $highlightClass)
}

答案 2 :(得分:0)

只需从契约中删除$,然后尝试

返回视图('walking',compact('highlightClass'));

并了解laravel实际上的紧凑工作: - [ https://www.quora.com/What-does-compact-do-in-Laravel ] [1]

答案 3 :(得分:0)

为什么不制定将数据发送到查看文件的标准。创建一个数组,并在该数组中添加所有数据(您要发送到视图文件),并使用键和视图文件中的键获取数据。

public function table_row(){
  $data = array();
  $data['highlightClass'] = "";
  if ($walk->is_paid && $walk->is_completed) {
    $data['highlightClass'] = 'bg-success';
  }
  elseif (empty($walk->is_paid) && $walk->is_completed) {
    $data['highlightClass'] = 'bg-warning';
  }
  elseif ($walk->is_cancelled) {
    $data['highlightClass'] = 'bg-danger';
  }
  return view('walks',$data); 
}