小编Pet*_*ter的帖子

未找到类'App\Http\Controllers\DB',我也无法使用新模型

我有非常基本的问题.在L4中,下面的方法开箱即用,所以现在我迷路了.请帮忙.几天前,我开始了一个Laravel 5.0项目.我现在有新鲜,干净的安装.

问题1:当我尝试从数据库中获取任何内容时

$headquote = DB::table('quotation_texts')->find(176);
Run Code Online (Sandbox Code Playgroud)

我明白了:

Class 'App\Http\Controllers\DB' not found
Run Code Online (Sandbox Code Playgroud)

问题2:在克隆User.php模型之前,将类名更改为"Quotation".下面是放在App根文件夹中的文件Quotations.php的内容:

<?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Quotation extends Model  {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'quotation_texts';
}
Run Code Online (Sandbox Code Playgroud)

任何尝试使用该模型

$headquote = Quotation::find(176);
Run Code Online (Sandbox Code Playgroud)

最终得到这个:

Class 'App\Http\Controllers\Quotation' not found
Run Code Online (Sandbox Code Playgroud)

我有什么想法可以解决这个问题?

php namespaces models laravel laravel-5

51
推荐指数
3
解决办法
14万
查看次数

怎么得到随机行laravel-5

在L-4中很简单:

$random_quote = Quotation::all()->random(1);
Run Code Online (Sandbox Code Playgroud)

但是现在在L-5中,这篇文章中描述的单一方法没有工作: Laravel - Eloquent或Fluent随机行

我的视图文件只是空白.有任何想法吗?

编辑:

解决: $ random_quote = Quotation :: orderByRaw("RAND()") - > first();

random eloquent laravel-5

38
推荐指数
7
解决办法
6万
查看次数

Laravel 5.3 db:seed命令根本不起作用

我按照书本做了一切:

  1. 安装了新的Laravel 5.3.9应用程序(我所有的非新鲜应用程序产生相同的错误)

  2. php artisan make:auth

  3. 为新表创建迁移`php artisan make:migration create_quotations_table --create = quotations

    Schema::create('quotations', function (Blueprint $table) {
        $table->increments('id');
    
        $table->string('text');
    
        // my problem persists even with the below two columns commented out
        $table->integer('creator_id')->unsigned()->index('creator_id');
        $table->integer('updater_id')->unsigned()->index('updater_id');
    
        $table->softDeletes();
        $table->timestamps();
    });
    
    Run Code Online (Sandbox Code Playgroud)
  4. 然后我跑了 php artisan migrate

  5. 然后我定义了一个新的种子 php artisan make:seeder QuotationsTableSeeder

添加简单插入后,文件的完整内容:

<?php

use Illuminate\Database\Seeder;

class QuotationsTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    DB::table('quotations')->insert([
        'text' => str_random(10),

    ]);
}
}
Run Code Online (Sandbox Code Playgroud)
  1. 然后我跑了 php …

php seeding laravel laravel-seeding laravel-5.3

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

如何更改从控制器发送的电子邮件中的语言环境 - laravel 5.4

我使用这段代码成功发送电子邮件:

 \App::setLocale('pl');
 Mail::send('emails.new_'.$itemtype, ['object' => $object, 'email' =>     $email_recipee], function ($m) use ( $email_recipee, $object, $itemtype) {
 $m->to($email_recipee, 'Title')->subject(' Subject of email');
 //
 });
Run Code Online (Sandbox Code Playgroud)

但是电子邮件被翻译成en应用程序的默认语言。

我的问题

如何让 Laravel 发送带有仅针对特定电子邮件声明的语言环境的电子邮件(每个收件人都有不同的语言集)。

我试过的:

  • \App::setLocale('pl');就在Mail命令之前
  • 在控制器中全局设置我的工作中间件__construct()

    $this->middleware('setLocale'); // sets the locale to the recipee locale

脏 (?) 解决方案

现在我只是在电子邮件视图中添加一行:

{{ \App::setLocale($lead->client->lang)}}
Run Code Online (Sandbox Code Playgroud)

有什么更好的方法吗?谢谢你。

php locale laravel laravel-5.3 laravel-5.4

6
推荐指数
3
解决办法
6367
查看次数

安装 barryvdh/laravel-translation-manager 包时 Laravel 8 应用程序中出现类型错误

在过去 3-4 周内,我在 Laravel 8 应用程序中收到此错误,这导致我的应用程序崩溃。卸载该软件包后问题消失。

请指教。

使用 Laravel 应用程序时:

TypeError
Argument 2 passed to Symfony\Component\Translation\Translator::addResource() must be an instance of Symfony\Component\Translation\mixed, array given, called in D:\www\MyBooks\vendor\nesbot\carbon\src\Carbon\AbstractTranslator.php on line 165
Run Code Online (Sandbox Code Playgroud)

更新应用程序和依赖项时:

TypeError

  Argument 2 passed to Symfony\Component\Translation\Translator::addResource() must be an instance of Symfony\Component\Translation\mixed, array given, called in D:\www\MyBooks\vendor\nesbot\carbon\src\Carbon\AbstractTranslator.php on line 165

  at D:\www\MyBooks\vendor\symfony\translation\Translator.php:109
    105|      * @param mixed  $resource The resource name
    106|      *
    107|      * @throws InvalidArgumentException If the locale contains invalid characters
    108|      */
  > 109|     public function addResource(string …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-8

5
推荐指数
1
解决办法
6748
查看次数

单字母单词后的不间断空格

在我的母语波兰语中,格式正确的文本在行尾不能有单个字母单词。在博客条目和短篇文章中,我只是把

&nbsp;
Run Code Online (Sandbox Code Playgroud)

在这样的话之后,工作就完成了。

有没有办法在 CSS 或 jQuery 中处理这个问题?

我考虑用我在这里得到的解决方案修复 30 000 条引文的集合。

提前致谢。

css mysql jquery text typography

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

使用like-laravel eloquent或原始MySQL查询进行反向查询

我有一个表,其中包含用户提交的链接.有些链接不包含

`http://`
Run Code Online (Sandbox Code Playgroud)

