我一直在使用資料庫中的正文在 Laravel 上發送郵件,一切正常,直到我在 laravel 查詢中添加連接函式。當我在 MySQL 作業臺上鍵入查詢時,我按預期回傳了一行,但 laravel 沒有。
歡迎.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('[email protected]', 'John Doe')
->subject('Welcome')
->markdown('mails.welcome')
->with([
'name' => 'New User',
'wMail' => DB::table('mails')
->join('users', 'mails.user_id', '=', 'users.id')
->where([
['mails.user_id', 'users.id'],
['mails.name', 'Welcome Email']
])->get(),
]);
}
}
用戶.php:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the mails for a user.
*/
public function mails()
{
return $this->hasMany(Mail::class);
}
}
郵件.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Mail extends Model
{
use HasFactory;
/**
* Get the user for a mail.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
2022_03_07_111014_create_mails_table.php:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mails', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('name');
$table->string('object');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('mails');
}
};
我的資料庫中也有 5 個用戶,一封郵件對應于以下工廠:
'user_id' => 3,
'name' => 'Welcome Email',
'object' => 'Bienvenu sur UH-Lawyers',
'body' => $this -> faker -> text(200)
歡迎.blade.php:
@component('mail::message')
Hello {{$name}},
@if (count($wMail)>0)
@foreach($wMail as $mail)
{{$mail->body}}
@endforeach
@else
No result
@endif
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
因此,當我在 MySQL 上鍵入此查詢時: SELECT body FROM Mails.mails join Mails.users on users.id = mails.user_id where users.id = mails.user_id and mails.name ="Welcome Email"; 我有一行與我期望的郵件正文相對應。但是當我在 Laravel 上發送郵件時,“No result”出現在我的郵件正文中。但是當我改變我的 Laravel 查詢時:
...->where([['mails.user_id', 'users.id'],...
to :
...->where([['mails.user_id', '3'],...
everything works just fine!!! I really don't get what the problem is...
uj5u.com熱心網友回復:
在您的where子句中,第一項過濾mails.user_id值'users.id'是字串的列。
我猜這不是你打算做的。假設您要根據特定用戶 ID 過濾該列,您需要提供該實際值。
通常,您可以使用 檢索當前(登錄)用戶的 ID Auth::user()->id。所以我建議你更新你的build()方法如下:
// Welcome.php
public function build()
{
$currentUserId = Auth::user()->id;
return $this->from('[email protected]', 'John Doe')
->subject('Welcome')
->markdown('mails.welcome')
->with([
'name' => 'New User',
'wMail' => DB::table('mails')
->join('users', 'mails.user_id', '=', 'users.id')
->where('mails.user_id', '=', $currentUserId)
->where('mails.name', '=', 'Welcome Email')
->first(),
]);
}
但是,如果您希望傳入另一個特定的用戶 ID,則需要為此使用建構式引數:
// Welcome.php
// (imports, traits and comments removed for brevity)
namespace App\Mail;
class Welcome extends Mailable
{
// ...
protected int $userId;
public function __construct(int $userId)
{
$this->userId = $userId;
}
public function build()
{
return $this->from('[email protected]', 'John Doe')
->subject('Welcome')
->markdown('mails.welcome')
->with([
'name' => 'New User',
'wMail' => DB::table('mails')
->join('users', 'mails.user_id', '=', 'users.id')
->where('mails.user_id', '=', $this->userId)
->where('mails.name', '=', 'Welcome Email')
->first(),
]);
}
}
然后,當您創建Welcome可郵寄實體時:
$userId = 0; // TODO: Retrieve the user ID you need
Mail::to('[email protected]')->send(new Welcome($userId));
相關檔案可以在該頁面上找到:https ://laravel.com/docs/9.x/mail#sending-mail
uj5u.com熱心網友回復:
如果該查詢適用于 MySQL,并且您無法使用該語法使其在 laravel 中作業,您可以嘗試使用 Laravel 的 DB 類,語法如下:
$awnser = DB::select('SELECT body FROM Mails.mails join Mails.users on users.id = mails.user_id where users.id = mails.user_id and mails.name ="Welcome Email"');
但是你應該使用引數而不是在那里寫所有東西:
$welcome = "Welcome Email";
$awnser = DB::select('SELECT body FROM Mails.mails join Mails.users on users.id = mails.user_id where users.id = mails.user_id and mails.name =:name', ['name' => $welcome]);
此解決方案可能對您有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440220.html
標籤:php mysql laravel database view
