带条件的laravel DB row

时间:2017-08-01 23:29:15

标签: php mysql wordpress laravel

我有一个名为advertises的表,我希望以随机顺序获取该表的行,并且每次只取1。我知道可以通过控制器中的查询来完成这样的事情:

$advertises = Advertise::inRandomOrder()->take(1);

但我真正想问的是,如何在某些地方进行查询,比如说我帖子的第二段呢?

这是WordPress中的一个例子,但是我需要知道是否可以在laravel中完成:

<?php

//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

    $ad_code = '<div>Ads code goes here</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }

    return implode( '', $paragraphs );
}

单一帖子视图:

<div class="col-md-12 col-sm-12">
                            <div class="single-blog blog-details two-column">
                                <div class="post-thumb">
                                    <img src="{{ url('images/') }}/{{ $post->image }}" class="img-responsive" style="width:100%; height:auto; max-height:500px;" alt="{{ $post->title }}">
                                    <div class="post-overlay">
                                        <span class="uppercase"><a href="#">{{$post->created_at->format('d')}} <br><small>{{$post->created_at->format('M')}}</small></a></span>
                                    </div>

                                </div>
                                <div class="post-content overflow">
                                    <div class="bodytext">{!!  $post->body !!}</div>
                                    <div class="post-bottom overflow">
                                      @role('Admin')
                                        {!! Form::open(['method' => 'DELETE', 'route' => ['posts.destroy', $post->id] ]) !!}
                                        @can('Edit Post')
                                        <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-info" role="button">Edit</a>
                                        @endcan
                                        @can('Delete Post')
                                        {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                                        @endcan
                                        {!! Form::close() !!}
                                      @endrole
                                      @role('Editor')
                                        {!! Form::open(['method' => 'DELETE', 'route' => ['posts.destroy', $post->id] ]) !!}
                                        <a href="{{ url()->previous() }}" class="btn btn-primary">Back</a>
                                        @can('Edit Post')
                                        <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-info" role="button">Edit</a>
                                        @endcan
                                        @can('Delete Post')
                                        {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                                        @endcan
                                        {!! Form::close() !!}
                                      @endrole
                                      <h3>Share this article:</h3>
                                      <div class="sharethis-inline-share-buttons"></div>
                                    </div>
                                    <div class="author-profile padding">
                                        <div class="row">
                                            <div class="col-sm-2">
                                                <img src="images/blogdetails/1.png" alt="">
                                            </div>
                                            <div class="col-sm-10">
                                                <h3>Rodrix Hasel</h3>
                                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliq Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi</p>
                                                <span>Website:<a href="www.jooomshaper.com"> www.jooomshaper.com</a></span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="response-area">
                                    <h2 class="bold">Comments</h2>
                                </div><!--/Response-area-->
                                @include('parts/comments')
                                </div>
                            </div>
                        </div>

1 个答案:

答案 0 :(得分:0)

像这样的东西(不是testet,应该稍微修改一下:

<script>
    $(document).ready(function(){
        $.get('/widget/random-advertisement', function(data){
            $('<div class="advertisement">'+data+'</div>').insertAfter('#your-post p');
        }, 'html');
    });
</script>