0

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?

0

4 Answers 4

3

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

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

3 Comments

I felt so depressed I didn't realize that, thanks bro, it's work for me :D
with quotes 'apartment.index'
@AbuUbaidillah if this worked then please make my answer accepted
1

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

Comments

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

1 Comment

Please avoid code-only answers. Add a brief text explaining how this solves the issue.
-1

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

4 Comments

i'am using third option, but i confused to display apartment foreach, i'am use @foreach ($apartment as $apartment) ... @endforeach, is that wrong? error message: Undefined variable: apartment
have you tried to print variable in blade file like this @php print_r($variableName); exit;@endphp
You should use NOT just $apartment, but $data['apartment'] because you pass to template $data array, and it contains key apartment
fyi, when using return view('apartment.index',$data); (and $data is an array like OP had it) there is no need to use $data[..] in your blade file, $datas keys will be available as variables inside blade

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.