[英]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:))
如果您对我的代码不清楚,请随时询问:))
<?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.