Проблема с отображением данных из моей таблицы «художников» с Laravel - PullRequest
2 голосов
/ 27 апреля 2019

Мне не удается отобразить данные из моей базы данных (я работаю с Laragon).

Я уже пытался изменить некоторые имена (переменные и т. Д.) Здесь и там, но я не могу найти, откуда возникла проблема.

Сначала я ничего не отображал, поэтому мне нужно было изменить:

resources \ views \ artist \ index.php -> resources \ views \ artist s \ index. blade .php

Мне, вероятно, нужно изменить больше вещей в коде, но я не знаю, где.

ПРОСМОТР (views \ artist \ index.blade.php)

@extends('layouts.app')

@section('title', 'Liste des artistes')

@section('content')
    <h1>Liste des {{ $resource }}</h1>

    <table>
        <thead>
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
        </tr>
        </thead>
        <tbody>
        @foreach($artists as $artist)
            <tr>
                <td>{{ $artist->firstname }}</td>
                <td>{{ $artist->lastname }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
@endsection

МАРШРУТЫ (маршруты \ web.php)

<?php

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

Route::get('artists', 'ArtistController@index');

КОНТРОЛЛЕР (Controllers \ ArtistController.php)

<?php

namespace App\Http\Controllers;

use App\Artist;
use Illuminate\Http\Request;

class ArtistController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        /*
         Take the artist from the db
         and send it to a specific template
         */
        $artists = Artist::all();

        return view('artists.index',
            [
            'artists' => $artists,
            'resource' => 'artistes',
            ]);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function edit(Artist $artist)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Artist $artist)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function destroy(Artist $artist)
    {
        //
    }
}

DATABASE (база данных \ ArtistsTableSeeder.php)

<?php

use Illuminate\Database\Seeder;

class ArtistsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $artists = [
            ['firstname'=>'Bob','lastname'=>'Sull'],
            ['firstname'=>'Marc','lastname'=>'Flynn'],
            ['firstname'=>'Fred','lastname'=>'Durand'],
        ];

        foreach ($artists as $a) {
            DB::table('artists')->insert([
                'firstname' => $a['firstname'],
                'lastname' => $a['lastname'],
            ]);
        }
    }
}

Единственное, что отображается в моем браузере при переходе на 127.0.0.1:8000/artists, это:

**Liste des Artistes** 

Firstname Lastname

Но ничего из содержимого моей таблицы Artistes не отображается.

Ответы [ 2 ]

0 голосов
/ 01 мая 2019

Большое спасибо, ребята, проблема была в том, что мне нужно было поменять имена моих колонок ...

Имя Фамилия Имя Имя Фамилия

Теперь данные отображаются правильно.

0 голосов
/ 29 апреля 2019

Изменить функцию контроллера.

public function index()
{
    $artists = Artist::get();
OR  $artists = Artist::all();
    $resource='Artistes';
    return view('artists.index',compact('artists','resource'));
OR  return view('artists.index')->with('resource',$resource)
                                ->with('artists',$artists);

}

На ваш взгляд

@extends('layouts.app')

@section('title', 'Liste des artistes')

@section('content')
<h1>Liste des {{ $resource }}</h1>

<table>
    <thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
    </tr>
    </thead>
    <tbody>
    @foreach($artists as $artist)
        <tr>
            <td>{{ $artist->firstname }}</td>
            <td>{{ $artist->lastname }}</td>
        </tr>
    @endforeach
    </tbody>
</table>
@endsection
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...