简体   繁体   English

如何设置我的外键以在我的另一个表中抓取一行? Laravel 8 调试

[英]How do I set my foreign key to grab a row in my other table? Laravel 8 Debugging

UPDATED: this is my latest problem .更新:这是我最新的问题 How do I set the 'food_id' to grab 'id' from food table?如何设置“food_id”以从餐桌上获取“id”? I have referenced it to the 'id' but it still doesn't have any value.我已经将它引用到“id”,但它仍然没有任何价值。 Can someone help?有人可以帮忙吗? Thanks!谢谢!

Here is all my codes.这是我所有的代码。 I have two tables of foods and images.我有两张食物和图片表。 and I'm trying to let my 'food_id' from images, to get 'id' which is from food table.我试图让我的'food_id'从图像中获取'id',它来自食物表。 I tried to combine codes of storing a product's data and image(s) in my store function( i did this because i want to use only one form in my view.).我试图在我的商店功能中结合存储产品数据和图像的代码(我这样做是因为我只想在我的视图中使用一种形式。)。 if you're unclear from my codes, feel free to ask:))如果您对我的代码不清楚,请随时询问:))


In migration create_images_table.php 在迁移中 create_images_table.php
<?php

namespace App\Http\Controllers;

use App\Models\Images;
use App\Models\Cart;
use App\Models\Food;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

public function store(Request $request)
{
    $this->validate($request, [
        'filename' => 'required',
        'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    
    if ($request->hasfile('filename')) {
        foreach ($request->file('filename') as $image) {
            $name = $image->getClientOriginalName();
            $image->move(public_path() . '/images', $name); // folder path
            $data[] = $name;
        }
    }

    $Upload_model = new Images;
    $Upload_model->filename = json_encode($data);
    $Upload_model->save();

    $foods = new Food([
        'name'            =>  $request->get('name'),
        'description'     =>  $request->get('description'),
        'price'           =>  $request->get('price'),
        // column name => frontend name
    ]);

    $foods->save();

    session()->flash('success', 'Food successfully added.');
    return redirect()->route('welcome');
}

In my FoodController在我的 FoodController

@section('content')
<div class="w3-container w3-black w3-padding-64 w3-xxlarge" id="cart">
    <div class="w3-content">
        <div class="w3-container w3-padding-32 w3-sand">
            <h1 class="w3-center">
            <span> Add New Food </span>
            </h1>
            <hr class="new1">
            
            <form method="post" action="{{ route('food.store') }}">
                @csrf
                <p><input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Food Name" required name="name"></p>
                <p><input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Description" required name="description"></p>
                <p><input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Price" required name="price"></p>
                <label> Images </label>
                <p><input class="class-control" type="file" placeholder="filename" required name="filename[]" multiple></p>
                <p><button class="w3-button w3-dark-grey w3-block  w3-hover-green" type="submit"> ADD! </button></p>
            </form>   

        </div>
    </div>
</div>
@endsection

In my create.blade.php在我的 create.blade.php

// Food Routes
Route::resources([
    'food' => App\Http\Controllers\FoodController::class,
]);

In my web.php在我的 web.php

 // Food Routes Route::resources([ 'food' => App\Http\Controllers\FoodController::class, ]);

While creating an entry with relationship, you must specify which entry on the food table to relate with food_id column in the images table.在创建具有关系的条目时,您必须指定 food 表上的哪个条目与 images 表中的 food_id 列相关。 You should create food entry before images entry.您应该在图像输入之前创建食物条目。

$foods = new Food([
    'name'            =>  $request->get('name'),
    'description'     =>  $request->get('description'),
    'price'           =>  $request->get('price'),
    // column name => frontend name
]);
$foods->save();

After creating the foods entry, you can specify the food_id from the foods object by,创建食品条目后,您可以通过以下方式从食品 object 中指定 food_id,

$Upload_model = new Images;
$Upload_model->filename = json_encode($data);
$upload_modal->food_id = $foods->id:
$Upload_model->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用其他表中的外键删除具有主键的行? - How do I delete row with primary key using foreign key from other table? 我是否需要在mysql上为表行增量设置外键 - Do I need to set a foreign key for table row increments on mysql 如何将主键放入另一个表的外键中? PHP,MySQL - How Do I Grab And Put Primary Key Into Foreign Key In Another Table? PHP, MySQL 无法使用laravel 5.5将外键保存到我的表中 - unable to save foreign key into my table using laravel 5.5 如何在laravel中使用外键在视图中使用其他表的字段 - how to use a field of other table in view using foreign key in laravel 我可以更改我的 Laravel 关系的默认外键吗? - Can I change default foreign key for my Laravel Relationship? 如何使用 Laravel 在我的客户注册视图中列出我的外键? - How do I list my foreign keys in my customer registration view with Laravel? 如何同时将外键设置为与另一个表的主键相同? - How do I set the foreign key same as primary key of another table concurrently? Laravel 5雄辩地从第二个联接表的外键包含另一个表的行 - Laravel 5 eloquent include the row of the other table from the foreign key of the 2nd joined table 我的MySQL数据库表上的此外键对我有什么作用? - What will this Foreign Key on my MySQL Database table do for me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM