Laravel 5 Framework Learning dates, Mutator and scope,laravelmutator_php tutorials

Source: Internet
Author: User
Tags compact naming convention

Laravel 5 Framework Study date, Mutator and Scope,laravelmutator


In our previous solution, assigning the published_at to the current date is actually a temporary solution, we need to set the release date, which may be released in the next 2 days, let's change the issue.

First modify the controller:

  Public function Store () {    article::create (Request::all ());    Return redirect (' articles ');  }

Then modify the view to add the Release date field

@extends (' layout ') @section (' content ')  

Write a New article

{{--using the illuminate\html Open Source Library We added--}} {!! Form::open ([' url ' = ' articles ') ]! {!! Form::label (' title ', ' title: ') !! {!! Form::text (' title ', null, [' class ' = ' Form-control ']) ! {!! Form::label (' body ', ' body: ') !! {!! Form::textarea (' body ', null, [' class ' = ' Form-control ']) ! {!! Form::label (' Published_at ', ' Publish on: ') !! {!! Form::input (' Date ', ' Published_at ', date (' y-m-d '), [' class ' = ' Form-control ']) ! {!! Form::submit (' Add article ', [' class ' = ' btn ' btn-primary Form-control ']) ! {!! Form::close ()!!} @stop

OK, let's add a new article and set the date to a future day, but the article appears directly at the beginning, which is not what we need. We need to show that genius. Of course, we need to be more specific, such as at 8:00, instead of the 0-point display. We can add a mutator (that is, a property set for other languages) and modify our model

<?php namespace App;use datetime;use Illuminate\database\eloquent\model;class article extends Model {protected $ fillable = [    ' title ',    ' body ',    ' published_at '  ];  The attribute property is set to conform to the Format Convention  //Set property of the public  function Setpublishedatattribute ($date) {    $this->attributes [' published_at '] = Carbon::createfromformat (' y-m-d ', $date)->hour (8)->minute (0)->second (0);}  }

Add a new record to view the database, we have set the time correctly, but our homepage still shows the future only post the article, we modify it.

Public Function Index () {    //$articles = Article::latest (' published_at ')->get ();    $articles = article::latest (' published_at ')->where (' Published_at ', ' <= ', Carbon::now ())->get ();    Return view (' Articles.index ', compact (' articles '));  }

The workaround above works, but the query statement is too long. We can use the scope provided by Laravel to simplify our work. The so-called scope can be understood as the intermediate query result used in the query process, such as we define a published scope, he can return all the currently published articles, let us modify the model.

  Set scope, follow the naming convention public  function scopepublished ($query) {    $query->where (' published_at ', ' <= ', Carbon: : Now ());  }

Modifying the controller using scope

Public Function Index () {    //$articles = Article::latest (' published_at ')->get ();    $articles = article::latest (' published_at ')->where (' Published_at ', ' <= ', Carbon::now ())->get ();    $articles = article::latest (' published_at ')->published ()->get ();    Return view (' Articles.index ', compact (' articles '));  }

The results are the same, but in complex queries we can use scope to decompose our tasks, or reuse queries.

Let's add a new query and query all the articles that haven't been published yet. Add scope to the model

  Public Function scopeunpublished ($query) {    $query->where (' published_at ', ' > ', Carbon::now ());  }

Modifying the controller using unpulished

Public Function Index () {    //$articles = Article::latest (' published_at ')->get ();    $articles = article::latest (' published_at ')->where (' Published_at ', ' <= ', Carbon::now ())->get ();    $articles = article::latest (' published_at ')->published ()->get ();    $articles = article::latest (' published_at ')->unpublished ()->get ();    Return view (' Articles.index ', compact (' articles '));  }

One more thing! If we use DD ($article->published_at) to view the results in the Show method, with DD ($article->created_at); The result is not the same as the former we make our own fields, which are automatically generated in createarticletable by $table->timestamp (). The automatically generated fields show up as carbon types, and ours is a string. There are many benefits to using the Crabon type, such as you can output DD ($article->created_at->diffforhumans ()); , this 1 hour ago results, but our published_at is not. How to modify? Modify the model to tell Laravel,published_at that it is a date.

  protected $dates = [' published_at '];

Use DD again ($article->published_at->diffforhumans ()); , the results are shown as 3 days from now,bingo!

The above is the whole content of this article, I hope to give you to learn LARAVEL5 framework to help.

http://www.bkjia.com/PHPjc/980210.html www.bkjia.com true http://www.bkjia.com/PHPjc/980210.html techarticle Laravel 5 Framework Learning Date, Mutator and Scope,laravelmutator in our previous solution, assigning a direct value to published_at as the current date is actually a temporary solution ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.