0

I have a featureds table (foreign key= product_id) that belongsto products table, Now i want to save some products id to my featured table,its give me a array reasult but i couldnot save it to database

Here is my Controller -->

  public function featuredProduct(Request $request)
   {
       if($request->isMethod('POST')){
         $product_id=$request->all();
         foreach ($product_id as $product) {
            $products[]=$product;
         }
         //dd($products);
       Featured::create($products);
       }
       return view('admin.products.featured');
   }
<form action="{{ route('featuredProduct') }}" method="POST" multiple>
  <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap verticle_middle">
    <thead>
      <tr>
        <th>Product</th>
        <th>Category</th>
        <th>Image</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
    @foreach ($products as $product)
      <tr>
        <td>
          <input id="{{ $product->id }}" value="{{ $product->id }}" type="checkbox" name="product[]">
          <label for=id={{ $product->id }}>{{ $product->product_name }}</label>
        </td>
        <td> {{ $product->category['cat_name'] }} </td>
        <td> <img src="{{asset($product->pro_img) }}" alt="" width="40"> </td>
        <td class="center">
        @if ($product->status === 1)
            <span class="btn btn-primary btn-xs">Published</span>
        @else
            <span class="btn btn-warning btn-xs">Unpublish</span>
        @endif
        </td>
        </tr>
          @endforeach
       </tbody>
    </table>
    <div class="modal-footer">
      <button type="button"class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary waves-effect waves-light">Submit</button>
    </div>
</form>

1 Answer 1

3

You can create each feature inside the for loop.

public function featuredProduct(Request $request)
{
    if($request->isMethod('POST')) {
        foreach($request->all() as $productId) {
            Featured::create([
                'product_id' => $productId,
                ... other data fields.
            ]);
        }
    }
}

Or if you want to save all the features at once.

public function featuredProduct(Request $request)
{
    if($request->isMethod('POST')) {

        $featureds = [];

        foreach($request->all() as $productId) {
            $featureds[] = [
                'product_id' => $productId,
                ... other data fields.
            ]
        }

        DB::table('featureds')->insert($featureds);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@tharaka_Dilshan its return me a blank page 😥
thanks its work for me! love from bangladesh 😘 i am new in proggraming its helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.