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)->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');
}
我的 print_r($report)
App\Report Object
(
[fillable:protected] => Array
(
[0] => site_url
[1] => reciepients
[2] => monthly_email_date
)
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[email_date] => 2018-08-23
[url] => http://foyston.com
[recipient] => cgallarmin@gmail.com
)
[original:protected] => Array
(
[email_date] => 2018-08-23
[url] => http://foyston.com
[recipient] => cgallarmin@gmail.com
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] => )1
我的 showSpecificSite 功能运行良好。我的本地主机看起来像这样http://localhost:8000/sites/1/
==
http://localhost:8000/sites/$site_id/
现在我的问题是我的 showMonthlyReport。
http://localhost:8000/sites/report
==
http://localhost:8000/sites/null/report/null
这是我一直走的路。
它应该是http://localhost:8000/sites/1/report/1
任何想法来解决这个问题?对不起,我的语法不好,我的英语不太好。
先谢谢了!~
解决方案
在您的控制器中更改将数据传递给视图的方式
view('reports', ['report' => $report, 'site_id' => $site_id, 'report_id' => $report_id]);
由于$report
没有 asite_id
或 a report_id
,但您计算它们并将 em 作为 and 传递到控制器中$site_id
,$report_id
您应该更改它
<a href="{{route('Reports',['site_id'=>$report->site_id, 'report_id'=>$report->report_id])}}">view</a>
对此
<a href="{{route('Reports',['site_id'=>$site_id, 'report_id'=>$report_id])}}">view</a>
推荐阅读
- java - 尝试删除所有缓存时出现 Ehcache 异常
- java - 无法从 Spring Boot 微服务模块中的 Zuul Header 获取 JWT Token
- android - 无法同时运行 Appium 测试模拟器和真实移动设备
- android - 是否可以在 Android Studio 中使用已发布的签名 APK 构建 App Bundle?我想将 apk 迁移到 Play 商店的应用程序包
- jmeter - 将 JMeter 与 ZAP 工具集成
- angular - 如何在 TypeScript 中同步下拉菜单?
- c# - 如何在应用程序代码中删除 app.config
- ios - 在蒙面的 UIView 周围添加阴影
- machine-learning - 在使用全卷积网络的语义分割中,为什么交叉熵损失优于 L1 或 L2 损失?
- android - android中画布文本的椭圆大小