How to fetch price through another table where $product->name is equal to $order->product in laravel?

I have 2 tables: product and order. In order, I want to get the price through the product table.

Form

<div id="TextBoxDiv1">
    <input type="hidden" value="{{ Auth::user()->name }}" name="order_by">
    <input class="text email" type="text" name="quantity[]"
           placeholder="Quantity" required="" id="textbox1"
           style="width: 230px; height: 41px; margin-top: 10px">
    <div class="form-group row">
        <div class="col-md-9">
            <select class="select2 form-select shadow-none"
                    style="width: 230px; height: 41px; margin-top: 10px"
                    name="product[]">
                <option>Select Product</option>
                @foreach ($products as $product)
                    <option value="{{ $product->name }}">{{ $product->name }}</option>
                @endforeach
            </select>
        </div>
    </div>
</div>

Below is my OrderController. I do not understand how to get the price as the value that we get from the form is in an array, and I want to convert it to a string, and through that name, I want the price through the products table.

public function store(Request $request)
{
    $products = Product::all();
    $request->validate([
        'order_by' => 'required',
        'product' => 'required',
        'quantity' => 'required',
        'status' => 'sometimes',
        'price' => 'sometimes',
    ]);

    $order = new Order();
    $order->product = implode(',', $request->product);
    $order->quantity = implode(',', $request->quantity);
    // $order->price = ;
    $order->order_by = $request->order_by;
    $order->status = $request->status;
    $order->save();
    
    return redirect()->route('user/pending', 
        compact('products'))->withSuccess('Done');
}

Orders table
How to fetch price through another table where $product->name is equal to $order->product in laravel?

Products table
How to fetch price through another table where $product->name is equal to $order->product in laravel?

Order model

use HasFactory;

protected $table = 'orders';

protected $fillable = [
    'order_by',
    'product',
    'quantity',
];

Product model

use HasFactory;

protected $table = 'products';

protected $fillable = [
    'name',
    'brand',
    'price',
    'detail',
    'image',
];

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should store product_id as foreign key in order table and make relationship between order and product. And you do not have to store product price and name in order table.

Order Model

use HasFactory;

protected $table = 'orders';

protected $fillable = [
    'order_by',
    'product',
    'quantity',
];

public function product()  
{
  return $this->belongsTo('AppProduct', 'product_id'); 
}

And you have to get product name and price like this

{{$order->product->name}}
{{$order->product->price}}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x