亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

鑒于一對一多態關系,無法檢索數據

鑒于一對一多態關系,無法檢索數據

PHP
偶然的你 2023-06-24 17:04:21
我有一個書籍表和一個圖像表,將來我還將有一個博客表。這就是為什么我想在書籍、博客和圖像表之間創建多態關系。但我的問題是,當我厭倦了從關系中檢索圖書圖像 url 的數據時,home.blade.php我@foreach得到了以下結果:Call to a member function getImage() on null (View: F:\xampp\htdocs\BookReader\resources\views\Main_pages\includes\Home\new_product.blade.php) 代碼如下:home.blade.php@extends('Main_pages.app')@section('title','Home')@section('content')    @include('Main_pages.includes.Home.new_product')    @include('Main_pages.includes.Home.testimonial')    @include('Main_pages.includes.Home.popular_products')    @include('Main_pages.includes.Home.news_letter')    @include('Main_pages.includes.Home.blogs')    @include('Main_pages.includes.Home.quick_view')@endsectionnew_product.blade.php<section class="wn__product__area brown--color pt--80  pb--30"><div class="container">    <div class="row">        <div class="col-lg-12">            <div class="section__title text-center">                <h2 class="title__be--2">New <span class="color--theme">Products</span></h2>                <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered lebmid alteration in some ledmid form</p>            </div>        </div>    </div>    <!-- Start Single Tab Content -->    <div class="furniture--4 border--round arrows_style owl-carousel owl-theme row mt--50">        @foreach($new_books as $book)        <!-- Start Single Product -->}}        <div class="product product__style--3">            <div class="col-lg-3 col-md-4 col-sm-6 col-12">                <div class="product__thumb">                    <a class="first__img" href="single-product.html"><img src="{{$book->image->getImage()}}" alt="product image"></a>                    <div class="hot__box">                        <span class="hot-label">BEST SALLER</span>                    </div>                </div>
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

這很明顯。有一本沒有任何圖像的書。將img代碼更改為:


@isset ($book->image)

    <img src="{{$book->image->getImage()}}" alt="product image">

@endisset

或使用optional method和Null Coalesce Operator來獲得默認圖像:


<img src="{{optional($book->image)->getImage() ?? 'default_image_path.png'}}" alt="product image">

或者這個:


@if (isset($book->image))

    <img src="{{$book->image->getImage()}}" alt="product image">

@else

    <img src="default_image_path.png" alt="product image">

@endif

我希望這可以幫助你。


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號