简体   繁体   中英

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? I have referenced it to the 'id' but it still doesn't have any value. 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. 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
<?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

@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

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

In my 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. 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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM