Php 包含两个参数的Laravel函数

Php 包含两个参数的Laravel函数,php,laravel,Php,Laravel,我的控制器 public function showSpecificSite($site_id){ $reports = Report::whereHas('site', function($query) use($site_id) { $query->where('site_id', $site_id); })->get(['email_date', 'url', 'recipient']); $siteName = Site::find($site_id)->s

我的控制器

public function showSpecificSite($site_id){

$reports = Report::whereHas('site', function($query) use($site_id) {
    $query->where('site_id', $site_id);
})->get(['email_date', 'url', 'recipient']);

$siteName = Site::find($site_id)->site_name;

  return view('newsite', compact('site_id', 'siteName', 'reports'));
}

public function showMonthlyReport($site_id, $report_id)
{

$site = Report::whereHas('site', function($query) use($site_id) {
    $query->where('site_id', $site_id);
})->get();

$report = $site->Report::find($report_id);

return view('reports')->with('report', $report)->with('site_id',$site_id)
->with('report_id', $report_id);
}
我的路线

Route::get('sites/{site_id}',['as'=>'SpecificSite','uses'=>'ReportController@showSpecificSite']);

Route::get('sites/{site_id}/report/{report_id}', ['as'=>'Reports', 'uses' => 'ReportController@showMonthlyReport']); 
我的刀片视图

<a href="{{route('SpecificSite',['site_id'=>$record->site_id])}}">view</a>  

<a href="{{route('Reports',['site_id'=>$report->site_id, 'report_id'=>$report->report_id])}}">view</a>
报告模型

public function report()
{
    return $this->hasMany('App\Report');
}
public function site()
{
    return $this->belongsTo('App\Site');
}
我的打印($report)

我的ShowSpecific网站功能运行得很好。我的本地主机是这样的
http://localhost:8000/sites/1/
==
http://localhost:8000/sites/$site\u id/

现在我的问题是我的showMonthlyReport。
http://localhost:8000/sites/report
==
http://localhost:8000/sites/null/report/null
这是我一直走的路

它应该是
http://localhost:8000/sites/1/report/1

有没有办法解决这个问题? 对不起,我语法不好,我英语不太好

提前谢谢你~


您不能使用锚点和链接来路由具有两个参数的路由
1-创建一条路由并使用表单将数据发送到该路由。
2-使用此已获取的数据重定向到您的路线。
代码:

public function redirectroute(Request $request){
        return Redirect::to('sites/'.$request->site_id.'/report'.$request->report_id)->with(compact('request'));
    }


然后执行您的工作。

在控制器中更改将数据传递到视图的方式

view('reports',['report'=>$report,'site\u id'=>$site\u id,'report\u id'=>$report\u id]);
由于
$report
没有
站点id
报告id
,但您计算它们并将它们作为
$site\u id
$report\u id
传递到控制器中,因此您应该更改此设置


对此


以下是生成路线的方法:

<a href="{{ route('SpecificSite', [$record->site]) }}">view</a>
<a href="{{ route('Reports', [$report->site, $report]) }}">view</a>


按照您的操作方式,每个数组键都将成为查询字符串中的一个参数。

我通过此方法解决了我的问题

public function showSpecificSite($site_id){

$reports = Report::whereHas('site', function($query) use($site_id) {
    $query->where('site_id', $site_id);
})->get(['email_date', 'url', 'recipient', 'report_id', 'site_id']);

$siteName = Site::find($site_id)->site_name;

return view('newsite', compact('site_id', 'siteName', 'reports'));
}
这是因为我只从我的站点获取了三个数据,这就是为什么我的ID为空。
但是非常感谢你们帮助我。祝你今天愉快!~

在我看来,你的
使用的
是错误的,你能检查一下吗?就像这样
['as'=>'Reports','uses=>ReportController@showMonthlyReport']
使用=>xx
作为整个字符串而不是键-pair@Phiter刚刚修复了,但仍然是相同的错误。我的站点id和报告id仍然为空。如果使用php artisan route:list,会发生什么情况?我指的是发生了什么。。。这条路线是如何印在这个命令上的?你能试着将
报告
路线移到
特定站点
路线上方吗?@Christian我的答案能帮你吗?让我试试。希望这会有帮助@AbolfazlMohajeri@Christian是的,试试这个,我所有的ID仍然是空的,兄弟,但谢谢你尝试帮助我。它给了我关于刀片视图的错误。未定义变量:report\u id未定义变量:report,但我在函数中声明了它。问题是我的特定站点已经声明到我的主视图中,这就是为什么我需要已经获取数据,但似乎很难找到如何做到这一点
public function showSpecificSite($site_id){

$reports = Report::whereHas('site', function($query) use($site_id) {
    $query->where('site_id', $site_id);
})->get(['email_date', 'url', 'recipient', 'report_id', 'site_id']);

$siteName = Site::find($site_id)->site_name;

return view('newsite', compact('site_id', 'siteName', 'reports'));
}