使用Laravel中控制器/视图中模型的函数 [英] Using a function from a model in a controller/view in Laravel
问题描述
我试图从模型中的函数中查询的查询中访问数据。我试图调用控制器内的函数,然后将数据发送到视图。到目前为止,我一直没有成功。以下是代码:
模型:Fanartist.php
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');
}
控制器:FansController.php
public function getHome(){
return View :: make('fans.home')
- > with ',Fan :: all())
- > with('fanartists',Fanartist :: fan_likes());
}
查看:
@foreach($ fanartists as $ fanartist)
{{$ fanartist}}
@endforeach
当我运行这个,我得到的错误:
非静态方法Fanartist :: fan_likes()不应静态调用,假设$ this来自不兼容的上下文
感谢您的帮助和建议。
更新:
新错误。我现在回来了,但现在,尝试运行这个:
@foreach($ fanartists as $ fanartist)
{{$ fanartist-> artists.id}}
@endforeach
我得到错误:
log.ERROR:异常'ErrorException'消息'尝试获取非对象的属性'在/ Applications / MAMP / htdocs / crowdsets / laravel-master / app / storage / views / 2ed7fc8952dab08cf4cb4f4e3d40d1ab:100
UPDATE 2:
@foreach($ fanartists as $ fanartist)
<?php var_dump($ fanartist); ?>
@endforeach
我得到以下输出:
NULL数组(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)fanartistsarray(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)和}}}} 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)和}} NULL NULL NULL NULL NULL
Fanartist :: fan_likes()
不应该被静态调用,但如果你想静态调用它,然后将你的fan_likes函数更改为静态。
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;
}
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
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;
}
这篇关于使用Laravel中控制器/视图中模型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!