laravel Eloquent ORM

app\models\news.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    protected $fillable = ['title', 'src', 'img_size', 'tab_id', 'is_artificial', 'is_preview'];
    
    /*
     * 定义与 Tab 一对多反向关联
     */
    public function tab() {
        return $this->belongsTo('App\Models\Tab');
    }
    
    /**
     *  定义与 NewsImg 一对多关联
     */
    public function imgs() {
        return $this->hasMany('App\Models\NewsImg');
    }
    /**
     * 定义条件只查询某一天的数据
     * 
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $date
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeInTheDay($query,$date) {
        $query->where('created_at', '>', date('Y-m-d',strtotime($date)));
        $query->where('created_at', '<', date('Y-m-d',strtotime('+1 day',strtotime($date))));
        return $query;
    }
    
    /**
     * 定义条件只查询人工编辑的
     * 
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeArtificial($query) {
        return $query->where('is_artificial', 1);
    }
    
    /**
     * 定义条件排除预览状态的
     * 
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNoPriview($query) {
        return $query->where('is_preview', 0);
    }
    
    /**
     * 定义条件只查预览状态的
     * 
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeIsPriview($query) {
        return $query->where('is_preview', 1);
    }
}

?>

app\http\controllers\newscontroller.php

<?php
public function recommend(Request $request) {
        $pageSize = $request->input('page_size', 10);
        $cacheKey = 'mini_api:news:recommend_'.$request->input('page',1).'_'.$request->input('is_preview',0);
        //获取编辑推荐
        $result = Cache::remember($cacheKey,1,function() use ($request,$pageSize){
            $news = Models\News::where(function($query) use ($request) {
                    //人工编辑 & 推荐栏目 & 当天
                    $query->artificial();
                    $query->where('tab_id', 1);
                    if(!$request->input('is_preview',0)){
                        $query->where('is_preview', 0);
                    }
                    $query->inTheDay(date('Y-m-d'));
                })->orWhere(function($query) {
                    //头条接口推荐数据
                    $query->where('tab_id', 0);
                })->orderBy('is_artificial', 'desc')
                        ->orderBy('id','desc')
                        ->limit($pageSize)->get();
            $news->load('imgs');
            return NewsResoure::collection($news)->toArray($request);
        });
        
        $data = [
            'code' => 0,
            'data' => $result,
            'message' => "数据列表"
        ];
        return response()->json($data);
    }

?>

<?php 

        $taoc_list = Models\Vip_taoc::select('id','taoc_name','valid_time','buy_price')->where('status',0)->where('type',1)->get();

        $userinfo = Models\Smember::select('mobile','icon','icon_list','username','sex','birthday','province','city')->where('mobile','138****0731')->first();

        $user = Models\Smember::where('mobile', $mobile)->first();
        $user->username = $username;
        $user->sex = $sex;
        $user->birthday = $birthday;
        $user->save();

?>

@foreach ($taoc_list as $taoc)
    {{$taoc->taoc_name}}
@endforeach

@foreach ($icon_list as $icon)
    <img src="{{$icon}}" alt="">
@endforeach

@if ($userinfo->sex=='男') checked @endif

    A+
发布日期:2018年05月29日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: