标签: laravel-6

具有整数列的 Laravel 关系数组

类别表

在此输入图像描述

产品表

在此输入图像描述

我想创建与类别表的关系 ->id 与产品表category_id 建议对此关系 数组与整数列关系的任何想法

控制器

Products::with('category')->get();
Run Code Online (Sandbox Code Playgroud)

产品型号

public function category() {
    return $this->hasMany(App\Models\Categories::class, 'id', 'category_id');
}
Run Code Online (Sandbox Code Playgroud)

类别型号

public function product() {
    return $this->belongsTo(Products::class,'category_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)

orm foreign-keys laravel eloquent laravel-6

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

Laravel 创建具有两个时间戳列的表时出错

我在 Laravel 6.6 中创建了一个具有以下定义的表。

public function up()
{
    Schema::create('quarters', function (Blueprint $table) {
        $table->integer('quarter_id')->unsigned();
        $table->integer('year')->unsigned();
        $table->integer('quarter_num')->unsigned();
        $table->timestamp('valid_from');
        $table->timestamp('valid_to'); // <------ error on this line
        $table->string('description')->nullable();
        $table->timestamps();
        $table->primary('quarter_id');
    });
} 
Run Code Online (Sandbox Code Playgroud)

当我运行迁移命令时,出现以下错误。

Illuminate\Database\QueryException : SQLSTATE[42000]:语法错误或访问冲突:1067 'valid_to' 的默认值无效(SQL:创建表quartersquarter_idint unsigned not null、year int unsigned not null、quarter_numint unsigned not null、valid_fromtimestamp not null、valid_to时间戳不为空、description varchar(255) 为空、created_at时间戳为空、updated_at时间戳为空) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

这是 Eloquent 生成的 SQL:

CREATE TABLE `quarters`(
    `quarter_id` INT UNSIGNED NOT …
Run Code Online (Sandbox Code Playgroud)

php mariadb eloquent laravel-6

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

如何在 Laravel 中安装 Chart.js?

我想在我的 Laravel 项目中安装 Chart.js。使用 npm install(Webpack 的配置),并查看我的索引页和一些 Chart.js 示例。在我第一次尝试时,我在浏览器中收到此错误。也许我没有正确配置 webpack?

未捕获的引用错误:图表未定义于(索引):134

因此,我复制并粘贴了在 Chart.js 文档集成中找到的此要求。

require(['path/to/chartjs/dist/Chart.min.js'], function(Chart){
    var myChart = new Chart(ctx, {...});
});
Run Code Online (Sandbox Code Playgroud)

我在 上遇到了这个错误npm run dev

./resources/js/app.js 中的错误模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):语法错误:/home/sa/laravelproject/resources/js/app.js:意外的标记(37:37)

35 | 35 36 | 36 需要(['路径/到/chartjs/dist/Chart.min.js'],函数(图表){

37 | 37 var myChart = new Chart(ctx, {...}); | ^ 38 | });

laravel laravel-5 chart.js laravel-6

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

Laravel 以干净的方式排除资源丰富的函数

目前我的文件中有这 4 条资源丰富的路线web.php

Route::resource('campaigns', 'CampaignController')->except(['show']);
Route::resource('users', 'UserController')->except(['show']);
Route::resource('models', 'ModelController')->except(['show']);
Route::resource('trims', 'TrimController')->except(['show']);
Run Code Online (Sandbox Code Playgroud)

我不禁想知道。我不能向该Route::resources函数添加一些内容以使其表现出这种方式吗?这是因为它们都有一个共同点。他们的except()方法show()

它想要有这样的东西。(这个例子不起作用,因为resources()没有except()方法。

Route::resources([
    'campaigns' => 'CampaignController',
    'users' => 'UserController',
    'models' => 'ModelController',
    'trims' => 'TrimController'
])->except(['show']);
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-6

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

Laravel 服务器硬件要求

我开发了一个 Laravel API 并考虑选择一个服务器来部署该项目。服务器上没有运行大型业务逻辑。这是一个简单的应用程序。但该应用程序在高峰时段每秒将有约 100 个用户访问。在这种情况下,我应该考虑服务器的哪些参数来选择服务器(从硬件方面 - RAM、存储、处理器等)?

API 将用于车间时间报告。每小时(当一小时结束时),约有 150 位用户将访问系统报告时间。

laravel-6

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

Can't install packages with Composer in Laravel 6.0

I've set up a new Laravel 6 project and am trying to install some packages. So far, I have downloaded the core packages, Laravel Passport, and Spatie's role system.

Right now, I'm trying to download this model generator.

https://github.com/krlove/eloquent-model-generator

但是,它没有用。我只得到以下输出:

 composer require krlove/eloquent-model-generator
Using version ^1.3 for krlove/eloquent-model-generator
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install …
Run Code Online (Sandbox Code Playgroud)

laravel composer-php laravel-6

3
推荐指数
1
解决办法
788
查看次数

使用switch后无法使用分页。-Laravel

我想在使用分页之前使用开关。但是我认为这个错误-

调用未定义的方法Illuminate \ Database \ Eloquent \ Builder :: links()

我的代码-MyController.php

$var = User::select('somefield', 'anotherfield');

switch($condition){
   case foo:
       $var->where('bar', '=', 'xyz');
   break;
   case etc:
       $var->where('bar', '=', 'abc')
   break;
}
$var->paginate(10);
return View::make('x.y.z', compact('var'));
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-paginate laravel-6

3
推荐指数
1
解决办法
58
查看次数

一次更新多行 Laravel Eloquent

我有products以下结构的表。

id | name | promote

promote是布尔类型的。

我想将boolean列的值1设置0为选定的行并设置为非选定的行。我在控制器中有以下代码来处理这个查询。

$yes = Tour::whereIn('id', $request->promote)->get();
$no = Tour::whereNotIn('id', $request->promote)->get();

foreach ($yes as $item) {
    $item->promote = 1;
    $item->save();
}

foreach ($no as $item) {
    $item->promote = 0;
    $item->save();
}
Run Code Online (Sandbox Code Playgroud)

我从表单请求中得到关注。

在此处输入图片说明

上面的代码确实有效,但我认为它不是很有效。我正在寻找以更有效的方式实现结果的可选方法。

laravel eloquent laravel-6

3
推荐指数
1
解决办法
6316
查看次数

JsonResource 和 ResourceCollection 有什么区别?在 Laravel v6 或 v7

有人可以解释 ResourceCollection 和 JsonResource 之间的区别吗?

在 Laravel 6 文档中,您可以生成 2 种不同类型的资源……ResourceCollection 和 JsonResource。https://laravel.com/docs/6.x/eloquent-resources#resource-responses

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ShopCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}
Run Code Online (Sandbox Code Playgroud)

对...

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Shop extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-6 laravel-7

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

Laravel 中的查询生成器 GROUP BY、HAVING、COUNT

如何在查询构建器中表达此代码。我正在使用 Laravel 6。

SELECT * FROM feedback GROUP BY noTicket having count(`status`) < 2 
Run Code Online (Sandbox Code Playgroud)

我的代码:

$feedback = DB::table('feedback')
            ->groupBy('noTicket')
            ->having('count(status)', '<', 2)
            ->get();
Run Code Online (Sandbox Code Playgroud)

错误代码:

SQLSTATE[42000]: Syntax error or access violation: 1055 'sifora.feedback.idFeedback' isn't in GROUP BY 
(SQL: select * from `feedback` group by `noTicket` having `count(status)` < 2) 
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题?sql 代码与查询构建器之间似乎匹配。

谢谢

mysql query-builder laravel eloquent laravel-6

3
推荐指数
1
解决办法
6589
查看次数