abaykan
abaykan

Reputation: 155

Undefined Variable in Blade Laravel

I have this in my controller:

public function detail($id) {
    $data = DB::table('data_api')->where('id', $id)->get();
    $carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
    return view('detail', ['data_api' => $data]);
    return view('detail', ['data_carousel' => $carousel]);
}

But when I try to echo-ing, $carousel by {{ $carousel }}, it says not found. But $data work perfectly. Any idea?

Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)

Upvotes: 2

Views: 2454

Answers (8)

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

in my case i use

@if(isset($users))

before my foreach like this example:

                <div class="form-group" id="boardAdminUserIdCon">
                    <p><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> مدیر بورد</p>
                    <select name="boardAdminUserId" id="boardAdminUserId" class="form-control" required="required">
                        <option value="">{{ __('auth.CHOOSEYOURADMIN') }}...</option>
                             @if(isset($users))
                                  @foreach($users as $user)
                                        <option value="{{ $user['id'] }}">{{ $user["name"] }} 
                                        </option>
                                  @endforeach
                             @endif
                    </select>
                </div>

Upvotes: 0

Gustavo Jordan
Gustavo Jordan

Reputation: 49

is that really working now? You tell us you are getting

Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)

And you will get that because you are not passing the variable carousel to your view, you are naming your variables as data_api and data_carousell

Second, you should pass your variables as an asociative array in only one sentence not two view calls like this

return view('detail', ['carousel' => $carousel,'data' => $data]);

Upvotes: 0

PHP Hupp Technologies
PHP Hupp Technologies

Reputation: 1952

You need to return view like below

public function detail($id) {
    $data = DB::table('data_api')->where('id', $id)->get();
    $carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
    return view('detail', compact('data','carousel'));
}

Upvotes: 0

Namoshek
Namoshek

Reputation: 6544

You cannot return two times from a function and expect both to actually return something. After the first return, execution of the function is stopped.

Try returning both variables at once instead:

return view('detail', [
    'data_api' => $api,
    'data_carousel' => $carousel
]);

Upvotes: 1

oreopot
oreopot

Reputation: 3450

Replace your code with following:

public function detail($id) {
    $data = DB::table('data_api')->where('id', $id)->get();
    $carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
    return view('detail')->with('data_api', $data)->with('data_carousel', $carousel);
}

Upvotes: 0

Gulshan S
Gulshan S

Reputation: 1094

Update:

public function detail($id) {
    $data = DB::table('data_api')->where('id', $id)->get();
    $carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
    return view('detail', ['data_carousel' => $carousel,'data_api' => $data]);
}

You are returning two views from the same controller. After the first return execution of code is halt and it will not return the second view. That's why you are unable to get the second view parameters

Upvotes: 1

user728650
user728650

Reputation: 1986

you need to change the double return statement to a single return

return view('detail', ['data_api' => $data]);
return view('detail', ['data_carousel' => $carousel]);

to

return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);

Upvotes: 4

Sunil Kashyap
Sunil Kashyap

Reputation: 2984

you returning view two times that's why only $data_api is available in view,

try this

public function detail($id) {
    $data = DB::table('data_api')->where('id', $id)->get();
    $carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();

    return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);

}

Upvotes: 1

Related Questions