我应该使用DATETEIME吗?还是INT?
谢谢!
编辑:我是专门针对的
public static $timestamps = true;
Run Code Online (Sandbox Code Playgroud)
在Eloquent模型中设置的标志.
在Laravel中我们可以设置如下关系:
class User {
public function items()
{
return $this->belongsToMany('Item');
}
}
Run Code Online (Sandbox Code Playgroud)
允许我们为用户获取数据透视表中的所有项:
Auth::user()->items();
Run Code Online (Sandbox Code Playgroud)
但是,如果我想得到相反的结果呢?并获得用户尚未拥有的所有项目.所以不要在数据透视表中.
有一个简单的方法吗?
我最近开始与工作Laravel和口才,和不知道的是,缺乏一个查找或创建模型选项.你总是可以写,例如:
$user = User::find($id);
if (!$user) {
$user = new User;
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有更好的方法来查找或创建?看来在这个例子中微不足道,但对于更复杂的情况下,这将是非常有益要么让现有的记录并更新或创建一个新的.
我是Laravel的新人.我正在使用laravel创建一个应用程序.当我创建一个帖子时,"created_at"和"updated_at"的值如下所示:
2014-06-26 04:07:31
2014-06-26 04:07:31
Run Code Online (Sandbox Code Playgroud)
但我希望这些值没有时间看起来像这样:
2014-06-26
2014-06-26
Run Code Online (Sandbox Code Playgroud)
只有没有时间的日期.
可能吗 ???
请帮我.
我的帖子模型:
<?php
class Post extends \Eloquent {
protected $fillable = array('title', 'body', 'meta','reporter', 'slug', 'image','top');
public static $rules = array(
'title'=>'required|min:2',
'body'=>'required|min:20',
'reporter'=> 'required|min:2',
'image'=>'image|mimes:jpeg,jpg,bmp,png,gif'
);
public function categories()
{
return $this->belongsToMany('Category');
}
}
Run Code Online (Sandbox Code Playgroud)
我的Post Controller商店:
public function store()
{
$validator = Validator::make(Input::all(), Post::$rules);
if ($validator->passes()) {
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->reporter = Input::get('reporter');
$post->meta = Input::get('meta');
$post->slug = Input::get('title');
$post->top = …Run Code Online (Sandbox Code Playgroud) 我有一个名为Eloquent的雄辩模型:
Products::where("actice", "=", true)->get()->toArray();
Run Code Online (Sandbox Code Playgroud)
现在我想为它添加join-statement,我已经定义了一个scopeQuery:
public function scopeJoinWithTags($query)
{
return $query->leftJoin("tags", "tags.id", "=", "products.tag_id");
}
Run Code Online (Sandbox Code Playgroud)
然后我们的主要查询更改为:
Products::where("actice", "=", true)->joinWithTags->get()->toArray();
Run Code Online (Sandbox Code Playgroud)
我得到的是好的,这是我所期望的,但我想将tags表的name属性更改为tag_name,我应该怎么做?我的意思是,我在我的查询中的某个地方说:
tags.name AS tag_name
Run Code Online (Sandbox Code Playgroud)
所以在最终结果数组中我做了:
$result[$i]['tag_name'];
Run Code Online (Sandbox Code Playgroud)
虽然现在我必须:
$result[$i]['name'];
Run Code Online (Sandbox Code Playgroud) 我有一个模型设置如下:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Upload extends Model {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'uploads';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('id', 'user', 'created_at', 'updated_at');
public function mime() {
return $this->hasOne('App\Models\Mime', 'mime');
}
}
Run Code Online (Sandbox Code Playgroud)
当JsonSerialize()被调用时,它返回:
{
"serverPath": "upload/2015/06/06/21/filename.jpg",
"filename": "filename.jpg",
"mime": "92"
}
Run Code Online (Sandbox Code Playgroud)
这92引用了另一个表(App\Models\Mime表示)中的id 和一个type与之关联的字符串.我想92 …
如何定义Laravel 5中JSON提供的数据类型pgsql 9.4?
我需要定义数据类型,存储和获取数据,到目前为止还无法找到在Laravel 5.1中处理它的方法
描述:我有一个充满测试数据的表.有时,我想清除它以获取新数据.我可以像DB WorkBench一样在DBMS应用程序中执行截断,但我正在尝试在我的应用程序中实现它.
目标:在单击时创建一个按钮以截断数据库中的表.
这是我的步骤:
1 - 申报路线
Route::delete('visitor/truncate',array('as'=>'visitor.truncate', 'uses'=>'VisitorController@truncate'));
Run Code Online (Sandbox Code Playgroud)
2 - truncate在我的中创建一个函数VisitorController
public function truncate()
{
$visitors = Visitor::all();
$visitors ->truncate();
return View::make('visitors.index')
->with('success', 'Truncate Done');
}
Run Code Online (Sandbox Code Playgroud)
3 - 在我的视图上创建一个按钮
{!! Form::model($visitors, array( 'route' => array('visitor.truncate'),'method' => 'DELETE')) !!}
<button type="submit" class="btn bgm-red btn-float waves-effect waves-effect waves-button waves-float"><i class="md md-remove"></i></button>
{!! Form::close()!!}
Run Code Online (Sandbox Code Playgroud)
4 - 测试
当我点击它时,它进入truncate()我的控制器中的功能,但我不断收到此错误
调用未定义的方法Illuminate\Database\Eloquent\Collection :: truncate()
我需要包含任何东西truncate()吗?
任何提示将非常感谢!
我关注多对多Laravel关系中的自动命名表.
例如:
Schema::create('feature_product', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
将表名更改为:
Schema::create('product_feature', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
我的关系有误.
有什么问题product_feature?
在Laravel 5.1中,我可以看到表列关系可以通过两种方式设置:
1)在迁移表中定义外键.
2)定义模型中的雄辩关系.
我已阅读文件,我仍然对以下内容感到困惑:
我是否需要同时使用或仅需要1个?
同时使用两者是不对的吗?或者它是否使其多余或引起冲突?
使用Eloquent关系而不提及迁移列中的外键有什么好处?
有什么不同?
这些是我现在的代码.如果我需要删除我在迁移文件中设置的外键,我仍然不清楚.
移民:
public function up()
{
Schema::create('apps', function (Blueprint $table) {
$table->increments('id');
$table->string('app_name');
$table->string('app_alias');
$table->timestamps();
$table->engine = 'InnoDB';
});
// This is the second Migration table
Schema::create('app_roles', function (Blueprint $table) {
$table->increments('id');
$table->integer('app_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->integer('role_id')->unsigned()->index();
$table->engine = 'InnoDB';
$table->unique(array('app_id', 'user_id'));
$table->foreign('app_id')
->references('id')
->on('apps')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
}
Run Code Online (Sandbox Code Playgroud)
具有雄辩关系的模型:
// App Model
class App extends Model
{
public function appRoles() {
return $this->hasMany('App\Models\AppRole');
}
} …Run Code Online (Sandbox Code Playgroud)