三个不同的表在一个视图laravel 5.2

时间:2016-01-05 21:03:17

标签: php arrays laravel laravel-5.2

您好我想使用laravel 5.2在一个视图中显示三个不同的表。但似乎我遇到了问题。

我的HomeController.php     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        $about = DB::select('select * from about');
        $teams = DB::select('select * from teams');
        $services = DB::select('select * from services');

        return view('master', ['about' => $about], ['teams' => $teams], ['services' => $services]);
    }
}

在我的观点中:

@foreach ($about as $abt)
      <h4>{{$abt->title}}</h4>
      <span class="semi-separator center-block"></span>
      <p>{{$abt->description}}</p>
@endforeach

@foreach ($teams as $team)
     <div class="creative-symbol cs-creative">
        <img src="assets/images/new/{{$team->icon}}" alt="">
        <span class="semi-separator center-block"></span>
        <h4><b>{{$team->title}}</b></h4>
        <p>{{$team->description}}</p>
     </div>
@endforeach

我无法显示第三个是$ services。请帮助我。 当我添加第三个时,它将显示错误

2 个答案:

答案 0 :(得分:3)

在Laravel 5.1中,我在/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php中找到了以下代码:

if (! function_exists('view')) {
    /**
     * Get the evaluated view contents for the given view.
     *
     * @param  string  $view
     * @param  array   $data
     * @param  array   $mergeData
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
     */
    function view($view = null, $data = [], $mergeData = [])
    {
        $factory = app(ViewFactory::class);

        if (func_num_args() === 0) {
            return $factory;
        }

        return $factory->make($view, $data, $mergeData);
    }
}

这是您尝试呼叫的功能。注意有多少个参数(有3个)。你试图通过4.我认为你要做的事情是这样的:

return view('master', [
    'about' => $about, 
    'teams' => $teams, 
    'services' => $services
]);

现在调用相同的函数,但只传递两个参数。

答案 1 :(得分:1)

请更改此内容:

        return view('master', ['about' => $about], ['teams' => $teams], ['services' => $services]);

到此:

        return view('master', ['about' => $about, 'teams' => $teams, 'services' => $services]);