Php Laravel-使用ID获取播种机中的产品价格

Php Laravel-使用ID获取播种机中的产品价格,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在尝试为我的数据库添加种子。目前,我正在尝试获取ID为$index的产品的价格。我的播种机看起来像这样 foreach(range(1,25) as $index) { DB::table('orderitems')->insert([ 'order_id' => rand(1, 25), 'product_id' => $index, 'price' => '',

我正在尝试为我的数据库添加种子。目前,我正在尝试获取ID为$index的产品的价格。我的播种机看起来像这样

foreach(range(1,25) as $index)
    {
        DB::table('orderitems')->insert([

            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => '',
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),

        ]);
    }
这颗种子我吃了25次。我试图实现的是获得具有给定id的每个产品的价格。例如,product_id为10。我想要ID为10的产品的价格。关系很好。我可以做一些像$product->price这样的事情,效果很好。我怎样才能做到这一点

数据库如下所示

Products
    name
    description
    price
    period

提前谢谢

您可以使用以下类似查询的代码按产品id获取产品价格:

foreach(range(1,25) as $index)
    {
        $product = DB::table("products")->where('id',$index)->first();
        DB::table('orderitems')->insert([

            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $product->price,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),

        ]);
    }

您可以使用以下类似查询的代码按产品id获取产品价格:

foreach(range(1,25) as $index)
    {
        $product = DB::table("products")->where('id',$index)->first();
        DB::table('orderitems')->insert([

            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $product->price,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),

        ]);
    }
你可以这样试试

foreach(range(1,25) as $index)
    {        
        $price = App\Products::->find($index)->price;
        DB::table('orderitems')->insert([
            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $price,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
你可以这样试试

foreach(range(1,25) as $index)
    {        
        $price = App\Products::->find($index)->price;
        DB::table('orderitems')->insert([
            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $price,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
试试这个:

$range = range(1,25);
$productIds = Product::whereIn('id', $range)->pluck('price', 'id');    

foreach($range as $index)
{
        DB::table('orderitems')->insert([    
            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $productIds[$index] ?? null,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),

        ]);
}
试试这个:

$range = range(1,25);
$productIds = Product::whereIn('id', $range)->pluck('price', 'id');    

foreach($range as $index)
{
        DB::table('orderitems')->insert([    
            'order_id' => rand(1, 25),
            'product_id' => $index,
            'price' => $productIds[$index] ?? null,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),

        ]);
}

如果它像你说的那样好,你到底需要什么?如果它像你说的那样好,你到底需要什么?这就是我要找的。非常感谢。这就是我要找的。非常感谢。