Php 如何打印特定于id的数据?

Php 如何打印特定于id的数据?,php,laravel,Php,Laravel,我正在尝试打印与我的餐厅相关的菜肴。每道菜都分配了一个餐厅id 每个餐厅都分配了一个ID 餐馆迁移 Schema::create('restaurants', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->timestamps(); }); Schema::create('dishes', fu

我正在尝试打印与我的餐厅相关的菜肴。每道菜都分配了一个
餐厅id

每个餐厅都分配了一个
ID

餐馆迁移

Schema::create('restaurants', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
});
Schema::create('dishes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->float('price');
        $table->integer('restaurant_id');
        $table->string('image');
        $table->timestamps();
});
碟形迁移

Schema::create('restaurants', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
});
Schema::create('dishes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->float('price');
        $table->integer('restaurant_id');
        $table->string('image');
        $table->timestamps();
});
餐厅播种机

DB::table('restaurants')->insert([
        'name' => 'Bellos Pizzeria',
        'updated_at' => DB::raw('CURRENT_TIMESTAMP'),
    ]);

    DB::table('restaurants')->insert([
        'name' => 'McDonalds',
        'updated_at' => DB::raw('CURRENT_TIMESTAMP'),
    ]);

    DB::table('restaurants')->insert([
        'name' => 'Ericos',
        'updated_at' => DB::raw('CURRENT_TIMESTAMP'),
    ]);
圆盘播种机

    DB::table('dishes')->insert([
        'name' => 'Butter Chicken',
        'price' => '12',
        'restaurant_id' => 1,
        'image' => 'dishes_images/default.png',
        'updated_at' => DB::raw('CURRENT_TIMESTAMP'),
    ]);

    DB::table('dishes')->insert([
        'name' => 'Hamburger',
        'price' => '10',
        'restaurant_id' => 2,
        'image' => 'dishes_images/default.png',
        'updated_at' => DB::raw('CURRENT_TIMESTAMP'),
    ]);
个人餐厅视图上的html

@section('content')
    <h1> {{$restaurant->name}} </h1>

    <a href="/assignment2/public/restaurant"> back </a>
@endsection  
@节(“内容”)
{{$restaurant->name}
@端部

我正在打印与餐厅相关的菜肴。例如,“黄油鸡”(
id=1
)将被列在餐厅“Bellos Pizzeria”(
id=1
)上。

您应该尝试使用laravel关系。像

创建
餐厅
菜肴
模型

在您的餐厅模式中:

class Restaurants extends Model
{
    function dishes() {
      return $this->hasMany('App\Dishes');
    }
}
在你的盘子里

class Dishes extends Model
{
    function restaurants() {
      return $this->hasMany('App\Restaurants');
    }
}

你应该试着使用拉威尔关系。像

创建
餐厅
菜肴
模型

在您的餐厅模式中:

class Restaurants extends Model
{
    function dishes() {
      return $this->hasMany('App\Dishes');
    }
}
在你的盘子里

class Dishes extends Model
{
    function restaurants() {
      return $this->hasMany('App\Restaurants');
    }
}

餐厅
模型中编写关系代码。看到你上面的问题,我明白这种关系是一对多的关系。在这种情况下,在餐厅模型中写下这句话

Restaurant.php

public function dishes(){
    return $this->hasMany(Dish::class);
    //it define the relation type between Restaurant and Dish model
}
刀片文件

<h1> {{$restaurant->name}} </h1>

<ul>
    @foreach($restaurant->dishes as $dish)
        <li>{{ $dish->name }}</li>
    @endforeach
</ul>
{{$restaurant->name}
    @foreach($restaurant->菜肴作为$dish)
  • {{$dish->name}
  • @endforeach
$restaurant->diseases
将返回与该餐厅关联的所有相关菜肴的数组/集合。使用
@foreach
显示所有菜肴


使用您自己的
Html
,我使用
ul
li
作为示例。

餐厅
模型中编写关系代码。看到你上面的问题,我明白这种关系是一对多的关系。在这种情况下,在餐厅模型中写下这句话

Restaurant.php

public function dishes(){
    return $this->hasMany(Dish::class);
    //it define the relation type between Restaurant and Dish model
}
刀片文件

<h1> {{$restaurant->name}} </h1>

<ul>
    @foreach($restaurant->dishes as $dish)
        <li>{{ $dish->name }}</li>
    @endforeach
</ul>
{{$restaurant->name}
    @foreach($restaurant->菜肴作为$dish)
  • {{$dish->name}
  • @endforeach
$restaurant->diseases
将返回与该餐厅关联的所有相关菜肴的数组/集合。使用
@foreach
显示所有菜肴


使用你自己的
Html
,我以
ul
li
为例。

你应该先建立关系,然后才能访问你的欲望数据你在餐厅和菜肴之间建立了雄辩的关系吗?你必须实现关系你应该首先建立关系,然后你可以访问你的欲望数据。你是否在餐厅和菜肴之间建立了一种雄辩的关系?你必须实现这种关系