2

I have a table statuses (columns id, statusename) and want display a "select" list based by this table dynamically. My model:

namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Statuses extends Model
    {
       protected $table = "CaseStatus";
    }

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Statuses;
class StatusesList extends Controller
{
    public function getstatuseslist() {
        $list = Statuses::all();
        return view('forms.statuses')->with('data', $list);
    }
}

statuses.blade.php:

<select name="Statuses">
@foreach ($data as $page)
<option value="{{ $page->ID }}">{{ $page->StatuseName }}</option>
@endforeach
</select>

Route:

Route::any('/list/statuses','StatusesList@getstatuseslist');

If i'm open url http://myproject/list/statuses - it work fine, i see the dropdown list. But if I include my statuses.blade.php in a form in another template:

 @include('forms.statuses')

I get the error

"Undefined variable data".

How to include it correctly? My Laravel version is 5.4.

3
  • 1
    where you had put @include('forms.statuses') , i mean in which .blade file? Commented Jun 3, 2017 at 5:54
  • i'm include it in my views\forms\dialog-mainform.blade.php Commented Jun 3, 2017 at 6:06
  • in that case you need to call return view('forms.dialog-mainform')->with('data', $list); Commented Jun 3, 2017 at 6:13

2 Answers 2

2

first of all models are meant to get raw's and not tables if you have table statuses your model name should be status.

the issue of your function not working in another blade is because your another blade controller does not connected to your model so it doesn't know where to get info that you asking for. to solve that issue it's easy, just add your model codes this->

public function getstatuseslist() {
        $list = Statuses::all();
        return view('your_other_view')->with('data', $list);
    }

into that page controller.

don't forget to call your model in your second controller as well.

use App\Statuses;

after that will work just fine in your second blade also.

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

Comments

0

I think your $data is passed to your 'forms.statuses' view only, when it's pass from that controller method you defined.

If you want to use this data for other "parent" views that include 'forms.statuses', you have to pass it again in the controller that returns that 'parent' view.

There's a way you can make it persist in many views. See 'view composer' for details. Here's the link to view composers at Laravel doc: https://laravel.com/docs/5.4/views#view-composers

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.