0

Sorry, I'm new to developing on Laravel. I'm trying to show info contained in the database on my page. But it can't find the variable holding all the data. I can see the info in Tinker, but i can't seem to deplay is.

I posted some pictures so you can have a look. I'd love to hear your feedback.

Images: https://i.stack.imgur.com/ynfkP.jpg

Code:

Route:

<?php

Route::get('/', function () {
    return view('index');
});

Route::resource('complaints', 'ComplaintController');

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Complaint;

class ComplaintController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $complaints = Complaint::all();

        return view('index', compact('complaints'));
    }

Blade:

@extends('layout')

@section('title','Welcome')

@section('content')

{{-- @foreach ($complaints as $complaint)
    <h1>{{ $complaint->title }}</h1>
    <p>{{ $complaint->body }}</p>
@endforeach --}}

{{ $complaints }}


@endsection
MBT
  • 21,733
  • 19
  • 84
  • 102
STechmo
  • 3
  • 2
  • 3
    It would be much easier to help if you posted your code as text rather than images. – Matt Nov 12 '18 at 10:48
  • Add code here too, which contains route, controller and blade file. – Kamal Paliwal Nov 12 '18 at 10:48
  • your code is ok , did you set your database config in .env ? – parastoo Nov 12 '18 at 10:55
  • Thank you for your feedback, Posting 3 different code tags does not work on stackoverflow, so i made a Pastebin for you to take a look: pastebin.com/dWv6YxEq The database is correctly configured because the POST method already works for adding new items into DB. – STechmo Nov 12 '18 at 11:22
  • 1
    your route is `/complaints` but you visited `/` in the pictures – Amir Nov 12 '18 at 11:25
  • the image you've posted is missed... – DaFois Nov 12 '18 at 11:26

4 Answers4

0

Try it with another syntax like below:

public function index() {
    $complaints = Complaint::all();
    return view('index')->with(compact('complaints'));
}

or

 public function index() {
    $complaints = Complaint::all();
    return view('index')->with('complaints', $complaints);
}
0

As @amirhosseindz said
When you're visiting this url : http://127.0.0.1:8000/complaints it will work because you're hitting

Route::resource('complaints', 'ComplaintController');

But when you visit this url : http://127.0.0.1:8000

you're hitting this action:

Route::get('/', function () {
    return view('index');
});

where $complaints doesn't exists

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
0

You should try this:

Your Controller

public function index() {
    $complaints = Complaint::all();
    return view('index',compact('complaints'));
}

Your View (index.blade.php)

@extends('layout')

@section('title','Welcome')

@section('content')

  @if(isset($complaints))
   @foreach ($complaints as $complaint)
    <h1>{{ $complaint->title }}</h1>
    <p>{{ $complaint->body }}</p>
   @endforeach
  @endif


@endsection
0

You are not routing to the correct function in your controller. Try this

Route::resource('complaints', 'ComplaintController@index');
Mike Miller
  • 3,071
  • 3
  • 25
  • 32