首页 > 解决方案 > 包含两个参数的 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

任何想法来解决这个问题?对不起,我的语法不好,我的英语不太好。

先谢谢了!~

标签: phplaravel

解决方案


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

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>

推荐阅读