[英]Trying to get property of non-object error in hosting, but in local is not error
但是当我尝试托管时最终出现了这样的错误
这是我的模型代码
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected $table = 'transactions';
public function User(){
return $this->belongsTo('App\User');
}
public function Product(){
return $this->belongsTo('App\Product');
}
}
这是我的控制器代码
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Transaction;
use App\Cart;
use App\Product;
use Auth;
class TransactionController extends Controller
{
public function index(){
$userTransaction = Transaction::where('user_id',Auth::user()->id)->get();
$userTransactiondate = Transaction::select('created_at')->where('user_id',Auth::user()->id)->first();
$getpayment = Transaction::select('payment')->where('user_id',Auth::user()->id)->first();
return view('transaction.history',compact('userTransaction','userTransactiondate','getpayment'));
}
}
这是错误的视图代码
@foreach ($userTransaction as $u)
<tr>
<td ><img src="{{public_path('productImg/' . $u->Product->product_image)}}" width="100" height="100" alt=""></td>
<td><br>{{$u->Product->product_name}}</td>
<td style="padding-top:30px;">{{$u->Qty}}</td>
<td> <br>X</td>
<td style="padding-top:30px;">{{$u->Product->Price}}</td>
<td><br>=</td>
<td style="padding-top:30px;">{{$u->Total_Price}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
我应该如何处理此错误? 本地主机和托管中的代码相同,谢谢
这是因为php版本和错误报告配置可能在您的本地服务器和托管服务器中有所不同。 您应该每次检查Product
对象是否存在。 尝试这种方式,可能对您有所帮助。
@foreach ($userTransaction as $u)
<tr>
<td ><img src="{{public_path('productImg/' . optional($u->Product)->product_image)}}" width="100" height="100" alt=""></td>
<td><br>{{optional($u->Product)->product_name}}</td>
<td style="padding-top:30px;">{{$u->Qty}}</td>
<td> <br>X</td>
<td style="padding-top:30px;">{{optional($u->Product)->Price}}</td>
<td><br>=</td>
<td style="padding-top:30px;">{{$u->Total_Price}}</td>
</tr>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.