Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

megamitch's avatar

2 models multiple loop

I have 2 declarations of my 2 models in my controller

*namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Routing\UrlGenerator; use App\mot_users; use App\branch; class mot extends Controller {

    protected $url;
    public function __construct(UrlGenerator $url)
{
    $this->middleware('auth');
            $this->url = $url;
}

    //replace dashes with spaces functions
    private function replace_dashes($string) {
        return str_replace("-", " ", $string);
        
    }
    //index page, dashboard page
public function index()
{   
        $data = branch::all();
        $breadcrumbs = '<li class="active"><a href="' . $this->url->to('/') . '">Dashboard</a></li>';
        $showpagefilter = "yes";
        return view('pages.home', compact('breadcrumbs', 'showpagefilter', 'data')); 
}*

and I display data with foreach in blade way

everything works fine, except if I declare multipe foreach, an error occurred saying

ErrorException in a0910ce17cd3f20917c51c28f39f2fa5 line 461: Trying to get property of non-object (View: C:\wamp\www\laravel\resources\views\master.blade.php) (View: C:\wamp\www\laravel\resources\views\master.blade.php)

and the line 461 is the 2nd declaration of the foreach.

any ideas whats wrong?

any help, clues, ideas, suggestions, recommendations is greatly appreciated. Thank you!

0 likes
1 reply
bobbybouwmann's avatar
Level 88

You can't give an item in an foreach loop the same name as the object you are looping through

@foreach($data as $data)

Should be

@foreach($data as $item)

And then you can do $item->name

1 like

Please or to participate in this conversation.