【问题标题】:Problems passing value from vue to controller laravel将值从 vue 传递到控制器 laravel 的问题
【发布时间】:2026-02-17 11:15:01
【问题描述】:

我又遇到了 Laravel 和 Vue 无法解决的其他问题,我是使用 Vue 的新手,也许这就是我遇到这么多困难的原因。 这是问题所在: 在 Vue 视图中,我得到一个发生变化的值,直到这里一切正常,但问题是,我必须将此值传递给控制器​​,以便进行其他操作,代码如下:

这是触发值变化的选择:

<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>

这就是改变值并理论上将这个值传递给控制器​​的函数:

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)
            })
        }

这是控制器中的函数:

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);
    }
}

我对这个案例的具体路线:

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

我想要做的是获取这个值,传递给控制器​​并做一个选择,这样我就可以填充这个:

<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>

我得到的错误是这样的:

GET http://localhost:8000/portal-cambos/tecelagem/manutencao/cadastro/axios-tipo?0=0500(内部服务器错误)

在 laravel 日志中我得到这个:

laravel.ERROR: 类 Illuminate\Database\Eloquent\Builder 的对象无法转换为字符串

正如我之前所说,我很新,不知道我到底做错了什么。

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 感谢@DerekPollard 帮助我,我已经尝试过遇到与以前相同的错误。
  • 你对这条线做了同样的事情吗? return response()-&gt;json($dados, 422); ?应该是return response()-&gt;json(['dados' =&gt; $dados], 422);
  • 是的,我做了,错误是一样的,我在想这可能是我的 vue 函数中的东西,但我不确定到底是什么。
  • 另外,删除这一行:\Log::info($tipoSelection);
  • Object of class Illuminate\Database\Eloquent\Builder could not be converted to string 是您的控制器中的一个问题 - 您正在尝试将模型转换为字符串,这就是导致问题的原因。很可能是日志语句

标签: php laravel vue.js


【解决方案1】:

@DerekPollard,感谢您帮助我,找出这里的错误...

换行

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

为:

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

现在控制器中的功能是:

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);
    }
}

现在正在按预期工作。 再次感谢。

【讨论】:

    最近更新 更多