我有非常基本的问题.在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)
我有什么想法可以解决这个问题?
在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();
我按照书本做了一切:
安装了新的Laravel 5.3.9应用程序(我所有的非新鲜应用程序产生相同的错误)
跑 php artisan make:auth
为新表创建迁移`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)然后我跑了 php artisan migrate
然后我定义了一个新的种子 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)
php …我使用这段代码成功发送电子邮件:
\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)
有什么更好的方法吗?谢谢你。
在过去 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) 在我的母语波兰语中,格式正确的文本在行尾不能有单个字母单词。在博客条目和短篇文章中,我只是把
Run Code Online (Sandbox Code Playgroud)
在这样的话之后,工作就完成了。
有没有办法在 CSS 或 jQuery 中处理这个问题?
我考虑用我在这里得到的解决方案修复 30 000 条引文的集合。
提前致谢。
我有一个表,其中包含用户提交的链接.有些链接不包含
`http://`
Run Code Online (Sandbox Code Playgroud)
我想使用以下查询列出这些记录:
$object = Related::whereHas( function($q)
{
$q->where('URL', 'like', 'http%');
})->get();
Run Code Online (Sandbox Code Playgroud)
如何反转查询以获取它们?
谢谢
我有这个解决方案在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中获取数组?
先感谢您.
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.
我只是定义了相同的关系,但使用->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) laravel ×7
php ×6
laravel-5 ×5
eloquent ×2
laravel-5.3 ×2
laravel-5.4 ×2
mysql ×2
css ×1
jquery ×1
laravel-8 ×1
locale ×1
model ×1
models ×1
namespaces ×1
random ×1
relation ×1
reverse ×1
seeding ×1
text ×1
typography ×1