我不知道如何在 laravel eloquent orm 中同时使用 update 和 limit 方法。
$affectedRows = Promo::where('used','=',0)
->update(array('user_id' => Auth::user()->id))
->limit(1); // Call to a member function limit() on a non-object
//->take(1); // Call to a member function take() on a non-object
Run Code Online (Sandbox Code Playgroud)
我尝试了 limit 和 take 方法。
我只想做一个结果就是更新。
但我认为,我不能在更新时使用限制或采取方法。
有没有办法通过 eloquent 只更新一行?
添加 :
雄辩的 ORM
$affectedRows = Promo::where('user_id','=',DB::raw('null'))->take(1)
->update(
array(
'user_id' => Auth::user()->id,
'created_ip' =>Request::getClientIp(),
'created_at' => new DateTime,
'updated_at' => new DateTime
)
);
Run Code Online (Sandbox Code Playgroud)
查询生成器
$affectedRows = DB::table('promos')->whereNull('user_id')
->take(1)
->update(array(
'user_id' => Auth::user()->id, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 firstOrFail() 获取数据以使用雄辩的 laravel 获取数据。但是面临一个问题,当我尝试获取不存在的数据时,它将返回模型异常。如果数据存在,我如何使它成为返回数据,而如果值或字段不存在,则返回 null 或“”。我尝试添加以下内容,但它会使页面完全空白。
(global.php)
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
// return Response::make('Not Found', 404);
return "";
});
Run Code Online (Sandbox Code Playgroud) Laravel 的 Eloquentupdate()函数返回一个布尔值,而不是更新后的条目。我正在使用:
return User::find($id)->update( Input::all() )
Run Code Online (Sandbox Code Playgroud)
这仅返回一个布尔值。有没有办法可以在不运行另一个查询的情况下获取实际行?
好的,所以我的问题是,我的 ajax 调用没有正确更新。它与我拥有的许多功能相同,只是不更新值。虽然,我可以在 mysql 中自行运行查询,并且可以正常更新。功能如下:
/*
* "elerts/quicksave" > Update elert from all screen
*/
public function quicksave($id)
{
$data = (object)Input::get();
$ev = Events::find($id);
$ev->limit = $data->limit;
$ev->EventEnabled = $data->EventEnabled;
$ev->SundayStart = $data->SundayStart;
$ev->MondayStart = $data->MondayStart;
$ev->TuesdayStart = $data->TuesdayStart;
$ev->WednesdayStart = $data->WednesdayStart;
$ev->ThursdayStart = $data->ThursdayStart;
$ev->FridayStart = $data->FridayStart;
$ev->SaturdayStart = $data->SaturdayStart;
$ev->save();
$queries = DB::getQueryLog();
$last_query = end($queries); dd($last_query);
return "Your eLert has been saved!";
}
Run Code Online (Sandbox Code Playgroud)
下面是一个被传递的示例 ID 和一个被传递的 $data 对象的示例。
$id = '107';
object(stdClass)#137 (9) { ["limit"]=> string(3) …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么这有效
$profile = Profile::all()->random(8);
Run Code Online (Sandbox Code Playgroud)
这不起作用
$profile = Profile::where('gender_id', '=', 1)->random(8);
Run Code Online (Sandbox Code Playgroud)
我收到错误说
调用未定义的方法 Illuminate\Database\Query\Builder::random()
机场表:
Schema::create('airports', function(Blueprint $table)
{
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('city');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
航班表:
Schema::create('flights', function(Blueprint $table)
{
$table->increments('id');
$table->integer('number');
$table->integer('origin')->unsigned();
$table->foreign('origin')->references('id')->on('airports');
$table->integer('destination')->unsigned();
$table->foreign('destination')->references('id')->on('airports');
$table->integer('price');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
飞行模型:
<?php
class Flight extends \Eloquent {
protected $fillable = ['number', 'origin', 'destination'];
public function origin(){
return $this->belongsTo('Airport');
}
public function destination(){
return $this->belongsTo('Airport');
}
}
Run Code Online (Sandbox Code Playgroud)
飞行控制器@索引:
public function index()
{
$flights = Flight::with('origin')->with('destination')->get();
return Response::json($flights, 200);
}
Run Code Online (Sandbox Code Playgroud)
部分回复:
[
{
"id": "1",
"number": "112",
"origin": null,
"destination": null,
"price": "232",
"created_at": "2014-12-28 11:49:44",
"updated_at": …Run Code Online (Sandbox Code Playgroud) I have a package that uses DB, and I wanted to create some tests that will run with sqlite in memory for the tests.
Now I have this base test class:
use Illuminate\Database\Capsule\Manager;
class TestCaseDb extends \PHPUnit_Framework_TestCase {
protected $db;
protected $tbmsg;
public function setUp()
{
parent::setUp(); // //DONT CARE
League\FactoryMuffin\Facade::getFaker()->unique($reset = true);//DONT CARE
$this->initDb();
$this->initTbmsg(); //DONT CARE
}
protected function initDb() {
//confi gfor the sqlite
$capsule = new Manager();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => …Run Code Online (Sandbox Code Playgroud) 我的目标是检索用户的所有“项目”,然后在我的视图中按他们的“状态”分组显示。有 4 种可能的状态,每种状态<div>在包含项目信息的页面上都有自己的状态。经过一番摸索,我相信我需要使用这样的groupBy()方法:
$items = Item::ownedBy( Auth::id() )->groupBy('status')->get();
Run Code Online (Sandbox Code Playgroud)
这似乎确实进行了某种分组,但是当我遍历集合时,我最多得到 4 个结果,每个状态一个。这并不能真正解决我的问题,因为我需要为每个状态显示用户的所有项目,而不仅仅是一个。我一定在这里遗漏了一些东西,我真的很想避免对每个状态进行查询并以这种方式显示它们。我想我可以按状态过滤集合并创建 4 个新集合,但这不是groupBy()应该做的吗?
我想在Laravel Product模型中添加一个方法,该方法按nameattr 过滤并返回所有匹配产品的集合,这就是我得到的:
public function filterByName($query)
{
return $this->where('name','LIKE','%'.$query.'%')->get();
}
Run Code Online (Sandbox Code Playgroud)
$products = collect(new Product);
$products->filterByName($name);
Run Code Online (Sandbox Code Playgroud)
正确的用法是什么?我需要使用QueryFilter吗?
我在Laravel Eloquent中有两个表:
UserCategory:
id | user | category
Run Code Online (Sandbox Code Playgroud)
和
Category:
id | name
Run Code Online (Sandbox Code Playgroud)
UserCategories与类别低谷所属到:
class UserCategory extends Model
{
public function category()
{
return $this->belongsTo('App\Category', 'category');
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试获取带有类别的UserCategories时,我得到了整数值(而不是Category模型)
$categories = [];
$userCategory = UserCategory::where('user', $user->id)->with('category')->get();
foreach($userCategory as $item) {
$categories[] = $item->category->name;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获取类别名称时,看到第二个错误:
试图获取非对象的属性“名称”
奇怪的是,当我使用时,dd($item)我看到$ item与Category对象具有正确的关系,但是 dd($item->category)返回整数值(类别ID)而不是类别模型。