0
votes

I have following problem with laravel. I created an migration called 2017_07_21_190208_create_spielplans_table.php in phpmyadmin the table is called spielplans. After the migration in the cmd I got a model Spielplan.php in the app folder. Then I created a controller (SpielplanController.php) via cmd which is in the folder app/http/controllers and look like that...

 <?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Spielplan;

class SpielplanController extends Controller
{
    public function getSpielplan(){
        $spielplans = Spielplan::all();
        return view('datenAbfrage')->with('messages', $spielplans);
    }
}

After that I created a route in the routes/web.php which looks like this

Route::get('/datenAbfrage', 'SpielplanController@getSpielplan');

And then my view in the view folder datenAbfrage.blade.php ...

    @if(count($spielplans) >0)
    @foreach($spielplans as $spielplan)
    <ul class="list-group">
        <li class="list-group items">Name: {{$spielplan->spieltag}}</li>
    </ul>
    @endforeach
@endif

-> spieltag is one field in the spielplans database!

Now I get an error Undefined variable: spielplans (View: /Applications/XAMPP/xamppfiles/htdocs/Dateneingabe/resources/views/datenAbfrage.blade.php) in line 3

Can anyone tell me what my problem is. For me it is not clear how larval/eloquent make connection the the database table...

I'm new with laravel, but I hope that anyone can give me a solution of my problem

3
->with('messages', $spielplans); will set $messages in your view to the contents of $spielplans. What you wanted was ->with('spielplans', $spielplans) see here for more info - castis

3 Answers

0
votes

Like that the variable in your view will be $message and not $spielplans.

Either you chabge your view to :

   @if(count($messages) >0)
      @foreach($messages as $message)
          <ul class="list-group">
              <li class="list-group items">Name: {{$message->spieltag}}</li>
         </ul>
     @endforeach
   @endif

Or your controller to:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Spielplan;

class SpielplanController extends Controller
{
    public function getSpielplan(){
        $spielplans = Spielplan::all();
        return view('datenAbfrage')->with('spielplans', $spielplans);
    }
}
0
votes

In your controller, you can write:

return view('datenAbfrage')->with('spielplans', $spielplans);

Or,

return view('datenAbfrage')->withSpielplans($spielplans);

Or,

return view('datenAbfrage',['spielplans'=>$spielplans]);

0
votes

I think you need to try this:

class SpielplanController extends Controller
{
    public function getSpielplan(){
        $spielplans = Spielplan::all();
        return view('datenAbfrage',compact('spielplans'));
    }
}

Hope this work for you !!!