Php 无法将Illumb\Database\Query\Builder类的对象转换为字符串
我正在控制器中创建一个函数,我想在其中附加用户和他的命令。首先,我要建立两个模型Php 无法将Illumb\Database\Query\Builder类的对象转换为字符串,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在控制器中创建一个函数,我想在其中附加用户和他的命令。首先,我要建立两个模型用户和订单之间的关系 这是用户型号中的功能: public function orders(){ return $this->belongsToMany('App\Orders', 'user_orders', 'user_id', 'order_id'); } public function users(){ return $this->belongsToMany
用户
和订单
之间的关系
这是用户
型号中的功能:
public function orders(){
return $this->belongsToMany('App\Orders', 'user_orders', 'user_id', 'order_id');
}
public function users(){
return $this->belongsToMany('App\User', 'user_orders', 'order_id', 'user_id');
}
这是订单
型号中的功能:
public function orders(){
return $this->belongsToMany('App\Orders', 'user_orders', 'user_id', 'order_id');
}
public function users(){
return $this->belongsToMany('App\User', 'user_orders', 'order_id', 'user_id');
}
这是新表user\u orders
,我想在其中附加具有我提到的这种关系的用户订单:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_orders', function (Blueprint $table) {
$table->increments('id');
/*$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders');*/
$table->integer('user_id');
$table->integer('order_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_orders');
}
}
最后,这是一个函数,在这个函数中,我将获取带有请求和订单id的用户名,并尝试将订单附加到给定的用户
public function makeOrder(Request $request, $id){
$user = User::where('name', $request['username'])->first();
$findorder = Orders::find($id);
$user->orders()->detach();
$user->orders()->attach(Orders::where('id', $findorder))->first();
return redirect()->back();
}
我还检查了我是否获得了正确的用户名和订单id,代码的前两行工作正常,但当我尝试附加订单时,出现以下错误:
ErrorException in Str.php line 289:
Object of class Illuminate\Database\Query\Builder could not be converted to string
有什么想法吗?这个问题相当简单。即这一行:
$user->orders()->attach(orders::where('id',$findorder))->first()代码>
更具体地说:
Orders::where('id',$findorder)
目前,这将返回一个模型
对象,该对象没有(也不应该)实现\uu toString
方法
长话短说,在Orders::where('id',$findorder)
之后添加->first()
或任何您需要的内容
有关附加/分离的详细信息,请参阅。您应该先获取第一个对象,然后获取ID:
$user->orders()->attach(Orders::where('id', $findorder)->first()->id);
您应该将ID传递给方法,而不是集合或对象
更新
似乎$findorder
是一个订单ID,所以只需执行以下操作:
$user->orders()->attach($findorder);
我一点也不懂你的意思,事实上这和我写的代码是一样的,不是。仔细阅读。你有->first()
在Orders::where('id',$findorder)
之后,我要说的是有Orders::where('id',$findorder)->first()
。因此,您的代码将如下所示:$user->orders()->attach(orders::where('id',$findorder)->first())->first()代码>。请注意,这里有2个first()
而不是1。啊哈,我现在得到了它,我更改了这一行,现在又出现了另一个错误:在null上调用成员函数first()
在这种情况下,Orders::where('id',$findorder)
不返回任何记录first()
在失败时将返回null,这与将返回空集合的get()
不同。我觉得有点恼人的前后矛盾。确保记录存在。@AlexeyMezenin是正确的。我完全忘记了id。他的答案更合适。如果我运行此代码,我会出现以下错误:尝试获取非对象的属性
,这是因为first()
返回null
。确保记录存在。@epowah,这意味着您没有ID=$findorder
的Order
。如果$findorder
是orders
表中真实订单的ID,只需执行以下操作:$user->orders()->attach($findorder))代码>是的,它现在可以工作了!非常感谢,但我还是不明白为什么这段代码不起作用$user->orders()->attach(orders::where('id',$findorder)->first()->id)代码>