2

I have a question about the where command. I have this form element in my database table with a lot of columns and I need to search specific values: customers and type. As shown below...

    <form action="{{ route(shop.find) }}">
    <select class="form-control"  name="customers1" id="customers1">
    @foreach ($customers as $key => $value)
    <option value="{{ $key }}">{{ $value }}</option>
    @endforeach
    </select>
    <select class="form-control" name="type1" id="type1">
    @foreach ($types as $key => $value)
    <option value="{{ $key }}">{{ $value }}</option>
    @endforeach
    </select>
    </form>

In my controller, I am stuck at the where command.

public function find(Request $request){
$customers = DB::table("tbl_customers")->pluck('name','id')->where('name', '=', $request->name);

//This where command is absolutely wrong. I need the right ways to do it. 
$types = DB::table("tbl_types")->pluck('race','raceid')->where('race', '=', $request->race);

return view('shop.find',compact('customers', 'types'));}

I don't know what I need. Or what I need to use. I hope you guys can help.

1 Answer 1

1

When you do pluck()->where() you're loading all rows and then working with the collection. The correct syntax is:

public function find(Request $request)
{
    $customers = DB::table("tbl_customers")->where('name', $request->name)->pluck('name', 'id');
    $types = DB::table("tbl_types")->where('race', $request->race)->pluck('race', 'raceid');
    return view('shop.find', compact('customers', 'types'));
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's worked great. But i need a second question. $customers and $types variables are must in same variable. So i can show them in the page with using foreach. In these case i cant use them with same foreach cycle. What should i do? I hope i can clear. Can you suggest a method or smt. Thanks, and sorry for my bad english.
@AliÖzen please accept the answer if it was helpful.
There collections will have different size. If you still want to iterate over them, you could use the $loop variable. @foreach ($customers as $customer) {{ $customer->name }} {{ $types[$loop->index]->id }} @endforeach

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.