Laravel Function that hold two parameters

My Controller

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);
}

My Route
Route::get('sites/{site_id}',['as'=>'SpecificSite','uses'=>'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7e5d2c7d8c5c3f4d8d9c3c5d8dbdbd2c5f7c4dfd8c0e4c7d2d4ded1ded4e4dec3d2">[email protected]</a>']);

Route::get('sites/{site_id}/report/{report_id}', ['as'=>'Reports', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="affdcadfc0dddbecc0c1dbddc0c3c3caddefdcc7c0d8e2c0c1dbc7c3d6fdcadfc0dddb">[email protected]</a>']);

My blade view
<a href="{{route('SpecificSite',['site_id'=>$record->site_id])}}" rel="nofollow noreferrer noopener">view</a>  

<a href="{{route('Reports',['site_id'=>$report->site_id, 'report_id'=>$report->report_id])}}" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">view</a>

Site Model
public function report()
{
    return $this->hasMany('AppReport');
}

Report Model
public function site()
{
    return $this->belongsTo('AppSite');
}

My print_r($report)
AppReport 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] => <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd8dcdad7d7dac9d6d2d5fbdcd6dad2d795d8d4d6">[email protected]</a>
    )

[original:protected] => Array
    (
        [email_date] => 2018-08-23
        [url] => http://foyston.com
        [recipient] => <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa9adaba6a6abb8a7a3a48aada7aba3a6e4a9a5a7">[email protected]</a>
    )

[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

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

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.

It should be 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.

Thank you in Advance!~

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In your controller change the way you pass the data to the view

view('reports', ['report' => $report, 'site_id' => $site_id, 'report_id' => $report_id]);

Since $report does not have a site_id or a report_id, but you calculate them and pass em in your controller as $site_id and $report_id, you should change this

<a href="{{route('Reports',['site_id'=>$report->site_id, 'report_id'=>$report->report_id])}}" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">view</a>

to this

<a href="{{route('Reports',['site_id'=>$site_id, 'report_id'=>$report_id])}}" rel="nofollow noreferrer noopener">view</a>

Method 2

This is how you must generate your routes:

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

The way you are doing, each array key will become a parameter in the query string.

Method 3

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'));
}

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!~

Method 4

You cant use anchor and link to route that has two parameters you can
1-make a route and send data to that route with form.
2-redirect to your route with this have been gotten data.
code:

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

and then do your jobs.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x