0

I'm trying to get my JSON file to go through a loop on test.blade.php

So far, if I print_r in jsonController.php then I can see the decoded JSON file but it'll be at the top of test.blade.php which is not what I want it to do.

I'm sure I'm missing something very obvious but I'm pulling blanks.

jsonController.php

namespace App\Http\Controllers; use Illuminate\Http\Request; class jsonController extends Controller { public function press_kit() { $jsonString = file_get_contents(base_path('resources/views/inc/press-kit.json')); $json = json_decode($jsonString, true); return view('press-kit', $json); } } 

routes/web.php

Route::get('press-kit', 'jsonController@press_kit', function () { return view('press-kit'); }); 

test.blade.php

@for($x = 0; $x < count($json['articles']); $x++) <div class="col-md-4 col-sm-6 col-12"> <div class="card mb-sm-5 mb-3"> <a href="{{ $json['articles'][$x]['url'] }}" target="_blank"> <div class="w-100" style="background-image:url('img/{{ $json['articles'][$x]['thumbnail'] }} ');"></div> <div class="card-body"> <h5 class="card-title">{{ $json['articles'][$x]['name'] }}</h5> <small>{{ $json['articles'][$x]['datePosted'] }}</small> </div> </a> </div> </div> @endfor 
1
  • And what would you expect to happen? Commented Jan 29, 2019 at 13:49

2 Answers 2

2

Remove return view('press-kit');

Route::get('press-kit', 'jsonController@press_kit'); 

Your controller can just return the view

and change return view('press-kit', $json); to return view('press-kit', [ 'json' => $json]);

And you can access it with $json. Also just do an a for each instead of a for loop

@foreach ($json['articles'] as $article) {{$article['url'}} @endforech 

Much cleaner code in the long run.

Sign up to request clarification or add additional context in comments.

Comments

0

Modify your route:

Route::get('press-kit', 'jsonController@press_kit'); 

And in your controller do this:

namespace App\Http\Controllers; use Illuminate\Http\Request; class jsonController extends Controller { public function press_kit() { $jsonString = file_get_contents(base_path('resources/views/inc/press-kit.json')); $json = json_decode($jsonString, true); return view('press-kit', compact('json')); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.