Ad

How To Get Foreach In Controller Into Table View In Blade Laravel

- 1 answer

I have been trying to get data from google analytics in laravel. It will show the pathpage and the pageviews in a form of table. I have retrieved the data using spatie/laravel-analytics but when I display it on the view the page views is not tally to the google analytics. So I have been trying to sort the data using the following code but keep getting errors

Here is my controller:

public function google(MediaSite $mediaSite)
{
$ga7day = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
    'metrics' => 'ga:sessions, ga:pageviews',
    'dimensions' => 'ga:yearMonth, ga:pagePath',



]
);

  $ga1month = Analytics::performQuery(
Period::days(30),
'ga:sessions',
[
    'metrics' => 'ga:sessions, ga:pageviews',
    'dimensions' => 'ga:yearMonth, ga:pagePath',



]
);

$ga3month = Analytics::performQuery(
Period::months(3),
'ga:sessions',
[
    'metrics' => 'ga:sessions, ga:pageviews',
    'dimensions' => 'ga:yearMonth, ga:pagePath',



]
);

$analyticsDataToday = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
    'metrics' => 'ga:sessions, ga:pageviews',
    'dimensions' => 'ga:yearMonth, ga:pagePath',



]
);

$mediaSite = new MediaSite;

$mediaSite = MediaSite::Where('media_owner_id','1');



foreach ($mediaSite as $site) {


$site->view7days = 0;



foreach ($ga7day as $ga) {
# code...


if($ga['1'] === '/media-site/'. $site->id);
   $site->view7days = $ga['3'];



 # code...
 }


 $site->view1month = 0;


 foreach ($ga1month as $mga) {
 # code...


 if($mga['1'] === '/media-site/'. $site->id);
   $site->view1month = $mga['3'];
 # code...
 }


 $site->view3month = 0;


 foreach ($ga3month as $nga) {
 # code...


 if($nga['1'] === '/media-site/' . $site->id);
   $site->view3month = $nga['3'];


 # code...
 }

 }
 return 
 view('analytics.google',compact('ga7day','ga1month','ga3month',
 'analyticsDataToday','site','mediaSite','ga','mga','nga'));

my view:

<body>

  <form method="get" action="{{ action('[email protected]') }}">

            {{ csrf_field() }}

            <div class="input-group">
              <input type="text" name="q" class="form-control" placeholder="eg: Media Ownner" required>
              <span class="input-group-btn">
                <button type="submit" class="btn btn-default">
                  Go!
                </button>
              </span>
            </div>
        </form>

<br>
<br>
<table class="table">
  <tr>

    <th> Media Site </th>
    <th> 7 days</th>
    <th> 1 month</th>
    <th> 3 month</th> 
    <th> Overall</th>
  </tr>
                        <tr>

                            @foreach ($ga as $index => $key)
                               <tr> <td> {{ $key['1'] }} </td>
                               <td> {{ $key['3'] }} </td>
                                <td> {{ $ga1month[$index]['3'] }}</td>
                                <td> {{ $ga3month[$index]['3'] }}</td>
                                <td> {{ $analyticsDataToday[$index]['3'] }}</td></tr>
                               @endforeach







                        </tr>

                    </table>
</body>
</html>

I keep getting these errors: enter image description here

Ad

Answer

Replace your controller with the following

public function google(MediaSite $mediaSite) {

$ga7day = Analytics::performQuery(  Period::days(7),
                                    'ga:sessions',
                                    [
                                        'metrics' => 'ga:sessions, ga:pageviews',
                                        'dimensions' => 'ga:yearMonth, ga:pagePath',
                                    ]
                                );

$ga1month = Analytics::performQuery(    Period::days(30),
                                        'ga:sessions',
                                        [
                                            'metrics' => 'ga:sessions, ga:pageviews',
                                            'dimensions' => 'ga:yearMonth, ga:pagePath',
                                        ]
                                    );

$ga3month = Analytics::performQuery(    Period::months(3),
                                        'ga:sessions',
                                        [
                                            'metrics' => 'ga:sessions, ga:pageviews',
                                            'dimensions' => 'ga:yearMonth, ga:pagePath',
                                        ]
                                    );

$analyticsDataToday = Analytics::performQuery(  Period::days(7),
                                                'ga:sessions',
                                                [
                                                    'metrics' => 'ga:sessions, ga:pageviews',
                                                    'dimensions' => 'ga:yearMonth, ga:pagePath',
                                                ]
                                            );

$mediaSite = new MediaSite;

$mediaSite = MediaSite::Where('media_owner_id','1');

foreach ($mediaSite as $site) {
    $site->view7days = 0;
    foreach ($ga7day as $ga) {
        # code...
        if($ga['1'] === '/media-site/'. $site->id);
            $site->view7days = $ga['3'];
        # code...
    }

    $site->view1month = 0;
    foreach ($ga1month as $mga) {
    # code...
    if($mga['1'] === '/media-site/'. $site->id);
        $site->view1month = $mga['3'];
        # code...
    }

    $site->view3month = 0;
    foreach ($ga3month as $nga) {
    # code...
    if($nga['1'] === '/media-site/' . $site->id);
        $site->view3month = $nga['3'];
        # code...
    }
}
return view('analytics.google',compact('mediaSite'));
}

And in the view

@foreach ($meadiaSite as $site)
<tr> 
    <td> {{ $site['1'] }} </td>
    <td> {{ $site->view7days }} </td>
    <td> {{ $site->view1month }}</td>
    <td> {{ $site->view3month }}</td>
    <td> {{ /*same logic*/ }}</td>
</tr>
@endforeach
Ad
source: stackoverflow.com
Ad