dpqvwgr74759 2016-06-18 15:13
浏览 98

Laravel - 如何在orWhere方法中更改表?

I want to do something like this:

$posts= Status::where('users_id',$user->id)->orWhere(DB::table('user_status_share.user_id', $user->id))->orderBy('created_at', 'DESC')->get();

But I'm getting an error: strtolower() expects parameter 1 to be string, object given - how can I change the table in "orWhere" method? Is this possible? If not - how to use 2 tables in one query?

Schema (Status):

public function up()
{
    Schema::create('users_status', function (Blueprint $table) {
        $table->increments('id')->unique();
        $table->longText('status_text');
        $table->integer('users_id')->unsigned();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users_status');
}

Schema (StatusShare):

public function up()
{
    Schema::create('user_status_share', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('status_id');
        $table->integer('user_id');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('user_status_share');
}

Model Status:

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Model;

class Status extends Model
{
    public $timestamps = true;
    protected $table = 'users_status';
    protected $guarded = ['id'];

    public function comments()
    {
        return $this->hasMany(StatusComments::class);
    }

    public function likes()
    {
        return $this->hasMany(StatusLikes::class);
    }

    public function shares()
    {
        return $this->hasMany(StatusShare::class);
    }

}

Model StatusShare:

<?php

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Model;

class StatusShare extends Model
{
    public $timestamps = true;
    protected $table = 'user_status_share';
    protected $guarded = ['id'];

    public function status()
    {
        return $this->hasOne(Status::class);
    }
}
  • 写回答

1条回答 默认 最新

  • donkey199024 2016-06-18 15:51
    关注

    I think you should try something like this

    tweak it , i didnt test it

     $posts = DB::table('status')->where('users_id',$user->id)->orWhere(function ($query) use ($user) {
                    $query->table('user_status_share')->where('user_id', $user->id);
                })
                ->get();
    

    can you share more informations because i have a feeling that these can be reached in a simple way using relations (show us database structure and relations in your models)

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序