How to Fix Undefined Offset: 0 in Laravel Blade

laravel Undefined offset: 0

It might be that you are using a get, using post might help. Other than that you are mixing model and controller code. It's always a good idea to seperate these. For instance your redirects should be done inside the controller and not in the model.

  • http://laravel.com/docs/validation
  • http://laravelbook.com/laravel-input-validation/
  • http://culttt.com/2013/07/29/creating-laravel-4-validation-services/

It's also better to do stuff on $validator->passes() and then else return with errors.

Controller

public function store() {

$data = [
"errors" => null
];

$rules = array(
'car' => array('required', 'unique:insur_docs,car_id')
);

$validation = Validator::make(Input::all(), $rules);

if($validation->passes()) {
$data = new InsurDoc();
$data->ownership_cert = Input::get('ownership_cert');
$data->authoriz = Input::get('authoriz');
$data->drive_permis = Input::get('drive_permis');
$data->sgs = Input::get('sgs');
$data->tpl = Input::get('tpl');
$data->kasko = Input::get('kasko');
$data->inter_permis = Input::get('inter_permis');
$data->car_id = Input::get('car');
$data->save();
return Redirect::to('/');
} else {
$data['errors'] = $validation->errors();
return View::make('pages.insur_docs_create', $data);
}

}

Your errors will be available in your view under $errors. Just do a {{var_dump($errors)}} in your blade template to verify that they are there.

View

@if($errors->count() > 0)
<p>The following errors have occurred:</p>

<ul>
@foreach($errors->all() as $message)
<li>{{$message}}</li>
@endforeach
</ul>
@endif

laravel undefined offset 0

Index Controller

public function show($slug) {
$service = Service::where('s_slug', $slug)->first();
return view('')->with(compact('service'));
}

in Service Blade

<h2 class="font-size-25">{{ $service->s_name}}  </h2>

Undefined offset: 0 in blade Laravel 5.6

I suggest to print the values of $val->u_id into the @foreach to debug your code.

Then the proper way to assign a value to a php variable in blade is using the @php directive.

How to Set Variables in a Laravel Blade Template

https://laravel.com/docs/5.7/blade#php

@foreach($school_data as $val)
{{$val->u_id}}
@php ($datas2 = \App\criteria::where('u_id', $val->u_id)->get()) @endphp
@php (dump($datas2) ) @endphp
@php ($trck2 = $datas2['0']->updated_by_email) @endphp
@endforeach

Blade returns undefined offset

The error point out that there was a issue while compiling blade file possibly because of syntax error.
So, just wrap the foreach variable inside paranthesis and the issue should be fixed.

@extends('public.layout')

@section('content')
Companies
@foreach ($companies as $company)
{{ $company->title }}
@endforeach
@stop


Related Topics



Leave a reply



Submit