标签: eloquent

如何简化此代码,以便它没有那么多if if if语句?

我很确定这可以简化,但我不确定如何.任何帮助将不胜感激!

foreach($color_count as $k => $v) {
        $index = count(json_decode($k, true));
        if($v < $max_color_count[$index]) {
            $diff = $max_color_count[$index] - $v;

            if($k == '["White","Blue","Black","Red","Green"]') { $this->add($filteredCards['White-Blue-Black-Red-Green'], $diff); }

            else if($k == '["White","Blue","Black","Red"]') { $this->add($filteredCards['White-Blue-Black-Red'], $diff); }
            else if($k == '["White","Blue","Black","Green"]') { $this->add($filteredCards['White-Blue-Black-Green'], $diff); }
            else if($k == '["White","Blue","Red","Green"]') { $this->add($filteredCards['White-Blue-Red-Green'], $diff); }
            else if($k == '["White","Black","Red","Green"]') { $this->add($filteredCards['White-Black-Red-Green'], $diff); }
            else if($k == '["Blue","Black","Red","Green"]') { $this->add($filteredCards['Blue-Black-Red-Green'], $diff); }

            else if($k == '["White","Blue","Black"]') { $this->add($filteredCards['White-Blue-Black'], $diff); }
            else if($k == '["White","Blue","Red"]') { …
Run Code Online (Sandbox Code Playgroud)

php collections laravel eloquent

0
推荐指数
1
解决办法
92
查看次数

Laravel - Insert a model multiple times

I have a model that I change some attributes I want to insert it but Eloquent, after the first save() will automatically do an update while I'm using save() method, here is my code:

for ($i = 0; $i < $range; $i++) {
  $model->attr = "Some new value";
  $model->save(); // after the first save() will do update but I want to an insert
}
Run Code Online (Sandbox Code Playgroud)

php model laravel eloquent

0
推荐指数
2
解决办法
1193
查看次数

与laravel中的多对多关系分页

我正在尝试在laravel 4.2中设置电子邮件系统.我有两个模型电子邮件和用户,他们之间有很多对多的关系.到目前为止一切都很好,但现在我只是在发送电子邮件时遇到问题.我已经尝试了2天,但还没有完成任务.

    //User.php    Model
    public function emails()
    {   
        return $this->belongsToMany('Email', 'users_email', 'reciever_uid','email_id')->
         withPivot('sender_UID');
    }

    //Email.php  Model
    public function users()
    {   
        return $this->belongsToMany('User', 'users_email', 'email_id','reciever_id');
    }
Run Code Online (Sandbox Code Playgroud)

我试图获取users_email数据透视表数据,以获取当前经过身份验证的用户QueryBuilder的所有电子邮件ID.

    $user=Auth::Id();
    $user_data=DB::table('users_email')->
    join('users','users_email.reciever_uid','=','users.Id')->
    where('users_email.sender_uid','=',$user->Id)->orderby   
    ('users_email.email_id','desc')->get();

    foreach ($user_data as $data) {
    $id[]=$data->email_id;
   }
Run Code Online (Sandbox Code Playgroud)

现在我有$ id数组,我想获取所有电子邮件以及要显示的接收器的f_name和l_name.到目前为止,我已经尝试了一些基本知识的热切加载,但无法获得预期的结果.我已经尝试过这个来分配$ id内的所有电子邮件,但getTotal只返回2,即使下面显示的图片指定id = 78的电子邮件已经发送了8个以上,所以我想通过电子邮件进行分页.Ex准备只有10封电子邮件.

    $pagination=User::with(array('emails'=>function($query) use ($id){
        $query->WhereIn('users_email.email_id',$id);
    }))->paginate(10);
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

谢谢

数据透视表users_email

在此输入图像描述

mysql email pagination laravel eloquent

0
推荐指数
1
解决办法
2790
查看次数

Laravel:完整性约束违规:1452无法添加或更新子行:外键约束失败

我的Laravel应用程序出现此错误:

无法添加或更新子行:外键约束失败

这是我的2张桌子

用户

Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username');
        $table->string('password');
        $table->string('firstname');
        $table->string('lastname');
        $table->string('middlename');
        $table->string('address');
        $table->string('birthday');
        $table->string('contact');
        $table->string('email');
        $table->enum('isAdmin', array(0,1))->default(0);
        $table->enum('isTeacher', array(0,1))->default(0);
        $table->timestamps();

    });
Run Code Online (Sandbox Code Playgroud)

Schema::create('attendance', function(Blueprint $table)
        {
        $table->increments('id');
        $table->string('status');
        $table->string('comment');
        $table->integer('student_id')->unsigned();
        $table->foreign('student_id')->references('id')->on('users');
        $table->string('student_firstname');
        $table->string('student_lastname');
        $table->integer('teacher_id')->unsigned();
        $table->foreign('teacher_id')->references('id')->on('users');
        $table->string('teacher_firstname');
        $table->string('teacher_lastname');
        $table->timestamps();
Run Code Online (Sandbox Code Playgroud)

这是我的模特

用户

class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's …
Run Code Online (Sandbox Code Playgroud)

laravel eloquent laravel-4 laravel-5

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

模型错误:Laravel 5中的all()

你能帮我吗?

我刚刚开始使用laravel 5并且无法获得这个简单的模型:all()可以工作.我确定我做错了什么,但我找不到什么.

这就是我所拥有的:

routes.php文件

Route::get('/', 'PageController@index');
Run Code Online (Sandbox Code Playgroud)

Partner.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Partner extends Model {
    protected $table = 'tab_parceiro';
    protected $fillable = '';
    public $timestamps = false;

    protected $primaryKey = 'codigo';
}
Run Code Online (Sandbox Code Playgroud)

PageController.php

<?php namespace App\Http\Controllers;

class PageController extends Controller {

    public function __construct()
    {
    }

    public function index()
    {
        $partners = \App\Partner::all();

        //return view('index', compact('partners'));
    }

}
Run Code Online (Sandbox Code Playgroud)

错误:

[2015-03-14 12:50:22] production.ERROR: exception 'ErrorException' with message 'array_flip() expects parameter 1 to be array, string given' in /home/ubuntu/workspace/storage/framework/compiled.php:9360 …
Run Code Online (Sandbox Code Playgroud)

php model laravel eloquent laravel-5

0
推荐指数
1
解决办法
1724
查看次数

Laravel 5轻松获取使用slug选择的记录的id

我试图找到一种简单的方法来获取记录的id(用slug选择),这样我就可以用它来查询这条记录的子项(父/子相关).

$product = Products::where('url','the-slug')->get();
Run Code Online (Sandbox Code Playgroud)

现在我可以使用该集合来获取id并继续查询孩子们.

有更简单的方法吗?

和...

我可以单独用slug查询孩子吗?

orm laravel eloquent laravel-5

0
推荐指数
1
解决办法
2777
查看次数

如何为Eloquent模型定义类字段

我从Yii和Codeigniter来到Laravel.并且找不到如何描述Eloquent模型的类字段的解决方案.我想知道这个类可用的变量.问题是当我尝试使用变量public =>时,此变量为空.如果我定义变量protected =>当我使用它时一切都很好,但是这个变量不适用于自动竞争(因为这个var受到保护).

谢谢!

class User extends Eloquent
{

...
protected $registered; // works, but not available
public $registered; // not works, always empty
var $registered; // not works, always empty
...

}
Run Code Online (Sandbox Code Playgroud)

eloquent laravel-5

0
推荐指数
1
解决办法
937
查看次数

Laravel,不使用foreach插入多行

$input['activities'] = array(3,2,5);

foreach($input['activities'] as $activity_id){

    $user_activities = new User_activities;

    $user_activities->activity_id = $activity_id;

    $user_activities->user_id = Auth::id();

    $user_activities->save();

}
Run Code Online (Sandbox Code Playgroud)

是否可以使用单行脚本为Laravel中的每个语句执行上述操作?而不是进行保存foreach,是否可以在一行中完成?

mysql insert save laravel eloquent

0
推荐指数
1
解决办法
3607
查看次数

如何使用Laravel 5从相关表中查询具有条件的表

我有两张桌子,

表用户:id,name等

表视频:id,user_id等

型号:视频

public function user()
{
    return $this->belongsTo('App\Models\User');
}
Run Code Online (Sandbox Code Playgroud)

我想选择用户"Max"所拥有的所有视频.

我尝试过以下内容:

$Videos = \App\Models\Video::with('user')->where('user.name','=','Max')->get();



Unknown column 'user.name' in 'where clause' (SQL: select * from `videos` where `videos`.`deleted_at` is null and `user`.`name` = Max)
Run Code Online (Sandbox Code Playgroud)

谢谢!

relationship conditional-statements laravel eloquent

0
推荐指数
1
解决办法
3799
查看次数

Laravel Eloquent:插入数据

我正在通过post方法发送一堆数据。现在,我正在使用一个可以正常工作的解决方案:

$dataClient = new Client;
$dataClient->name    = $post['client']['name'];
$dataClient->address = $post['client']['address'];
...
$dataClient->save();        
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更短的解决方案?就像发布数组一样,Laravel可以将键映射到db字段吗?

如果您还要添加更多内容(例如计算所得的价值)怎么办?

例:

$dataClient = new Client;
Then map array keys to db keys
$dataClient->someField = someCalculatedValue 
$dataClient->save();
Run Code Online (Sandbox Code Playgroud)

先感谢您。

laravel eloquent

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