Reputation: 711
I try to use trait in laravel blade, but didn't work.
This is trait
namespace App\Repositories;
trait Residence
{
public $country=['USA','Japan','Italy'];
public $building=['duplex','condominium'];
}
I try to use in my blade like this:
@inject('Residence', 'App\Repositories\Residence')
@foreach($Residence->country as $country)
{{$country}}
@endforeach
@foreach($Residence->building as $building)
{{$building}}
@endforeach
But I get Target [App\Repositories\Residence] is not instantiable.
error, any suggestion?(I'm using laravel 5.7)
Upvotes: 1
Views: 7097
Reputation: 711
Thank you @Louis R remind that "A trait is supposed to be used by a Class"
So I use the trait in a class, and inject into the blade, it works!
This is trait.
namespace App\Repositories;
trait Residence
{
public $country=['USA','Japan','Italy'];
public $building=['duplex','condominium'];
}
This is a class use the trait.
namespace App\Repositories;
Class ResidenceClassForBlade
{
use Residence;
}
This is blade:
@inject('Residence', 'App\Repositories\ResidenceClassForBlade')
@foreach($Residence->country as $country)
{{$country}}
@endforeach
@foreach($Residence->building as $building)
{{$building}}
@endforeach
Upvotes: 9