vendredi 21 juillet 2017

Trying to get property of non-object in laravel blade

I'm having this error with a @foreach in a blade view: Trying to get property of non-object

This is the model used in the controller

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categorias extends Model
{
    protected $table = 'categorias';
    protected $fillable = ['categoria'];
    protected $guarded = ['id_categoria'];
}

This is the part involved in my controller

use App\Categorias;

...

public function create()
{
    $category = Categorias::pluck('categoria','id_categoria');

    return view('convocatorias.create',compact('category'));
}

This what i have in my convocatorias.create (blade)

<div class="form-group">
      <label class="col-md-2 control-label" for="selectbasic">Categoría</label>
      <div class="col-md-6">
        <select id="category" class="form-control input-lg" name="category" value="">
          <option value="">Seleccionar</option>
          @foreach($category as $cat)
              <option value=""></option>
          @endforeach
        </select>
      </div>
      @if ($errors->has('category'))
          <span class="help-block">
              <strong></strong>
          </span>
      @endif
    </div>

If i do a in the view it returns:

Collection {#339 ▼
#items: array:2 [▼
1 => "musicians wanted"
2 => "musicians offered"
 ]
}

Am i missing something? (laravel 5.4)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire