簡體   English   中英

在Laravel中獲取針對ID的數據

[英]Get data against id in Laravel

這是我的控制器代碼,用於根據訂單ID顯示訂單詳細信息。 當按下按鈕檢查詳細訂單時,我已成功接收到來自訂單頁面的訂單ID,但在findL之后沒有收到

public function detailorders($id){
   print_r($id); die;
   $this->dorders = orderdetail::find($id);

   $data = (array)$this;

   return view('template/admin/modules/orders/detailorders',compact('data'));
}

這里的代碼顯示了訂單的所有詳細信息,這些都來自訂單頁面,再次是訂單ID

<tr role="row" class="odd">
  <td class="sorting_1">{{$data->id}}</td>
  <td>{{$data->order_id}}</td>
  <td>{{$data->product_id}}</td>
  <td>{{$data->price}}</td>
  <td>{{$data->subtotal}}</td>
  <td>{{$data->quantity}}</td>

orderdetail模型

namespace App;
use Illuminate\Database\Eloquent\Model;

class orderdetail extends Model{

}

我收到此錯誤:

試圖獲取非對象的屬性“ id”
(視圖:C:\\ xampp \\ htdocs \\ shopping_cart \\ resources \\ views \\ template \\ admin \\ modules \\ orders \\ detailorders.blade.php)

問題在於您沒有向視圖發送正確的東西。 看到這一行:

$data = (array)$this;

這意味着您要將Controller轉換為array ,並將其發送到視圖。 由於無法在array上使用對象訪問->... ,因此出現了錯誤,但這不是整個問題。 將您的代碼固定如下:

public function detailorders($id){
  // Removed print_r() and die(); not needed here, and prevents execution.
  $dorder = orderdetail::findOrFail($id); // Changed to findOrFail; will thrown an error if unable to find record.

  return view('template/admin/modules/orders/detailorders',compact('dorder'));
}

然后,在您的視圖文件中,只需引用為:

<tr role="row" class="odd">
  <td class="sorting_1">{{$dorder->id}}</td>
  <td>{{$dorder->order_id}}</td>
  <td>{{$dorder->product_id}}</td>
  <td>{{$dorder->price}}</td>
  <td>{{$dorder->subtotal}}</td>
  <td>{{$dorder->quantity}}</td>

編輯: findOrFail($id)正在檢查where id = ? 在您的orderdetail表中。 要解決此問題,請使用

$dorder = orderdetail::where("order_id", "=", $id)->firstOrFail();

這將檢查where order_id = ? 在您的orderdetail表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM