Ivan Periša Loga Ivan Periša Loga - 6 months ago 734
PHP Question

Array to string conversion Laravel 5.2

I'm working on project, and I need help with converting array list to string.
This is my controler:

public function editprofila($id)
{
$user = User::findOrFail($id);
$gradovi = Grad::all();

$nazivGrada = DB::table('grad')
->select('grad.naziv_grada')
->join('users', 'grad.id', '=', 'users.id_grada')
->where('grad.id', '=', Auth::user()->id_grada)
->get();


if($user->id == Auth::user()->id || Auth::user()->legenda == 1){

return view('korisnik.uredivanje_profila',compact('user', 'gradovi','nazivGrada' ));


How can select one value from options by nazivGrada (eng. nameCity)



@foreach($gradovi as $grad)

<option value="{{$grad->id}}" selected="">{{$grad->naziv_grada}}</option>

@endforeach

<option value="{{Auth::user()->id_grada}}" selected="selected">{{''.$nazivGrada.''}}</option>

</select>


My problem is that $nazivGrada is Array list like this:
[{"naziv_grada":"Rovinj"}]
and i get error Array to string conversion

Answer

You don't need the extra query for $nazivGrada.

In template you can simply do:

@foreach($gradovi as $grad)
    <option value="{{$grad->id}}" selected="{{ $grad->id == Auth::user()->id_grada ? 'selected' : '' }}">{{$grad->naziv_grada}}</option>
@endforeach
Comments