如何在laravel-8中从另一个表中的表调用id
这里我有三个表,分别命名为如何在laravel-8中从另一个表中的表调用id,laravel,eloquent,laravel-8,Laravel,Eloquent,Laravel 8,这里我有三个表,分别命名为user,product,project 在项目表中,我有两个异化名称用户id和产品id 我想在ProjectController.php 在这里,我可以在index方法中调用user\u id,但我不能调用product\u id。谢谢您的帮助 这是我在ProjectController.php中的索引方法 如何在何处写入产品标识?我无法获取产品id值 public function index() { $projects = Project::where('user
user
,product
,project
在项目表
中,我有两个异化名称用户id
和产品id
我想在ProjectController.php
在这里,我可以在index方法中调用user\u id,但我不能调用product\u id。谢谢您的帮助
这是我在ProjectController.php中的索引方法
如何在何处写入产品标识?我无法获取产品id值
public function index()
{
$projects = Project::where('user_id',auth()->user()->id)->where('product_id')->latest() ->paginate(20);
return view('projects.index', compact('projects'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
这是products table products.php
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('detail');
$table->string('color');
$table->string('image');
$table->string('logo');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
这是projects表projects.php
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('chapter_name', 255)->nullable();
$table->string('sub_section_name', 500)->nullable();
$table->string('title_1', 255)->nullable();
$table->string('description_1', 5000)->nullable();
$table->string('image_1', 255)->nullable();
$table->string('image_2', 255)->nullable();
$table->string('image_3', 255)->nullable();
$table->string('title_2', 255)->nullable();
$table->string('description_2', 5000)->nullable();
$table->string('title_3', 255)->nullable();
$table->string('description_3', 255)->nullable();
$table->string('video_1', 255)->nullable();
$table->string('video_2', 255)->nullable();
$table->string('video_3', 255)->nullable();
$table->unsignedBigInteger ('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// $table->foreignId('product_id')->nullable();
$table->unsignedBigInteger('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable();
});
这是ProjetcImport.php
return new Project([
'chapter_name' => $row['chapter_name'],
'sub_section_name' => $row['sub_section_name'],
'title_1' => $row['title_1'],
'description_1' => $row['description_1'],
'image_1' => $row['image_1'],
'image_2' => $row['image_2'],
'image_3' => $row['image_3'],
'title_2' => $row['title_2'],
'description_2' => $row['description_2'],
'title_3' => $row['title_3'],
'description_3' => $row['description_3'],
'video_1' => $row['video_1'],
'video_2' => $row['video_2'],
'video_3' => $row['video_3'],
'user_id' => auth()->user()->id,
'product_id' => Product::where('user_id',Auth::id())->pluck('id'))
// 'product_id' => id()->id
]);
我希望它能对您起作用。根据添加的产品id的条件?我不想在这里添加身份验证条件,我只希望produtc id能在项目表中按异化方式工作。它将采用产品id。一对多关系一个产品将具有多个项目和表结构好的,然后获得产品id基于用户id获得它?项目::其中('product_id',products::where('user_id',Auth::id())->pulk('id')->toArray();它的工作我需要更多的帮助我必须在我的projetcImport.php中调用相同的内容你能帮我吗好的,请用表结构添加其他问题。请尝试这个
Project::whereIn('product_id',Products::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);