Mohamed Elbiheiry
Mohamed Elbiheiry

Reputation: 353

getting product id in laravel

hi i'm trying to get the product id from table products to insert it into the wishlist table and this is how the controller is :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\User;
use App\Product;
use DB;

class listController extends Controller
{
    //
    public function getCreate(){

        $list=DB::table('wishlist')->get(['id','product_id']);
        $product=Product::get(['name','id','salary','image_name']);
        $user=Auth::user()->id;
        return view('contents.wishlist')->with('list',$list)
                                        ->with('product',$product)
                                        ->with('user',$user);
    }
    public function postCreate(Request $request){
        $id=$request->input('id');
        $user_id=Auth::user()->id;
        $product_id=Product::find('id');

        DB::table('wishlist')->insert(['id'=>$id,'user_id'=>$user_id,'product_id'=>$product_id]);
        return redirect()->action('listController@postCreate');
    }
}

what is the function i could use to get ? and the view that show the list is that :

<form id="wishlist-view-form" action="{{ action('listController@postCreate') }}" method="post">
    <fieldset>
        <input name="form_key" type="hidden" value="inYgLvzSpOOWWVoP" />
        <table class="data-table" id="wishlist-table">
            <thead>
                <tr>
                    <th></th>
                    <th>Product Details and Comment</th>
                    <th>Add to Cart</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach($product as $p)
                    @foreach($list as $l)
                        @if($p->id == $l->product_id)
                            <tr id="item_6">
                                <td>
                                    <a class="product-image" href="product_detail" title=" {{$p->name}} "> <img src="{{asset('images/')}}/{{$p->image_name}}" width="113" height="113" alt=" {{$p->name}} " /> </a>
                                </td>
                                <td>
                                    <h3 class="product-name"><a href="product_detail" title=" {{$p->name}} "> {{$p->name}} </a></h3>
                                    <div class="description std">
                                        <div class="inner">The must-have tee for his third birthday!</div>
                                    </div>
                                    <textarea name="description[6]" rows="3" cols="5" title="Comment" placeholder="Please, enter your comments..."></textarea>
                                </td>
                                <td>
                                    <div class="cart-cell">
                                        <div class="price-box" itemscope itemtype="http://schema.org/Product">
                                            <span class="regular-price" id="product-price-258"> <span class="price"  content="{{$p->salary}}">${{$p->salary}}</span> </span>
                                        </div>
                                        <div class="add-to-cart-alt">
                                            <input type="text" class="input-text qty validate-not-negative-number" name="qty[6]" value="1" />
                                            <button type="button" title="Add to Cart" onclick="addWItemToCart(6);" class="button btn-cart"><span><span>Add to Cart</span></span></button>
                                        </div>
                                        <p><a class="link-edit" href="#">Edit</a></p>
                                    </div>
                                </td>
                                <td>
                                    <a href="#" title="Remove Item" class="btn-remove btn-remove2">Remove item</a>
                                </td>
                            </tr>
                        @endif
                    @endforeach
                @endforeach
            </tbody>
        </table>
        <div class="buttons-set buttons-set2">
            <button type="submit" name="save_and_share" title="Share Wishlist" class="button btn-share"><span><span>Share Wishlist</span></span></button>
            <button type="button" title="Add All to Cart" class="button btn-add"><span><span>Add All to Cart</span></span></button>
            <button type="submit" name="do" title="Update Wishlist" class="button btn-update"><span><span>Update Wishlist</span></span></button>
        </div>
    </fieldset>
</form>

Upvotes: 4

Views: 3896

Answers (2)

smartrahat
smartrahat

Reputation: 5609

In your html inside foreach add another input to get product id

<input type="hidden" name="product_id" value="{{ $p->id }}" />

In your postCreate() method use this:

$product_id = $request['product_id'];

//or

$product_id = Input::get('product_id');

Upvotes: 1

Mohamed Elbiheiry
Mohamed Elbiheiry

Reputation: 353

the answer of my question is that i hda to make a hidden input file to put the value of the product to, and the controller should be like that :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Auth;
use App\User;
use App\Product;
use DB;

class listController extends Controller
{
    //
    public function getCreate(){

        $list=DB::table('wishlist')->get(['id','product_id','user_id']);
        $product=Product::get(['name','id','salary','image_name']);
        $user=Auth::user()->id;
        return view('contents.wishlist')->with('list',$list)
                                        ->with('product',$product)
                                        ->with('user',$user);
    }
    public function postCreate(Request $request){
        $id=$request->input('id');
        $user_id=Auth::user()->id;
        $product_id=Input::get('product_id');


        DB::table('wishlist')->insert(['id'=>$id,'user_id'=>$user_id,'product_id'=>$product_id]);
        return redirect()->action('listController@postCreate');
    }
}

Upvotes: 1

Related Questions