Ivan Periša Loga Ivan Periša Loga - 1 year ago 1114
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')
->join('users', 'grad.id', '=', 'users.id_grada')
->where('grad.id', '=', Auth::user()->id_grada)

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>


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


My problem is that $nazivGrada is Array list like this:
and i get error Array to string conversion

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download