0

I am trying to access data from a query that I have placed within a function in a model. I am trying to call the function within a controller, and then send along that data to a view. So far, I have been unsuccessful. Here is the code:

Model: Fanartist.php

public function fan_likes() {
        $fan_likes = DB::table('fanartists')
                    ->join('artists', 'fanartists.fan_id', '=', 'artists.id')
                    ->where('fanartists.fan_id', '=', Auth::user()->id)
                    ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description');

           }

Controller: FansController.php

public function getHome() {
            return View::make('fans.home')
            ->with('fans', Fan::all())
            ->with('fanartists', Fanartist::fan_likes());

        }

View:

@foreach($fanartists as $fanartist)

{{$fanartist}}

@endforeach

When I run this, I get the error:

Non-static method Fanartist::fan_likes() should not be called statically, assuming $this from incompatible context

Thank you for your help and suggestions.

UPDATE:

New error. I am returning the view, but now, trying to run this:

@foreach($fanartists as $fanartist)
                {{$fanartist->artists.id}}

            @endforeach

I get the error:

log.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in /Applications/MAMP/htdocs/crowdsets/laravel-master/app/storage/views/2ed7fc8952dab08cf4cb4f4e3d40d1ab:100

UPDATE 2:

running:

@foreach($fanartists as $fanartist)
<?php var_dump($fanartist); ?>          
            @endforeach

I get the following output:

NULL array(6) { [0]=> string(10) "artists.id" [1]=> string(18) "artists.stage_name" [2]=> string(12) "artists.city" [3]=> string(13) "artists.state" [4]=> string(18) "artists.image_path" [5]=> string(19) "artists.description" } bool(false) string(10) "fanartists" array(1) { [0]=> object(Illuminate\Database\Query\JoinClause)#201 (3) { ["type"]=> string(5) "inner" ["table"]=> string(7) "artists" ["clauses"]=> array(1) { [0]=> array(4) { ["first"]=> string(17) "fanartists.fan_id" ["operator"]=> string(1) "=" ["second"]=> string(10) "artists.id" ["boolean"]=> string(3) "and" } } } } array(1) { [0]=> array(5) { ["type"]=> string(5) "Basic" ["column"]=> string(17) "fanartists.fan_id" ["operator"]=> string(1) "=" ["value"]=> string(1) "1" ["boolean"]=> string(3) "and" } } NULL NULL NULL NULL NULL NULL
7
  • Error states it all try dumping $fanartists, see what it contains ? Is it an object?? Commented Jul 7, 2013 at 18:55
  • hmm, it won't run the page when I try: <?php var_dump($fanartists); ?> Commented Jul 7, 2013 at 19:14
  • don't know what are you doing?? just try <?print_r($fanartists);?> in your view and comment everything else Commented Jul 7, 2013 at 19:17
  • WHen I run that, I get: The website encountered an error while retrieving crowdtest.dev:8888/fans/home. It may be down for maintenance or configured incorrectly. Commented Jul 7, 2013 at 19:19
  • 1
    let us continue this discussion in chat Commented Jul 7, 2013 at 19:24

2 Answers 2

0

Fanartist::fan_likes() is not supposed to be called statically, but if you want to call it statically then change your fan_likes function to static. for your example

public static function fan_likes() {
    $fan_likes = DB::table('fanartists')
                ->join('artists', 'fanartists.fan_id', '=', 'artists.id')
                ->where('fanartists.fan_id', '=', Auth::user()->id)
                ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description');
return $fan_likes;
       }
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, now I'm getting the error in the view: [2013-07-07 18:17:30] log.ERROR: exception 'ErrorException' with message 'Invalid argument supplied for foreach()' in /Applications/MAMP/htdocs/crowdsets/laravel-master/app/storage/views/2ed7fc8952dab08cf4cb4f4e3d40d1ab:99
Error is in view.Because $fanartists is null.In your fan_likes() function you are not returning anything
Ok, yes that was the issue. I updated my question with a new error: I can't access {{$fanartist->artists.id}} or any of the other columns...
0

if you have a non-static function like this in your MyClass controller:

    public function my_func( ) {
            // do stuff
    return ( $stuff ) ;
}

then in your controller, you can call it with code like this:

$local_obj = MyClass::findOrFail( $myobj_id );
$rval = $local_obj->my_func();

if you want to call it from your view, I think you need to pass the object to the view similar to this:

$data['my_obj'] = $local_obj;
return View::make('view_name.show')->with('data',$data);

and then to use in the view use something like this:

{{{$data['my_obj']->my_func() }}}

(above is copied and edited from working code - apols if i've made a typo )

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.