【问题标题】:Object of class Illuminate\Database\Query\Builder could not be converted to string类 Illuminate\Database\Query\Builder 的对象无法转换为字符串
【发布时间】:2017-03-15 17:26:06
【问题描述】:

我正在控制器中创建功能,我想在其中附加用户和他的订单。首先,我在 UserOrders 两个模型之间建立关系。

这是User模型中的函数:

public function orders(){
        return $this->belongsToMany('App\Orders', 'user_orders', 'user_id', 'order_id');

    }

这是Orders 模型中的函数:

public function users(){
      return $this->belongsToMany('App\User', 'user_orders', 'order_id', 'user_id');
   }

这是新表user_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

有什么想法吗?

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    你应该得到第一个对象,然后得到一个 ID:

    $user->orders()->attach(Orders::where('id', $findorder)->first()->id);
    

    您应该将 ID 传递给 attach() 方法,而不是集合或对象。

    更新

    看来$findorder是一个订单ID,就这样吧:

    $user->orders()->attach($findorder);
    

    【讨论】:

    • 如果我运行这段代码,我会遇到这个错误:Trying to get property of non-object
    • 那是因为first() 返回null。确保记录存在。
    • @epowah,这意味着您没有 ID = $findorderOrder
    • 如果$findorderorders 表中的真实订单ID,只需这样做:$user->orders()->attach($findorder);
    • 是的,它现在可以工作了!非常感谢,但我还是不明白为什么这段代码不起作用$user->orders()->attach(Orders::where('id', $findorder)->first()->id);
    【解决方案2】:

    问题很简单。即这一行:

    $user->orders()->attach(Orders::where('id', $findorder))->first();

    更具体地说:

    Orders::where('id', $findorder)

    就目前而言,这将返回一个Model 对象,它没有(也不应该)实现__toString 方法。

    长话短说,在Orders::where('id', $findorder) 之后添加->first() 或您需要的任何内容。


    有关附加/分离的更多详细信息,请参阅here

    【讨论】:

    • 我根本没看懂你,其实我写的代码就是这样的..
    • 不是。仔细阅读。你有->first()Orders::where('id', $findorder)之后,我说的是Orders::where('id', $findorder)->first()。所以你的代码看起来像:$user->orders()->attach(Orders::where('id', $findorder)->first())->first();。请注意,有 2 个first() 而不是 1 个。
    • 啊哈,我现在明白了,我改变了这一行,现在又出现了另一个错误:Call to a member function first() on null
    • 在这种情况下,Orders::where('id', $findorder) 不会返回任何记录。 first() 将在失败的情况下返回 null,这与 ->get() 不同,它将返回一个空集合。我觉得有点烦人的不一致。确保记录存在。
    • @AlexeyMezenin 是正确的。我完全忘记了身份证。他的回答更合适。
    猜你喜欢
    • 2016-05-13
    • 2020-12-01
    • 2019-11-29
    • 2020-05-13
    • 2022-01-24
    • 2016-01-01
    • 2018-06-16
    • 2017-07-02
    • 2015-09-23
    相关资源
    最近更新 更多