Abu Ubaidillah
Abu Ubaidillah

Reputation: 51

Pass Data With Array in Laravel 6?

In CodeIgniter 3.x, we can pass data with array by using this code:

$data=array(
   'apartment' => $this->apartmentmodel->get_all(),
   'title' => "List Apartement"
);
$this->load->view('apartment/index',$data);

But, this code is not working when implement on my Laravel 6 project

$data=array(
   'apartment' => DB::table('apartment')->get();,
   'title' => "List Apartement"
);
return view(apartment.index,$data);

What is the problem and how to fix it? Anyone can help me?

Upvotes: 0

Views: 227

Answers (4)

Priyank Panchal
Priyank Panchal

Reputation: 119

In Laravel we can use eloquent for fetch data from database.

$data=[
   'apartment' => Apartment::get();,
   'title' => "List Apartment"
];

return view('apartment.index', ['data' => $data]);

It will help full for you.

Thanks

PHpanchal

Upvotes: 1

Abdul Moiz
Abdul Moiz

Reputation: 502

Just remove ; from line number 2 before , your it will be work

$data=array(
   'apartment' => DB::table('apartment')->get(),
   'title' => "List Apartement"
);
return view('apartment.index', $data);

or you can use compact too, see below docs https://laravel.com/docs/6.x/views

Upvotes: 3

Rosnowsky
Rosnowsky

Reputation: 223

You can use compact() method, and you have an error (if this is project code).

Try:

$apartment = DB::table('apartment')->get();
$title = 'List Apartement';

return view('apartment.index', compact('apartment', 'title'));

Or

$data=[
   'apartment' => DB::table('apartment')->get();,
   'title' => "List Apartement"
];

return view('apartment.index', compact('data'));

Or:

$data=[
   'apartment' => DB::table('apartment')->get();,
   'title' => "List Apartement"
];

return view('apartment.index', ['data' => $data]);

Update:

In template file you can use it by calling $data, because in return statement you pass the variable name that will be shared with current template. So, to get some data from your array $data you just need to do:

@foreach($data['apartment'] as $apartment)
{{ $apartment }}
@endforeach

And to get your title use $data['title'] as well

Upvotes: -1

chandan gupta
chandan gupta

Reputation: 81

return view('apartment.index',compact('data'));

Upvotes: 0

Related Questions