Here I have 3 tables named user, products, and projects. Table users and products have one to many relations. Every user can have many products and products have their own id. Table products and projects have one to many relations. Every product can have many projects. Table users have column user_id as a foreign key. Where user_id = id of the user. And Table projects have two foreign keys named user_id and product_id. Where user_id = id of the user and product_id = id of products.
Here I can register a user and create a product under the user.
.
Now in step 2 I want to upload an excel file in the projects table. Where in projects table column product_id = id of products table and user_id = id of the users table. When I try to do this I have this error:-
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[1,2,3]' for column laravel_content_excel.projects.product_id at row 1 (SQL: insert into projects (chapter_name, sub_section_name, title_1, description_1, image_1, image_2, image_3, title_2, description_2, title_3, description_3, video_1, video_2, video_3, user_id, product_id, updated_at, created_at) values (chapter 1, sub 1, titel 1, Desp 1, Img 1, img 2, img 3, Ti 2, Des 2, ti3, des 3, v1, vdo2, vdo3, 1, [1,2,3], 2021-06-10 09:58:43, 2021-06-10 09:58:43))
Here I have created 3 products there id is 1,2,3. You can see product_id is taking every values of products but it should be take only one value of product when i upload the Excel file.
I belive there is problem in projectController.php and ImportProject.php This is my ProjectController.php
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// $projects = Project::where('user_id',auth()->user()->id)->latest()->paginate(20);
// $projects = Project::where('user_id',auth()->user()->id)->where('product_id')->latest()->paginate(20);
// $projects = Project::where('user_id',auth()->user()->id)->latest('product_id',8) ->paginate(20);
// $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest();
// $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
$projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
return view('projects.index', compact('projects'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'chapter_name' => 'required',
'sub_section_name' => 'required',
'title_1' => 'required',
'description_1' => 'required',
'image_1' => 'required',
'image_2' => 'required',
'image_3' => 'required',
'title_2' => 'required',
'description_2' => 'required',
'title_3' => 'required',
'description_3' => 'required',
'video_1' => 'required',
'video_2' => 'required',
'video_3' => 'required',
]);
// $input = $request->all();
// $input['user_id'] = auth()->user()->id;
// $input['product_id'] = $id;
$input = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id'));
Project::create($input);
return redirect()->route('project.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function show(Project $project)
{
// $category = $project->category;
return view('projects.show', compact('project'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
// $user_id = Auth::user()->id ;
$request->validate([
'chapter_name' => 'required',
'sub_section_name' => 'required',
'title_1' => 'required',
'description_1' => 'required',
'image_1' => 'required',
'image_2' => 'required',
'image_3' => 'required',
'title_2' => 'required',
'description_2' => 'required',
'title_3' => 'required',
'description_3' => 'required',
'video_1' => 'required',
'video_2' => 'required',
'video_3' => 'required',
]);
$input = $request->all();
$project->update($input);
return redirect()->route('project.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect()->route('projects.index')
->with('success', 'Project deleted successfully');
}
public function importProject()
{
Excel::import(new ProjectsImport, request()->file('file'));
return back()->with('success','Project created successfully.');
}
public function export()
{
return Excel::download(new UsersExport, 'projects.xlsx');
}
}
This is my ImportProject.php
class ProjectsImport implements ToModel, WithHeadingRow
{
public function __construct()
{
Project::truncate();
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
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' => Product::pluck('id')
// 'product_id' => 2
]);
}
}
Thanks for you help :)