好吧,这是我的迁移......
public function up()
{
Schema::create('instagrams', function (Blueprint $table) {
$table->bigInteger('id')->unsigned()->primary();
// ...
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('instagram_id')->unsigned()->nullable();
// ...
});
}
Run Code Online (Sandbox Code Playgroud)
我有一个用户模型和一个 Instagram 模型。这是我的 Instagram 模型:
class Instagram extends Model
{
public function user()
{
return $this->hasOne('App\User');
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 Instagram 与用户的关系不起作用。我无法从 Instagram 访问用户,即使他们都在数据库中。
>>> $u = App\User::first()
=> App\User {#695
id: 1,
instagram_id: "3620243170",
}
>>> $i = App\Instagram::first()
=> App\Instagram {#696
id: "3620243170",
}
>>> $i->user
=> null
Run Code Online (Sandbox Code Playgroud)
所以,我花了很长时间绞尽脑汁,直到找到这些有用的修补方法......这就是它给我的: …
有没有办法可以重命名 laravel 收集结果中的数据键?此刻这个
DB::table('contracts AS C')->paginate(1)
Run Code Online (Sandbox Code Playgroud)
回报
DB::table('contracts AS C')->paginate(1)
Run Code Online (Sandbox Code Playgroud)
我想要的是这个
{
"current_page": 1,
"data": [
{
"id": 191,
"buyer": "John",
"provider": "Jane"
}
],
"from": 1,
"last_page": 1,
"next_page_url": "",
"path": "",
"per_page": 1,
"prev_page_url": null,
"to": 1,
"total": 1
}Run Code Online (Sandbox Code Playgroud)
这里的挑战是改变各方的数据密钥
我有一个名为的模型ReferrerMedium以及表的迁移referrer_mediums。
这是我的课程:
namespace App;
class ReferrerMedium extends \Eloquent
{
//
}
Run Code Online (Sandbox Code Playgroud)
这是我的迁移:
Schema::create('referrer_mediums', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
foreach (ReferrerMedium::all() as $referrer_medium) {
$options[$referrer_medium->name] = $referrer_medium->name;
}
Run Code Online (Sandbox Code Playgroud)
此代码导致错误Base table or view not found: 1146 Table 'leadbind.referrer_media' doesn't exist
为什么它尝试查询referrer_media表而不是referrer_mediums???
我有一个Question来自非常大的问题表(600,000 条记录)的模型,与Customer、Answer和Product模型相关。关系与这个问题无关,但我提到它们是为了澄清我需要使用 Eloquent。当我打电话时,Question::with('customer')->get();它运行平稳且快速。
但是还有另一个表,其中我列出了question_id不应显示的所有问题(出于特定原因)。
我尝试了这段代码:
// omitted product ids, about 95,000 records
$question_ids_in_product = DB::table('question_to_product')
->pluck('product_id')->all();
$questions = Question::with('customer')
->whereNotIn('product_id', $question_ids_in_product)
->paginate($perPage)->get();
Run Code Online (Sandbox Code Playgroud)
花费了很多时间并显示此错误:
SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders
有时Fatal error: Maximum execution time of 30 seconds exceeded
当我使用普通 sql 查询运行它时:
SELECT * FROM questions LEFT JOIN customers USING (customer_id)
WHERE question_id NOT IN (SELECT question_id FROM question_to_product)
只需要80毫秒
在这种情况下我该如何使用 Eloquent?
我正在尝试从数据库中获取所有数据,其中 id 等于数组中的 id。我希望它按顺序运行,例如,如果调用数组$attributes并且看起来像这样;
array:2 [\xe2\x96\xbc\n 0 => 11\n 1 => 12\n]\nRun Code Online (Sandbox Code Playgroud)\n\n我想获取 id 等于的所有结果11并将它们分组在一起以便稍后循环。然后继续查询数据库以获取 id 等于 的所有结果12。
我可以创建一个循环并在每次迭代中运行一个查询,但如果数组有 30 多个 id,则将导致 30 个查询,这些查询本质上是相同的,只是匹配不同的 id。
\n\n我可以将数组传递给where or子句并对结果进行分块,以便我可以循环认为它稍后分组吗?
我有这个数据库结构
table tools table details
----------- -------------
id * id *
name balance_shoot
tool_id
Run Code Online (Sandbox Code Playgroud)
我需要计算toolsBalance_shoottool_details小于或等于零(危险)和大于零(保存)的数量。就像是 :
[
danger_count => 0,
save_count => 5
];
Run Code Online (Sandbox Code Playgroud)
我已经实现了这一点:
public function index(Request $request){
$trans_date = date('Y-m-d');
$tools = Tool::select('id')
->whereHas(
'details' , function ($query) use ($trans_date){
$query->where('trans_date', $trans_date );
}
)
/*->with([
'details' => function ($query) use ($trans_date){
$query->where('trans_date', $trans_date );
}
])*/
->withCount([
'details as danger' => function ($query) use ($trans_date){
$query->where('trans_date', $trans_date )->where('balance_shoot', '<=', 0 );
},
'details …Run Code Online (Sandbox Code Playgroud) 如何自动删除7天后创建的数据库记录?我是否必须放置在index.blade.php 或其他内容中?也许有一些调度程序?
public function index()
{
$results = Test::orderBy('created_at', 'desc')->take(5)->get();
if (!Auth::user()->isAdmin()) {
$results = $results->where('user_id', '=', Auth::id());
}
return view('results.index', compact('results'));
}
Run Code Online (Sandbox Code Playgroud) 我正在构建使用递归 url 构造的 Laravel 应用程序。我想知道是否可以访问模型中 hasone 相关模型的数据,以将构造的 url 直接返回到视图,而无需与控制器交互。\
public function link(){
var_dump($this->category());
$url = ['news'];
$url[] = $this->category()->url;
$url[] = $this->url;
return implode('/',$url);
}
Run Code Online (Sandbox Code Playgroud)
像这样的简单代码示例返回这个
Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$url (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php)
Run Code Online (Sandbox Code Playgroud)
那么有没有什么好的方法可以通过仅使用雄辩的模型来解决它,或者只能通过使用控制器等来解决?
我正在开发 Laravel 应用程序。数据库表中的一个小变化会导致新的迁移。有没有什么方法可以仅在 laravel 的表上重新迁移。
我正在 laravel 中制作一个关注者用户控制器。
我的控制器具有以下功能:
public function follow($id)
{
if (Auth::user()) {
$follower = Auth::user()->user_id;
$user_to_follow = User::Find($id);
if ($user_to_follow) {
$follower = new Follower;
$follower->follower_id = $follower;
$follower->user_id = $user_to_follow;
if ($follower->save()) { <-- receiving error here
return "done";
} else {
return redirect('/')->back();
}
}
return $follower;
}
}
Run Code Online (Sandbox Code Playgroud)
追随者型号:
protected $table = "followers";
protected $fillable = ['follower_id', 'user_id', 'accepted'];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
Run Code Online (Sandbox Code Playgroud)
用户模型
public function followers()
{
return $this->hasMany('App\Follower', 'user_id');
} …Run Code Online (Sandbox Code Playgroud) eloquent ×10
laravel ×10
php ×8
laravel-5 ×3
mysql ×3
collections ×1
laravel-5.4 ×1
oop ×1
sql ×1