首页 > 解决方案 > 类似“类 std 类的对象无法转换为字符串”之类的错误

问题描述

这是我的控制器

function index()
    {
        $name_list = DB::table('customers')
            ->groupBy('first_name')
            ->get(); 
      $name_list2 = DB::table('customers')
      ->where('first_name', $name_list)->first();

     return view('dynamic_field')->with('name_list2',$name_list2); 
    }

这是我的观点,我正在用 javascript 进行检索。

@foreach($name_list2 as $name)
        '<option value="{{ $name->first_name}}">{{ $name->first_name }}</option>'+
        @endforeach

标签: phpmysqllaravel

解决方案


You don't need to foreach $name_list2 as this is a single record being fetched from the database as per your query.

You can try this without using foreach:

{{ $name->first_name}}

Hope it helps!


推荐阅读