【发布时间】:2021-08-18 07:45:06
【问题描述】:
所以我正在尝试将计划与当前用户相关联。 我有模式和迁移称为计划。我有一个名为 user 的方法,我使用 belongsTo() 来获取与该计划关联的所有用户。我不确定为什么它不起作用 模态
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Plan extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'stripe_plan',
'cost',
'description'
];
public function getRouteKeyName()
{
return 'slug';
}
public function user()
{
return $this->belongsTo(User::class);
}
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePlansTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('plans', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->string('stripe_plan');
$table->float('cost');
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('plans');
}
}
我从我的控制器调用计划模型
controller that calls the model
the view that i call the users method
显示空白
【问题讨论】:
-
邮政编码不是图片。没有显示任何内容,因为您没有将用户附加到计划中。由于您在表
plans中没有属性user_id。您应该如何知道每个计划属于哪个用户?
标签: laravel relationship