Laravel函数具有两个参数 [英] Laravel Function that hold two parameters

查看:74
本文介绍了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/

My showSpecificSite function was working so well. My localhost is look like this http://localhost:8000/sites/1/ == http://localhost:8000/sites/$site_id/

现在我的问题是我的showMonthlyReport.
http://localhost:8000/sites/report == http://localhost:8000/sites/null/report/null 这是我总能走的路.

Now my problem is my showMonthlyReport.
http://localhost:8000/sites/report == http://localhost:8000/sites/null/report/null This is the path that I always get.

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

有解决此问题的主意吗? 对不起,我的语法不好,我的英语不太好.

Any idea to fix this issue? Sorry for my bad grammar I'm not that well in English.

预先感谢您!〜

推荐答案

我以此解决了我的问题.

I resolve my problem by this.

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为空的原因. 但是非常感谢你们对我的帮助.祝你有美好的一天!〜

Its because I'm only getting three data from my site that's why my ID's are null. But thank you so much guys for helping me. Have a great day!~

这篇关于Laravel函数具有两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