首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能从一对一的多态实现中检索数据

不能从一对一的多态实现中检索数据
EN

Stack Overflow用户
提问于 2020-06-23 07:48:20
回答 1查看 32关注 0票数 0

我有一张书表和一张图像表,将来我也会有一个博客表格。这就是为什么我想在图书、博客和图像表之间创建一个多态关系的原因。但我的问题是,当我尝试从视图home.blade.php@foreach的关系中检索图书图像url的数据时,我得到了以下信息:

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

代码语言:javascript
复制
@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')
@endsection

new_product.blade.php

代码语言:javascript
复制
<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>
                <div class="product__content content--center">
                    <h4><a href="single-product.html">{{$book->title()}}</a></h4>
                    <ul class="prize d-flex">
                        <li>$35.00</li>--}}
                        <li class="old_prize">$35.00</li>
                    </ul>
                    <div class="action">
                        <div class="actions_inner">
                            <ul class="add_to_links">
                                <li><a class="cart" href="cart.html"><i class="bi bi-shopping-bag4"></i></a></li>
                                <li><a class="wishlist" href="wishlist.html"><i class="bi bi-shopping-cart-full"></i></a></li>
                                <li><a class="compare" href="#"><i class="bi bi-heart-beat"></i></a></li>
                                <li><a data-toggle="modal" title="Quick View" class="quickview modal-view detail-link" href="#productmodal"><i class="bi bi-search"></i></a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="product__hover--content">
                        <ul class="rating d-flex">
                            <li class="on"><i class="fa fa-star-o"></i></li>
                            <li class="on"><i class="fa fa-star-o"></i></li>
                            <li class="on"><i class="fa fa-star-o"></i></li>
                            <li><i class="fa fa-star-o"></i></li>
                            <li><i class="fa fa-star-o"></i></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- Start Single Product -->
        @endforeach
    </div>
    <!-- End Single Tab Content -->
</div>

图像迁移

代码语言:javascript
复制
public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->id();
        $table->text('url');
        $table->unsignedBigInteger('imageable_id');
        $table->text('imageable_type');
        $table->timestamps();
    });
}

图书迁移

代码语言:javascript
复制
public function up()
{
    Schema::create('books', function (Blueprint $table) {
        $table->id();
        $table->string('book_title');
        $table->string('book_author');
        $table->text('book_description');
        $table->timestamps();
    });
}

图像模型

代码语言:javascript
复制
class Image extends Model
{
    public function imageable(){
        return $this->morphTo();
   }
    public function getImage(){
        return $this->url;
   }
}

图书模型

代码语言:javascript
复制
class Book extends Model
{
    public function image(){
        return $this->morphOne(Image::class,'imageable');
    }
    public function title(){
        return $this->book_title;
    }
    public function author(){
        return $this->book_author;
    }
    public function desc(){
        return $this->book_description;
    }
}

HomeController

代码语言:javascript
复制
class HomeController extends Controller
{
    public function view(){
        $new_books = Book::latest()->get();

        return view('Main_pages.Pages.home',['new_books'=>$new_books]);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-23 08:09:28

这很明显。有一本没有图像的书。将img代码更改为:

代码语言:javascript
复制
@isset ($book->image)
    <img src="{{$book->image->getImage()}}" alt="product image">
@endisset

或者使用optional methodNull Coalesce Operator获得默认图像:

代码语言:javascript
复制
<img src="{{optional($book->image)->getImage() ?? 'default_image_path.png'}}" alt="product image">

或者这个:

代码语言:javascript
复制
@if (isset($book->image))
    <img src="{{$book->image->getImage()}}" alt="product image">
@else
    <img src="default_image_path.png" alt="product image">
@endif

希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62529857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档