在Laravel中显示“艺术家”表中的数据时出现问题

时间:2019-04-27 13:33:06

标签: php laravel

我无法显示数据库中的数据(我正在使用Laragon)。

我已经试图在各处更改一些名称(变量等),但是我找不到问题的根源。

我无法首先显示任何内容,因此我需要进行更改:

resources \ views \ artist \ index.php-> resources \ views \ artist s \ index。刀片 .php

我可能需要更改代码中的更多内容,但我不知道该在哪里。

VIEW(views \ artists \ 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

路线(routes \ web.php)

<?php

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

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

CONTROLLER(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

但是我的Artists表的内容中什么也没有显示。

2 个答案:

答案 0 :(得分:0)

更改控制器功能。

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

答案 1 :(得分:0)

非常感谢你们,问题在于我需要更改列的名称...

姓氏到姓氏

数据现在可以正确显示。