将数据从控制器传递到刀片视图 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43293123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:45:05  来源:igfitidea点击:

passing data from controller to blade view laravel

phplaravelviewcontrollerblade

提问by Ileana Profeanu

I am trying to send parameters through a href to a page from an events list to an event page.

我正在尝试通过 href 将参数发送到从事件列表到事件页面的页面。

My route is

我的路线是

 Route::get('eventpage', 'EventController@index')->name('eventpage');

And my event Controller is

我的事件控制器是

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

   class EventController extends Controller
{

public function index(Request $request)
{
    $id = $request->query('id');
    $event = DB::table('events')->where('id',$id)->get();
    $pics = DB::table('pictures')->where('event_id',$id)->get();
    $n = count($pics); // the number of pictures for a particular event
    return view('pages.eventPage');
}

}

}

The trouble is that for the first variable I try to use, $n, it gives me an error, "Undefined variable: n "

麻烦的是,对于我尝试使用的第一个变量 $n,它给了我一个错误,“未定义的变量:n”

My blade code is as follows

我的刀片代码如下

@for($i = 1; $i < $n; $i++)
<li data-target="#carousel-example-generic" data-slide-to="{{ $i }}"></li>
@endfor

What am I doing wrong?

我究竟做错了什么?

回答by Karthik

route

路线

 Route::get('eventpage', 'EventController@index')->name('eventpage');

event Controller

事件控制器

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

   class EventController extends Controller
{

public function index(Request $request)
{
    $id = $request->query('id');
    $event = DB::table('events')->where('id',$id)->get();
    $pics = DB::table('pictures')->where('event_id',$id)->get();

return view('pages.eventPage',compact('event','pics'));

}

}

blade code

刀片代码

@for($i = 1; $i < count($pics); $i++)
<li data-target="#carousel-example-generic" data-slide-to="{{ $i }}"></li>
@endfor

回答by Amr Aly

You can pass your data to your view like that:

您可以像这样将数据传递给您的视图:

public function index(Request $request)
{
  ....

  return view('pages.eventPage', compact('id', 'event', 'n'));
}