我想使用以下查询列出这些记录:

        $object = Related::whereHas( function($q)
                {
                $q->where('URL', 'like', 'http%');

                    })->get();
Run Code Online (Sandbox Code Playgroud)

如何反转查询以获取它们?

谢谢

mysql reverse laravel eloquent laravel-5

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

从belongsToMany关系获取相关ID数组 - Laravel 5.4

我有这个解决方案在Laravel 5.3中运行良好

$procedure = Procedure::findOrFail($id);
$attached_stages = $procedure->stages()->getRelatedIds()->toArray();
Run Code Online (Sandbox Code Playgroud)

在我的Procedure模型中:

public function stages()
{

    return $this->belongsToMany('App\Models\Stage', 'procedure_stage', 'procedure_id', 'stage_id')->withPivot('id','status')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)

现在,在迁移到Laravel 5.4后,我收到此错误:

Call to undefined method Illuminate\Database\Query\Builder::getRelatedIds()
Run Code Online (Sandbox Code Playgroud)

似乎getRelatedIds已被删除.

我的问题:

如何在5.4中获取数组?

先感谢您.

php laravel laravel-5 laravel-5.4

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

Laravel - how get a single model instance not as collection, but single model from belongsToMany

I have this relation defined in one of my models. It is the simplest possible case.

use \App\Models\Related;

public function entities()
{
    return $this
           ->belongsToMany(Entity::class, 'entity_related', 'related_id', 'entity_id');
}
Run Code Online (Sandbox Code Playgroud)

Now, I want to create a relation which gets only one model from the table.

Current solution

我只是定义了相同的关系,但使用->take(1)。粗暴,但行得通。该解决方案的低迷之处在于,我需要做一个foreach循环以获得所需的单个模型。

use \App\Models\Entity;

public function firstOfEntities()
{
    return $this
           ->belongsToMany(Entity::class, 'entity_related', 'related_id', 'entity_id')
           ->take(1); // <---
}
Run Code Online (Sandbox Code Playgroud)

去做

如何正确定义仅获取一个(几乎任何一个)模型实例的关系,而不是创建一个集合?

理想的用法

完成上述操作后,我希望能够在foreach循环内在模板文件中使用单个模型:

@foreach($object as $o)
    <h2>{{ $o->singleEntity->name }}</h2>
    <p>{{ …
Run Code Online (Sandbox Code Playgroud)

php model relation laravel laravel-5

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