Laravel hasMany 方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34571957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel hasMany method not working
提问by zen
I have 2 tables, users
and websites
. One user can have multiple websites assigned to it.
我有 2 张桌子,users
和websites
. 一个用户可以分配多个网站。
public function websites() {
return $this->hasMany('App\Models\Website');
}
The error I get is:
我得到的错误是:
Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string
Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string
If I try to output it as an array using print_r
I get hundreds of lines of array which I'm assuming is caused by an error somewhere.
如果我尝试将它输出为一个数组,print_r
我会得到数百行数组,我假设这是由某个地方的错误引起的。
Also, why do I have to use App\Models\Website
in the hasMany
method if I specified at the top of User
model use App\Models\Website;
?
另外,如果我在模型顶部指定,为什么我必须App\Models\Website
在hasMany
方法中使用?User
use App\Models\Website;
If I use just Website
in the method, I get class not found error:
如果我只Website
在方法中使用,我会得到 class not found 错误:
Class 'Website' not found
Class 'Website' not found
Not a big deal, but it would be much cleaner not to have to use the full namespace path.
没什么大不了的,但不必使用完整的命名空间路径会更干净。
The code in the controller:
控制器中的代码:
<?php namespace App\Http\Controllers;
use DB;
use App\User;
class DashboardController extends Controller {
/*------------------------------------------------------------------
Dashboard page
-------------------------------------------------------------------*/
public function show() {
//$user = auth()->user();
$user = User::find(5);
return $user->websites();
//return view('dashboard');
}
};
回答by patricus
The websites()
function itself is not causing the error. The error is coming from wherever you are using the function.
该websites()
函数本身不会导致错误。错误来自您使用该函数的任何地方。
The hasMany()
function returns an Illuminate\Database\Eloquent\Relations\HasMany
object. So, when you call $user->websites()
, this will return the HasMany
relationship object. Wherever you are using this function, you are then trying to convert that result to a string, which is the error you're seeing.
该hasMany()
函数返回一个Illuminate\Database\Eloquent\Relations\HasMany
对象。因此,当您调用 时$user->websites()
,这将返回HasMany
关系对象。无论您在哪里使用此函数,您都会尝试将该结果转换为字符串,这就是您看到的错误。
For example:
例如:
$user = User::find(1);
echo $user->websites();
In this example, if you try to echo
the response from the websites()
method, you will get the "could not be converted to string" error.
在此示例中,如果您尝试echo
从该websites()
方法响应,您将收到“无法转换为字符串”错误。
If this doesn't help you enough to resolve the issue, you will need to post the code that is calling the websites()
function for anyone to be able to help further.
如果这不足以帮助您解决问题,您将需要发布调用该websites()
函数的代码,以便任何人都能够进一步提供帮助。
Edit
编辑
"The controller doesn't echo the websites() method, it just returns it." Anything returned by a controller method is converted to a string. This is your issue.
“控制器不回显网站()方法,它只是返回它。” 控制器方法返回的任何内容都将转换为字符串。这是你的问题。
Instead of returning the HasMany
object, you need to return the results of the HasMany
object. There are two main options: call get()
on the relationship to actually get the results and return that, or use the relationship attribute instead of the method.
HasMany
您需要返回HasMany
对象的结果,而不是返回对象。有两个主要选项:调用get()
关系以实际获取结果并返回结果,或者使用关系属性而不是方法。
Option 1:
选项1:
public function show() {
$user = User::find(5);
// call "get()" to get the results
return $user->websites()->get();
}
Option 2:
选项 2:
public function show() {
$user = User::find(5);
// use lazy loaded attribute instead of using relationship method
return $user->websites;
}
Both of the options above will return the same thing: a Collection
of the Website
objects. Since you're returning this from the controller, Laravel will convert this to a string, so your output will be a json array of json Website
objects.
这两个以上选项会返回同样的事情:一Collection
对的Website
对象。由于您从控制器返回 this,Laravel 会将其转换为字符串,因此您的输出将是 jsonWebsite
对象的 json 数组。
回答by whoacowboy
I have a couple of possible solutions:
我有几个可能的解决方案:
First, do you have the inverse relationship defined?
首先,你是否定义了逆关系?
User.php
用户名.php
public function websites() {
return $this->hasMany('App\Models\Website');
}
Website.php
网站.php
public function user()
{
return $this->belongsTo('App\Models\User');
}
Second, have you run composer dump-autoload
in terminal.
其次,您是否composer dump-autoload
在终端中运行。
Note if you want a cleaner interface you can this if you are using >= php 5.5
请注意,如果您想要一个更干净的界面,如果您使用的是 >= php 5.5,则可以这样做
User.php
用户名.php
public function websites() {
return $this->hasMany(App\Models\Website::class);
}
or
或者
use App\Models\Website;
public function user() {
return $this->hasMany(Website::class);
}
Edit 1
编辑 1
Do your models have the right namespace?
你的模型有正确的命名空间吗?
namespace App\Models;
Edit 2
编辑 2
You could try 2 more things.
你可以再尝试 2 件事。
<?php namespace App\Http\Controllers;
use DB;
use App\User;
class DashboardController extends Controller {
public function show() {
$user = User::find(5);
return $user->websites()->toArray();
}
// this one will query the database only once.
public function newShow() {
$user = User::with('websites')->find(5);
return $user->websites->toArray();
}
};