I am trying to create a job board, I am using Crud application for the cv part of the portal, i don want everyone to see other user's cvs.
So the user inputs his data, then only sees his data to apply, edit, show etc. A possible way is to add a created by, but I am not sure if it is the best option.
Users migration <?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('status')->default(1);
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
cv migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCvTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cv', function (Blueprint $table) {
$table->index('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string ('FName');
$table->string ('Nationality');
$table->string ('Residence');
$table->string ('email');
$table->string ('Phone');
$table->string('DOB');
$table->string ('Gender');
$table->string ('EmploymentStatus');
$table->string ('EducationLevel');
$table->string ('MajorField');
$table->string ('Major');
$table->string ('university');
$table->string ('GradCounrty');
$table->string ('GPA');
$table->string ('ProfCertifications');
$table->string ('PrevJobTitle');
$table->date('PrevJobStart');
$table->date('PrefJobEnd');
$table->integer('WorkExp');
$table->string('fileCV');
$table->boolean('status')->default(0);
$table->boolean('trash')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cv');
}
}
CV controller
<?php
namespace App\Http\Controllers;
use App\Models\cv;
use Illuminate\Http\Request;
class cvController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
function __construct()
{
$this->middleware('permission:cv-list|cv-create|cv-edit|cv-delete', ['only' => ['index', 'show']]);
$this->middleware('permission:cv-create', ['only' => ['create', 'store']]);
$this->middleware('permission:cv-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:cv-delete', ['only' => ['destroy']]);
}
public function index(Request $request)
{
$data = cv::all();
return view('cv.index',compact('data'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('cv.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'FName' => 'required',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$imageDestinationPath = 'uploads/images/sliders';
$postImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($imageDestinationPath, $postImage);
$input['image'] = "$postImage";
}
cv::create($input);
return redirect()->route('cv.index')
->with('success','CV Upladed successfully.');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$cv = cv::find($id);
return view('cv.show', compact('cv'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$cv = cv::find($id);
return view('cv.edit',compact('cv'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$cv = cv::find($id);
$this->validate($request, [
'FName' => 'required',
]);
$input=$request->all();
if ($image = $request->file('image')) {
$imageDestinationPath = 'uploads/images/sliders';
$postImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($imageDestinationPath, $postImage);
//dd($path);
$input['image'] = "$postImage";
}
else{
unset($input['image']);
}
$cv->update($input);
return redirect()->route('cv.index')
->with('success', 'CV updated successfully.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
cv::find($id)->delete();
return redirect()->route('cv.index')
->with('success', 'CV deleted successfully.');
}
public $gender = [
"0" => "Female",
"1" => "Male",
];
}
CodePudding user response:
Add where clause to the index function
public function index(Request $request)
{
$data = cv::where('user_id', $request->user()->id)->get();
return view('cv.index', compact('data'));
}
CodePudding user response:
when you use authentication you can retrieve info of each user by his token
so you can get only his CVs by the id_user
this is the example without token
public function example(Request $request)
{
$data = cv::where('user_id', $request->user()->id)->get();
return view('cv.index', compact('data'));
}
this is the example with token and authentication
public function example()
{
$data = cv::where('user_id', auth()->user()->id)->get();
return view('cv.index', compact('data'));
}