Ankündigung

Einklappen
Keine Ankündigung bisher.

Variable wird einer blade nicht übergeben

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [Laravel] Variable wird einer blade nicht übergeben

    Ich mache gerade dieses Tutorial in Laravel 8: https://appdividend.com/2022/01/26/laravel-8-crud/


    Mein Problem ist, dass ich nicht weiß, wieso die eine PHP Variable $games der index-blade nicht übergeben wird. Ich weiß nicht, was ich falsch gemacht hab.

    Im GamesController habe ich schon return view('index',compact('games')); und view('index',array('games' => $games)); und view('index',array('games' => 'games')); ersetzt, hat aber nichts gebracht.

    Hier habe ich eine layout.blade.php:

    PHP-Code:
    <!-- layout.blade.php -->

    <!
    DOCTYPE html>
    <
    html lang="en">
    <
    head>
      <
    meta charset="UTF-8">
      <
    meta name="viewport" content="width=device-width, initial-scale=1.0">
      <
    meta http-equiv="X-UA-Compatible" content="ie=edge">
      <
    title>Laravel 8 CRUD Tutorial</title>
      <
    link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
    </
    head>
    <
    body>
      <
    div class="container">
        @yield(
    'content')
      </
    div>
      <
    script src="{{ asset('js/app.js') }}" type="text/js"></script>
    </body>
    </html> 
    Hier index.blade.php:

    PHP-Code:
    <!-- index.blade.php -->

    @extends(
    'layout')

    @
    section('content')
    <
    style>
      .
    uper {
        
    margin-top40px;
      }
    </
    style>
    <
    div class="uper">
      @if(
    session()->get('success'))
        <
    div class="alert alert-success">
          {{ 
    session()->get('success') }}  
        </
    div><br />
      @endif
      <
    table class="table table-striped">
        <
    thead>
            <
    tr>
              <
    td>ID</td>
              <
    td>Game Name</td>
              <
    td>Game Price</td>
              <
    td colspan="2">Action</td>
            </
    tr>
        </
    thead>
        <
    tbody>
            @foreach(
    $games as $game)
            <
    tr>
                <
    td>{{$game->id}}</td>
                <
    td>{{$game->name}}</td>
                <
    td>{{$game->price}}</td>
                <
    td><a href="{{ route('games.edit', $game->id)}}" class="btn btn-primary">Edit</a></td>
                <
    td>
                    <
    form action="{{ route('games.destroy', $game->id)}}" method="post">
                      @
    csrf
                      
    @method('DELETE')
                      <
    button class="btn btn-danger" type="submit">Delete</button>
                    </
    form>
                </
    td>
            </
    tr>
            @endforeach
        </
    tbody>
      </
    table>
    <
    div>
    @
    endsection 
    Hier der Controller.

    PHP-Code:
    <?php

    // GameController.php

    namespace App\Http\Controllers;

    use 
    Illuminate\Http\Request;
    use 
    App\Models\Game;

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

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

        
    /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        
    public function create()
        {
            return 
    view('create');
        }

        
    /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        
    public function store(Request $request)
        {
            
    $validatedData $request->validate([
                
    'name' => 'required|max:255',
                
    'price' => 'required',
            ]);
            
    $show Game::create($validatedData);

            return 
    redirect('/games')->with('success''Game is successfully saved');
        }

        
    /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        
    public function show($id)
        {
            
    //
        
    }

        
    /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        
    public function edit($id)
        {
            
    $game Game::findOrFail($id);

            return 
    view('edit'compact('game'));
        }

        
    /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        
    public function update(Request $request$id)
        {
            
    $validatedData $request->validate([
                
    'name' => 'required|max:255',
                
    'price' => 'required'
            
    ]);
            
    Game::whereId($id)->update($validatedData);

            return 
    redirect('/games')->with('success''Game Data is successfully updated');
        }

        
    /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        
    public function destroy($id)
        {
            
    $game Game::findOrFail($id);
            
    $game->delete();

            return 
    redirect('/games')->with('success''Game Data is successfully deleted');
        }
    }

  • #2
    Schon einmal geschaut ob Fehler protokolliert werden? Zu finden sind sie in den Logfiles unter "storage/logs/".
    Du kannst auch mal pruefen ob $games ueberhaupt irgendwelche Eintraege enthaelt:

    PHP-Code:
    $games Game::all();
    if (
    $games->isEmpty()) {
        \
    Illuminate\Support\Facades\Log::info('No data!');

        
    // Zeige Fehlerseite
    } else {
        return 
    view('index',compact('games'));

    Kommentar

    Lädt...
    X