dimanche 27 décembre 2015

Get the ID of a selected value from a dropdown selectbox using Laravel 5.1

I've been on this for a while, what I want to do is to get the id of the selected value from a select box. Using this snippet, it doesn't get the id and I wonder why. I'm confused what what could be wrong

This code is to get the id of the selected value:

private static function compareCompany(ProductRequest $productRequest){
            $companyPicked = $productRequest->companyname;
            $listedCompanies = Company::where('user_id', '=', Auth::user()->id);
            $companies = new Company;
            if($companies->user_id === Auth::user()->id)
            {   
                foreach($listedCompanies as $company) {
                    if($company->companyname === $companyPicked)
                    {
                        return $company->id;
                    } 
                }
            }
        }

This is to create a new product using the id returned for a company

public function store(ProductRequest $productRequest)
    {
       $product = new Product;
        $company = new Company;
       if($productRequest->isMethod('post')){

       $product->user_id     = Auth::user()->id;
       $product->company_id  = $this->compareCompany($productRequest);
       $product->companyname = $productRequest->companyname;
       $product->productname = $productRequest->productname;

       $product->save();
       return redirect()->route('companyindex')->with('message', 'Your question has been posted.');
       }else{
            return redirect('company-create')->withErrors($productRequest)->withInput();
        }
    }

THis is the view:

<p>{!! Form::select('companyname', array('' => 'Select a Company') + $listCompanies) !!} </p>

THis is the code used in binding the returned value to the view:

public function create()
    {

      $listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'companyname')->toArray();
        return view('product.create')
        ->with('listCompanies', $listCompanies);
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire