Can't Access data from Many to many relationship

can definition
can synonym
can verb
can y
can noun meaning
can band
cant
can't

I have Bus and Route model trying to access route through bus but gets error

Undefined property: stdClass::$route

Controller:

foreach ($Bus as $tBus){

            foreach ($tBus->route as $tBusRoute) {//Undefined property: stdClass::$route
                if($tBusRoute->id == $rId){
                    $BusRouteId = $tBusRoute->pivot->id;            }
            }
            }


Bus Model:

class Bus extends Model
{
    protected $table = 'bus';
    public $primaryKey = 'id';
    public $timestamp = true;


    public function route(){

        return $this->belongsToMany(Route::class , 'bus_route' , 'bus_id' , 'route_id')->withPivot('id');
}

Route Model:

class Route extends Model
{

    protected $table = 'route';
    public $primaryKey = 'id';
    public $timestamp = true;

    public function bus(){

        return $this->belongsToMany(Bus::class , 'bus_route' , 'route_id' , 'bus_id');
    }

Error: Undefined property: stdClass::$route


You are using Query Builder directly and not going through Eloquent to do your query.

To use the model:

$buses = Bus::where('transport_company_id', $tld)->get();

The Query Builder returns a Collection of stdClass objects, hence your error. If you query through the Model you will get a Collection of Model instances.

Laravel 6.0 Docs - Eloquent - Retrieving Models

May I? Can I? - English Grammar - English, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.


Better to Use Eloquent with relationship.

$buses = Bus::with('route')->where('transport_company_id',$tId)->get();

Use can in a sentence, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the


First of all, you are using the Query Builder instead of Eloquent, so your objects will not be casted to models. Try this instead:

$buses = Bus::where('transport_company_id', $tld)->get();

Then, you are looping through the $buses collection and calling the relationship, so I suggest you to Eager Load this values to avoid the N+1 problem:

$buses = Bus::with('route')->where('transport_company_id', $tld)->get();

PS: Given the fact that a Bus may have many Routes you should rename your relationship to plural: routes

Third, you can compare your results like this:

$buses = Bus::with('route')->where('transport_company_id', $tld)->get();

foreach($buses as $bus)
{
    foreach($bus->route as $route)
    {
        // now you can get the $route instance with the pivot data:
        dd($route->pivot->a_pivot_field);
    }
}

CAN (noun) definition and synonyms, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.


Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.


Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Can means someone or something knows how to, is able to, is likely to or has the right to do something. An example of can is someone knowing how to play the piano. An example of can is a cat being able to paint. An example of can is a car that usually starts.


The Official CAN / Spoon Records Website, can. (kæn). n. 1. a container, esp for liquids, usually of thin sheet metal: a petrol can;  Looking for online definition of CAN or what CAN stands for? CAN is listed in the World's largest and most authoritative dictionary database of abbreviations and acronyms The Free Dictionary