Laravel - 显示名称而不是 id
Laravel - Show name instead of id
我正在尝试编写一些代码,显示 Laravel 中数据库中的数据。但是我有两个 table 值通过外键相互链接,当我尝试在我的页面上显示它时,它只显示 ID 而不是实际名称。我在另一个 Whosebug 问题中看到您应该在您的模型中定义 table 数据值,但这不起作用或者我做错了。
有人可以帮我解决这个问题吗? :)
型号Planet.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Planet extends Model
{
public function solar()
{
return $this->belongsTo(SolarSystems::class, 'id');
}
}
型号SolarSystems.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SolarSystems extends Model
{
public function planet()
{
return $this->belongsTo(Planet::class, 'solar_systems_id');
}
}
PlanetController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Planet;
use App\Models\SolarSystems;
class PlanetController extends Controller
{
public function index()
{
$planets = Planet::all();
$solar = SolarSystems::all();
return view('welcome', ['planets'=>$planets]);
}
public function show($planeet)
{
$planets = Planet::all();
$solar = SolarSystems::all();
$planets = collect($planets)->where('name', $planeet);
return view('welcome', ['planets' => $planets]);
}
}
welcome.blade.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body>
@foreach ($planets as $planeten)
<ul>
<li>{{ ucfirst($planeten->name) }}</li>
<p>{{ ucfirst($planeten->description) }}</p>
<p>{{ ucfirst($planeten->solar_systems_id) }}
</ul>
@endforeach
</body>
</html>
import.sql:
CREATE DATABASE laravel;
USE laravel;
CREATE TABLE planets (
id int(11) AUTO_INCREMENT,
name text,
description text,
size_in_km int(11),
solar_systems_id int(11),
PRIMARY KEY (id)
);
CREATE TABLE solar_systems (
id int(11) AUTO_INCREMENT,
name text,
age_in_years int(11),
PRIMARY KEY (id)
);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('mars', 'Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System, being larger than only Mercury.', 3389, 1);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('venus', 'Venus is the second planet from the Sun. It is named after the Roman goddess of love and beauty.', 6051, 1);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('earth', 'Our home planet is the third planet from the Sun, and the only place we know of so far thats inhabited by living things.', 6371, 1);
INSERT INTO solar_systems (name, age_in_years)
VALUES ('The Milky Way', 1360000000);
这是我现在得到的,但它需要说的不是“1”:'The Milky Way'
替换此行
<p>{{ ucfirst($planeten->solar_systems_id) }}
用这条线
<p>{{ ucfirst($planeten->solar()->name) }}</p>
只需替换这一行
{{ ucfirst($planeten->solar_systems_id) }}
with this line
{{ ucfirst($planeten->solar->name) }}
因为需要从关系中获取名字
您的代码几乎没有问题;
关系
你有 1-N 关系,一个行星属于一个太阳系,一个太阳系有许多行星。
Planet.php
public function solar()
{
return $this->belongsTo(SolarSystems::class, 'solar_systems_id');
}
SolarSystems.php
public function planets()
{
return $this->hasMany(Planet::class, 'solar_systems_id');
}
控制器
PlanetController.php
public function show($planeet)
{
$planets = Planet
::with('solar')
->where('name', $planeet)
->get();
return view('welcome', ['planets' => $planets]);
}
你看到了吗,我正在使用 with
和 where
来优化查询。
with
将预加载关系:well explained in the documentation
where
将向数据库查询添加条件,这将比集合更有效地执行。 Usage
查看
既然已经加载了eager loading的关系,就可以直接访问星球的模型了。
<p>{{ ucfirst($planeten->solar->name) }}
我正在尝试编写一些代码,显示 Laravel 中数据库中的数据。但是我有两个 table 值通过外键相互链接,当我尝试在我的页面上显示它时,它只显示 ID 而不是实际名称。我在另一个 Whosebug 问题中看到您应该在您的模型中定义 table 数据值,但这不起作用或者我做错了。 有人可以帮我解决这个问题吗? :)
型号Planet.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Planet extends Model
{
public function solar()
{
return $this->belongsTo(SolarSystems::class, 'id');
}
}
型号SolarSystems.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SolarSystems extends Model
{
public function planet()
{
return $this->belongsTo(Planet::class, 'solar_systems_id');
}
}
PlanetController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Planet;
use App\Models\SolarSystems;
class PlanetController extends Controller
{
public function index()
{
$planets = Planet::all();
$solar = SolarSystems::all();
return view('welcome', ['planets'=>$planets]);
}
public function show($planeet)
{
$planets = Planet::all();
$solar = SolarSystems::all();
$planets = collect($planets)->where('name', $planeet);
return view('welcome', ['planets' => $planets]);
}
}
welcome.blade.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body>
@foreach ($planets as $planeten)
<ul>
<li>{{ ucfirst($planeten->name) }}</li>
<p>{{ ucfirst($planeten->description) }}</p>
<p>{{ ucfirst($planeten->solar_systems_id) }}
</ul>
@endforeach
</body>
</html>
import.sql:
CREATE DATABASE laravel;
USE laravel;
CREATE TABLE planets (
id int(11) AUTO_INCREMENT,
name text,
description text,
size_in_km int(11),
solar_systems_id int(11),
PRIMARY KEY (id)
);
CREATE TABLE solar_systems (
id int(11) AUTO_INCREMENT,
name text,
age_in_years int(11),
PRIMARY KEY (id)
);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('mars', 'Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System, being larger than only Mercury.', 3389, 1);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('venus', 'Venus is the second planet from the Sun. It is named after the Roman goddess of love and beauty.', 6051, 1);
INSERT INTO planets (name, description, size_in_km, solar_systems_id)
VALUES ('earth', 'Our home planet is the third planet from the Sun, and the only place we know of so far thats inhabited by living things.', 6371, 1);
INSERT INTO solar_systems (name, age_in_years)
VALUES ('The Milky Way', 1360000000);
这是我现在得到的,但它需要说的不是“1”:'The Milky Way'
替换此行
<p>{{ ucfirst($planeten->solar_systems_id) }}
用这条线
<p>{{ ucfirst($planeten->solar()->name) }}</p>
只需替换这一行
{{ ucfirst($planeten->solar_systems_id) }}
with this line
{{ ucfirst($planeten->solar->name) }}
因为需要从关系中获取名字
您的代码几乎没有问题;
关系
你有 1-N 关系,一个行星属于一个太阳系,一个太阳系有许多行星。
Planet.php
public function solar()
{
return $this->belongsTo(SolarSystems::class, 'solar_systems_id');
}
SolarSystems.php
public function planets()
{
return $this->hasMany(Planet::class, 'solar_systems_id');
}
控制器
PlanetController.php
public function show($planeet)
{
$planets = Planet
::with('solar')
->where('name', $planeet)
->get();
return view('welcome', ['planets' => $planets]);
}
你看到了吗,我正在使用 with
和 where
来优化查询。
with
将预加载关系:well explained in the documentationwhere
将向数据库查询添加条件,这将比集合更有效地执行。 Usage
查看
既然已经加载了eager loading的关系,就可以直接访问星球的模型了。
<p>{{ ucfirst($planeten->solar->name) }}