我在使用select选项中的foreach的形式中出现此错误。我无法找到错误的位置。
未定义的变量:类型(查看:/var/www/laravel/resources/views/clases/create.blade.php)
形式:
...
<div class="form-group{{ $errors->has('types') ? ' has-error' : '' }}">
<label class="col-md-2 control-label" for="selectbasic">Doy clases de<em>*</em></label>
<div class="col-md-6">
<select id="type" name="types[]" value="" class="selectpicker form-control show-tick" data-live-search="true" multiple="multiple" multiple title="Seleccionar una o varias opciones" data-size="6" data-max-options="8" data-selected-text-format="count > 3">
@foreach($types as $tp)
<option value="{{$tp->id}}" {{in_array($tp->id, old("types") ?: []) ? "selected": ""}}>{{$tp->type}}</option>
@endforeach
</select>
</div>
@if ($errors->has('types'))
<span class="help-block">
<strong>{{ $errors->first('type') }}</strong>
</span>
@endif
</div>
...
ClaseController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Carbon\Carbon;
use Validator;
use App\Clase;
use App\Province;
use App\Style;
use App\Type;
use App\Instrument;
use Auth;
use Image;
use Illuminate\Validation\Rule;
class ClaseController extends Controller
{
...
public function create()
{
$userid = Auth::user()->id;
$user_clases = \DB::table('clases')
->where('user_id', '=', $userid)
->first();
return view('clases.create',compact('userid'));
}
public function store(Request $request)
{
$validator = Validator::make($request->all(),[
'description' => 'required',
'types' => 'required|required|array|min:1',
'phone' => 'required',
'image' => 'image|mimes:jpeg,bmp,png|max:6144',
'g-recaptcha-response' => 'required|captcha'
]);
$clase = new Clase;
...
$clase->types()->attach($request->types);
$clase->save();
}
模特'Clase':
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
use App\Traits\DatesTranslator;
use App\SessionHistory;
use Carbon\Carbon;
class Clase extends Model
{
use SoftDeletes, DatesTranslator;
public $dates = ['moderated_at','reported_at','deleted_at'];
protected $fillable = ['content','...','...','types'];
...
public function types(){
return $this->belongsToMany('App\Type');
}
...
}
型号'类型':
namespace App;
use Illuminate\Database\Eloquent\Model;
class Type extends Model
{
public $timestamps = false;
protected $fillabe = ['type'];
public function clases(){
return $this->hasMany('App\Clase');
}
}
已经检查了我在控制器中使用的两个模型但是找不到它在foreach中的错误($ types as $ tp)... 任何帮助都要提前感谢。
答案 0 :(得分:0)
<强>控制器强>
public function create()
{
$userClases = Clase::where('user_id', Auth::user()->id);
return view('clases.create',compact('userClases'));
}
<强>形式:强>
<div class="col-md-6">
<select id="type" name="types[]" value="" class="selectpicker form-control show-tick" data-live-search="true" multiple="multiple" multiple title="Seleccionar una o varias opciones" data-size="6" data-max-options="8" data-selected-text-format="count > 3">
@foreach($userClases->types as $tp)
<option value="{{$tp->id}}" {{in_array($tp->id, old("types") ?: []) ? "selected": ""}}>{{$tp->type}}</option>
@endforeach
</select>
</div>