Search the database and return the value - Laravel

Asked

Viewed 1,473 times

0

I’m doing a search in my database, I want to take the value found and save it (are two different tables) but the value is not being saved, is being saved the number 0.

I use this command in my controller:

$clientes = DB::table('cliente')->pluck('razaosocial', 'id');
return view('admin.contrato.create', ['cliente' => $clientes]);

And in my view I use this:

<div class="form-group col-md-5">
     {{ Form::label('cliente', 'Cliente') }}
         <select class="js-example-basic-multiple" name="cliente_id" >
            <option selected>Selecione o cliente</option>
              @foreach ($cliente as $cli)
                <option value="{{ $cli }}">{{ $cli }}</option>
              @endforeach
         </select>
</div>

When I use it, I see the data, but I can’t save it. I don’t know much about PHP, someone knows how I can solve it?

  • Law a little about Eloquent in this link: https://laravel.com/docs/5.6/eloquent and see how the objects are persisted in the bank

2 answers

0

You’re passing the social reason to the database, Try it that way:

<div class="form-group col-md-5">
     {{ Form::label('cliente', 'Cliente') }}
         <select class="js-example-basic-multiple" name="cliente_id" >
            <option selected>Selecione o cliente</option>
              @foreach ($cliente as $key => $value)
                <option value="{{ $value }}">{{ $key }}</option>
              @endforeach
         </select>
</div>
  • Did not give the value passed yet was 0.

0


Try:

@foreach ($cliente as $cli)
    <option value="{{ $cli->id }}">{{ $cli->razaosocial }}</option>
@endforeach
  • It worked, thank you very much !

  • Cool. I now saw that I put an equal sign before '->'. But you must have adjusted it. I’ll edit it. A hint, use Eloquent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.