1

I am new in laravel. i have a table but dont have model for this. I am using below to fetch record.

$classes = DB::table('school_classes')->get();
return view('classes', ['allClass' => $classes]);

it returns below:

[{"id":1,"register_school_id":1,"class":"I","section":"A","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},
{"id":2,"register_school_id":1,"class":"I","section":"B","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},
{"id":3,"register_school_id":1,"class":"I","section":"C","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},    {"id":4,"register_school_id":1,"class":"I","section":"D","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},        
{"id":5,"register_school_id":1,"class":"I","section":"E","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"}]

how I can pick the value separately of each item... i tried foreach but getting error. plz help. thanks in advance.

1
  • In Laravel, if you echo out objects, they are automatically translated to json. Try the dump and die helper dd($allClass); to see the real object. Make use of models. It will greatly speed up the coding of your project. Commented Mar 20, 2020 at 8:38

2 Answers 2

3

The query builder returns a Illuminate\Support\Collection instance. You can access the collection as below:

@foreach($allClass as $class)
    {{ $class->id }}
@endforeach

Also check Collections

Sign up to request clarification or add additional context in comments.

Comments

0

You want to get data in array format, then you can use "toArray" & also easily access data using foreach loop Laravel function.

$classes = DB::table('school_classes')->get()->toArray();

return view('classes', ['allClass' => $classes]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.