javineo's avatar

Undefined property: Illuminate\Database\Eloquent\Collection::$socios

Hello,

I have not been able to get out of this problem, I can not guide myself on how to solve it.

It gives me an error that I do not realize where to fix it.

DB Access:

class Modulo extends Model implements SluggableInterface {


    use SluggableTrait;

    protected $sluggable = array(
        'build_from' => 'modulo',
        'save_to'    => 'slug',
    );



    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'modules';

    public $timestamps = false;

Now ProfileComposer:

    public function compose(View $view)
    {
        $conf= Conf::first();
        $modulos= Modulo::all();
        //$users = User::all();
        $view->with('conf', $conf)->with('modulo', $modulos);
    }

View Code:

        @if(session()->has('es_socio') && session()->get('es_socio') == '1' && (Request::segment(2) === null || Request::segment(2) == 'resumen' || Request::segment(2) == 'consumos' || Request::segment(2) == 'dtohabermax') && $modulo->socios)


And this is a error:

Undefined property: Illuminate\Database\Eloquent\Collection::$socios (View: C:\NeoWebServer\www\proyecto\resources\views\submenus\consultas.blade.php)

If someone can help me, I thank you.

Total Thanks

0 likes
3 replies
tykus's avatar

In your view template, $modulo is a collection of Modulo models, not an individual Modulo instance, so you cannot get the socios property. Question is, did you want to have a single Modulo instance, or all Modulo instances; the solution will depend on which you need (your variable name changes between the controller and view from $modulos to $modulo).

(i) Collection require iteration:

// view template
@foreach ($modulos as $modulo) 
    @if(session()->has('es_socio') && session()->get('es_socio') == '1' && (Request::segment(2) === null || Request::segment(2) == 'resumen' || Request::segment(2) == 'consumos' || Request::segment(2) == 'dtohabermax') && $modulo->socios)
 // other logic
@endforeach

(ii) Do not use all() if you want a single model instance

1 like
javineo's avatar

I solved it by paying attention to your instructions.

Replace:

@if(session()->has('es_socio') && session()->get('es_socio') == '1' && (Request::segment(2) === null || Request::segment(2) == 'resumen' || Request::segment(2) == 'consumos' || Request::segment(2) == 'dtohabermax') && $modulo->socios)

For this:

@if(session()->has('es_socio') && session()->get('es_socio') == '1' && (Request::segment(2) === null || Request::segment(2) == 'resumen' || Request::segment(2) == 'consumos' || Request::segment(2) == 'dtohabermax') && $modulo[0]->socios)

I use Modulo :: all (); as a global variable. In other views I use for each.

Total Thanks

1 like
tykus's avatar
tykus
Best Answer
Level 104

In that case, consider caching to save some queries on every request.

1 like

Please or to participate in this conversation.