小编wob*_*ano的帖子

如何使用Laravel和Eloquent ORM在两个日期之间进行查询?

我正在尝试创建一个报告页面,显示从特定日期到特定日期的报告.这是我目前的代码:

$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', $now)->get();
Run Code Online (Sandbox Code Playgroud)

这在纯SQL中的作用是什么select * from table where reservation_from = $now.

我在这里有这个查询,但我不知道如何将其转换为雄辩的查询.

SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to
Run Code Online (Sandbox Code Playgroud)

如何将上面的代码转换为雄辩的查询?先感谢您.

php orm laravel laravel-5

89
推荐指数
8
解决办法
14万
查看次数

试图获得非物体的财产 - Laravel 5

我试图在我的文章中回应用户的名字而我正在接受 ErrorException: Trying to get property of non-object.我的代码:

楷模

1. News

    class News extends Model
    {
      public function postedBy()
      {
         return $this->belongsTo('App\User');
      }
      protected $table = 'news';
      protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
    }

2. User

    class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;

        protected $table = 'users';

        protected $fillable = ['name', 'email', 'password'];

        protected $hidden = ['password', 'remember_token'];

    }
Run Code Online (Sandbox Code Playgroud)

架构

users

在此输入图像描述

news

在此输入图像描述

调节器

