【问题标题】:How can i get one table's value from a another table of Database in Laravel如何从 Laravel 的另一个数据库表中获取一个表的值
【发布时间】:2020-01-10 13:21:21
【问题描述】:

我对 Laravel 完全陌生。 有人可以指导我如何在两个模型BorrowersRequestCars 之间建立关系吗?

我已经在请求状态上完成了它,它也是BorrowersRequest 的外键,它完全可以工作。现在我在想为什么它不适用于Cars

型号

class BorrowerRequest extends Model
{
    public $timestamps = false;

    public function requestStatus() {
        return $this->belongsTo('App\RequestStatus');
    }

    public function requestedCar() {
        return $this->belongsTo('App\Car');
    }
}

这在我的 borrowershistory 页面中。请求状态运行良好。

<tr>
                        <td><p class="text-muted"><small>Car to be Rented:</small></td>
                        <td><h6>{{ $borrower_request->requestedCar->car_name }}</h6></td>
                    </tr>
                    <tr>
                        <td><p class="text-muted"><small>Date of Return:</small></td>
                        <td><h6>{{ $borrower_request->return_date }}</h6></td>
                    </tr>
                    <tr>
                        <td><p class="text-muted"><small>Request Status:</small></td>
                        <td><h6><em>{{ $borrower_request->requestStatus->request_status }}</em> 
 </h6></td>
                    </tr>

这是我的控制器

public function viewBorrowManager()
{
    $borrower_requests = BorrowerRequest::all();
    $request_statuses = RequestStatus::all();
    return view('/borrowmanager', [
        'borrower_requests' => $borrower_requests,
        'request_statuses' => $request_statuses
    ]);
}

和我的迁移

public function up()
{
    Schema::create('borrower_requests', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->unsignedBigInteger('car_id');
        $table->timestamps();
        $table->string('borrowers_name', 50);
        $table->string('email')->unique();
        $table->bigInteger('contact_number');
        $table->date('return_date');
        $table->unsignedBigInteger('request_status_id')->default(0);
        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('car_id')->references('id')->on('cars');
        $table->foreign('request_status_id')->references('id')->on('request_statuses');

    });
}

【问题讨论】:

  • 有错误吗?
  • 嗨,是的,我很抱歉我忘了包括。它说尝试获取非对象的属性“car_name”(查看:C:\Users\Ethel\Documents\zuitte\CSP2\resources\views\borrowershistory.blade.php)

标签: php laravel laravel-5 eloquent orm


【解决方案1】:

某些borrower_request 的汽车不存在。您可以在视图中查看:

<td><p class="text-muted"><small>Car to be Rented:</small></td>
                        <td><h6>{{ $borrower_request->requestedCar ? $borrower_request->requestedCar->car_name : '' }}</h6></td>

或者您可以添加查询以仅获取汽车借款人请求:

$borrower_requests = BorrowerRequest::whereHas('requestedCar')->get();

【讨论】:

  • 嗨,谢谢您的回复 :) 这辆车存在于我的数据库中,我的意思是我已经在我的数据库中保存了一些记录。当我只放 $borrower_request->car_id 时,它只显示汽车的 ID。我试着用你说的 {{ $borrower_request->requestedCar ? $borrower_request->requestedCar->car_name : '' }} 但它只显示我在 ' ' 中输入的内容
【解决方案2】:

如果你想要来自Cars 模型的car_name 列值或与borrower_request 关联的cars 表,你需要这样处理

$borrower_request->requestedCar()->find($borrower_request->car_id)->car_name

简短说明

这里$borrower_request-&gt;requestedCar()这个返回App\Car实例,所以avobe code of line等价于

use App\Car;
$car = Car::find($id);
$car->car_name

$borrower_request 有一列car_id 属于App\Car 模型。

【讨论】:

  • 您好,感谢您的回复。我试过了,效果很好。而且我还发现当我创建 public function requestedCar() { return $this->belongsTo('App\Car', 'car_id);它也可以正常工作。谢谢
猜你喜欢
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
相关资源
最近更新 更多