1
votes

I'm back with other problem that just can't solve with Laravel and Vue, i'm very new working with Vue and maybe that's why i'm having so much hard time. Here is the problem: In the Vue view, i get one value that changes, until here everything is working but the problem is, i have to pass this value to the controller so i can make some other operations, here's the code:

This is the select that triggers the change of values:

<b-col md="4" sm="12">
                <b-form-group label="Tipo: " label-for="tipo">
                    <input type="hidden" v-model="maintenance.typeId" name="tipoId" id="tipoId">
                    <b-form-select v-model="selecionado" id="tipo" name="tipo" :options="options" :readonly="mode === 'remove'" @change="selecionar"></b-form-select>
                </b-form-group>
            </b-col>

That's the function that'll change the values and theoretically pass this value to the controller:

selecionar(value){
            const kind = this.selecionado
            const url = this.rotatipoautcomp

            axios.get(`${url}?${kind}=` + kind, this.manutencoes).then(res => {
                this.manutencoes = res.data
                console.log(`${url}?${kind}=`)
                console.log(res.data)
            })
        }

This is the function in the controller:

public function axiosServices()
{
    $tipo = Input::get('selecionado');

    $tipoSelection = TecManutencaoTipo::where('tipo', '=', (string)$tipo);

    \Log::info($tipoSelection);

    $tiposSelected = TecManutencaoTipo::select('manutencao AS value')
        ->where('tipo', '=',  $tipoSelection->tipo)
        ->get();

    $dados = [];

    if($tiposSelected) {
        $dados = $tiposSelected;
        //\Log::info($tipo);
        return $dados;
    } else {
        return response()->json($dados, 422);
    }
}

My route for this case in particular:

Route::get('axios-tipo', ['as'=>'tecelagem.manutencao.cadastro.axios-tipo', 'uses' => 'TecelagemManutencaoController@axiosServices']);

What i want to do is to take this value, pass to the controller and do a select so i can populate this:

<b-col md="4" sm="12">
                <b-form-group label="Manutenção: " label-for="manutencao">
                    <input type="hidden" v-model="maintenance.manutencao" name="manutencaoId" id="manutencaoId">
                    <!--<b-form-input id="manutencao" placeholder="Informe o nome da manutenção..." :readonly="mode === 'remove'" />-->
                    <b-form-input list="input-list1" id="manutencao" placeholder="Informe o nome da manutenção..." :readonly="mode === 'remove'"></b-form-input>
                    <b-form-datalist id="input-list1" :options="manutencoes"></b-form-datalist>
                </b-form-group>
            </b-col>

The error that i'm getting is this:

GET http://localhost:8000/portal-cambos/tecelagem/manutencao/cadastro/axios-tipo?0=0 500 (Internal Server Error)

In the laravel log i get this:

laravel.ERROR: Object of class Illuminate\Database\Eloquent\Builder could not be converted to string

As i've said before, i'm very new and, don't know what exactly i'm doing wrong here.

Any help will be very appreciated.

Thanks in advance.

1
Thanks for helping me @DerekPollard, i've tried and get the same error as before.Gabriel Sassaki
did you do the same for this line? return response()->json($dados, 422); ? should be return response()->json(['dados' => $dados], 422);Derek Pollard
Yes i did and the erro is the same, i was thinking that could be something in my vue function but i'm not sure what exactly could be.Gabriel Sassaki
and also, remove this line: \Log::info($tipoSelection);Derek Pollard
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string is an issue in your controller - you're trying to convert a model into a string, which is what's causing the issue. Most likely, it's the log statementDerek Pollard

1 Answers

0
votes

@DerekPollard, thanks for helping me, figure what was the mistake here...

Change the line

axios.get(`${url}?{kind}=` + kind, this.manutencoes).then(res => {

for:

axios.get(`${url}?tipo=` + kind, this.manutencoes).then(res => {

The function in the controller now is:

public function axiosServices()
{
    $tipo = Input::get('tipo');

    //dd($tipo);
    //return response()->json($tipo);

    //\Log::info($tipo);

    //$tipoSelection = $this->TecManutencaoTipoM->where('tipo', '=', $tipo)->get();

    //return response()->json($tipoSelection);

    //\Log::info($tipoSelection);

    $tiposSelected = TecManutencaoTipo::select('manutencao AS value')
        ->where('tipo', '=',  $tipo)
        ->get();

    $dados = [];

    if($tiposSelected) {
        $dados = $tiposSelected;
        //\Log::info($tipo);
        return $dados;
    } else {
        //return response()->json(['dados' => $dados], 422);
        return response()->json($dados, 422);
    }
}

Is working as suposed to be now. Thanks again.