public function showArticle($slug)
    {
        $article = News::where('slug', $slug)->firstOrFail(); …
Run Code Online (Sandbox Code Playgroud)

php laravel

27
推荐指数
6
解决办法
17万
查看次数

如何将自定义功能传递给Laravel Blade模板?

我有一个自定义功能,我想在刀片模板中传递它.这是功能:

function trim_characters( $text, $length = 45, $append = '…' ) {

    $length = (int) $length;
    $text = trim( strip_tags( $text ) );

    if ( strlen( $text ) > $length ) {
        $text = substr( $text, 0, $length + 1 );
        $words = preg_split( "/[\s]| /", $text, -1, PREG_SPLIT_NO_EMPTY );
        preg_match( "/[\s]| /", $text, $lastchar, 0, $length );
        if ( empty( $lastchar ) )
            array_pop( $words );

        $text = implode( ' ', $words ) . $append;
    }

    return $text;
}
Run Code Online (Sandbox Code Playgroud)

用法是这样的:

$string …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-blade

19
推荐指数
1
解决办法
2万
查看次数

如何在AJAX成功时以编程方式关闭Bootstrap 3模式?

我有一个代码,我想要做的是关闭ajax成功的模式.这是我的代码:

脚本

success: function() {
    console.log("delete success");
    $('#deleteContactModal').modal('hide');
    $( "#loadContacts" ).load( "/main/loadContacts" );

}
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="modal fade" id="deleteContactModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
<!--everything goes here -->
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

一切正常,除非代码$('#deleteContactModal').modal('hide');触发,它只显示一个黑色褪色的屏幕,如下所示:

在此输入图像描述

模态关闭但黑色褪色仍然存在.我在这里错过了什么吗?先感谢您.

我正在使用bootstrap 3.3.

javascript ajax jquery twitter-bootstrap twitter-bootstrap-3

14
推荐指数
4
解决办法
2万
查看次数

如何重用swagger定义并删除其中的一些参数?

这是我的代码:

definitions:
  User:
    type: object
    properties:
      id:
        type: integer
      username:
        type: string
      first_name:
        type: string
      last_name:
        type: string
      password:
        type: string
      created_at:
        type: string
        format: date-time
      updated_at:
        type: string
        format: date-time
    required:
      - username
      - first_name
      - last_name
      - password

/api/users:
  post:
    description: Add a new user
    operationId: store
    parameters:
      - name: user
        description: User object
        in: body
        required: true
        type: string
        schema:
          $ref: '#/definitions/User'
    produces:
      - application/json
    responses:
      "200":
        description: Success
        properties:
          success:
            type: boolean
          data:
            $ref: '#/definitions/User'
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在下面的帖子键中, …

api yaml swagger swagger-2.0 openapi

10
推荐指数
1
解决办法
2万
查看次数

如何在JavaScript中使用数组减少条件?

所以我有一个数组

const records = [
    {
        value: 24,
        gender: "BOYS"
    },
    {
        value: 42,
        gender: "BOYS"
    },
    {
        value: 85,
        gender: "GIRLS"
    },
    {
        value: 12,
        gender: "GIRLS"
    },
    {
        value: 10,
        gender: "BOYS"
    }
]
Run Code Online (Sandbox Code Playgroud)

我想得到sum所以我使用JavaScript数组reduce功能并且正确.这是我的代码:

someFunction() {
  return records.reduce(function(sum, record){
    return sum + record.value; 
  }, 0);
}
Run Code Online (Sandbox Code Playgroud)

使用该代码,我得到的值173是正确的.现在我想做的是将所有金额仅用于那些有"BOYS"性别的对象.

我试过类似的东西

someFunction() {
  return records.reduce(function(sum, record){
    if(record.gender == 'BOYS') return sum + record.value; 
  }, 0);
}
Run Code Online (Sandbox Code Playgroud)

但我一无所获.我在这里错过了什么吗?任何帮助将非常感激.

javascript arrays vue.js computed-properties

9
推荐指数
4
解决办法
1万
查看次数

Twig检查文件是否存在

你好,所以我使用纤细的框架和树枝,这是我目前在PHP中的代码:

$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
Run Code Online (Sandbox Code Playgroud)

现在我想把if语句放在我的模板文件中.如何file_exists在我的树枝模板中使用该功能,以便检查文件是否存在?

php file-exists twig

7
推荐指数
2
解决办法
2万
查看次数

检查文本框值是字符串还是javascript中的数字

基本上我有以下代码:

<input type="text" value="123" id="txtbox">
<script>
var myVar = document.getElementById('txtbox').value;

if (myVar.substring) {
alert('string');
} else{
alert('number');
}
</script>
Run Code Online (Sandbox Code Playgroud)

无论您在文本框中放置什么值,它都会始终发出警报string.有没有办法,如果你在文本框中放一个数字,它会提醒number而不是字符串?谢谢.

javascript

6
推荐指数
2
解决办法
6974
查看次数

MaterialiseCSS NavBar和SideNav

我正在创建一个sb admin 2 like page,其中有2个导航,如下所示:

在此输入图像描述

到目前为止我所做的是:

在此输入图像描述

如您所见,侧面导航在顶部栏处延伸.到目前为止我的代码是这样的:

<!DOCTYPE html>
<html>
  <head>
    <!--Import Google Icon Font-->
    <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>

  <body>

<div class="navbar-fixed">

<!-- Dropdown Structure -->
<ul id="dropdown1" class="dropdown-content">
  <li><a href="#!">User Profile</a></li>
  <li><a href="#!">Settings</a></li>
  <li class="divider"></li>
  <li><a href="#!">Logout</a></li>
</ul>
<nav class="light-blue lighten-1" role="navigation">
  <div class="nav-wrapper container">
    <a href="#!" class="brand-logo">Point of Sale</a>
    <ul class="right hide-on-med-and-down">
      <!-- Dropdown Trigger -->
      <li><a class="dropdown-button" href="#!" …
Run Code Online (Sandbox Code Playgroud)

css css3 materialize

6
推荐指数
2
解决办法
3万
查看次数

如何在 JavaScript 中将 Ajax 转换为 Fetch API?

所以我使用RiveScriptJavaScript 端口,它使用 ajax,当然我不想再使用 jQuery。只有一行 ajax,我想将其更改为新的 Fetch API。

**FYI: You can see the ajax code in line 1795 of the CDN.**
Run Code Online (Sandbox Code Playgroud)

所以这是原始代码:

return $.ajax({
    url: file,
    dataType: "text",
    success: (function(_this) {
        return function(data, textStatus, xhr) {
            _this.say("Loading file " + file + " complete.");
            _this.parse(file, data, onError);
            delete _this._pending[loadCount][file];
            if (Object.keys(_this._pending[loadCount]).length === 0) {
                if (typeof onSuccess === "function") {
                    return onSuccess.call(void 0, loadCount);
                }
            }
        };
    })(this),
    error: (function(_this) {
        return function(xhr, textStatus, errorThrown) {
            _this.say("Ajax …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery fetch-api rivescript

6
推荐指数
1
解决办法
8775
查看次数