Answer the question
In order to leave comments, you need to log in
Files are not loaded and are not recorded in the database?
Here I created a Laravel project
. I have 2 Candidate Models with its data and CandidateFiles for storing file names and candidate_id!
The Candidate itself is created and writes all the data to the database! But CandidateFiles is not written, and the files are not stored either! I put down the links in the models and also indicated the links in the migrations!
I send a request with Postman:
Controller:
namespace App\Http\Controllers\Api\V1\Home;
use App\Entity\Candidate\Candidate;
use App\Entity\Candidate\CandidateFiles;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class HomeController extends Controller
{
public function getAllFaculties()
{
return [
'name' => 'Board Api'
];
}
public function createCandidate(Request $request)
{
$newCandidate = Candidate::createNewCandidate(
$request['candidateName'],
$request['candidateLastName'],
$request['candidatePatronymic'],
$request['candidatePhoneNumber'],
$request['departmentId'],
$request['candidateEmail']
);
foreach ($request->file('documents') as $document) :
$fileName = Str::random(10) . $document->extension();
$document->storeAs('uploads/' . $request['departmentId'], $fileName);
CandidateFiles::create([
'candidate_id' => $newCandidate->id,
'candidate_document_file' => $fileName
]);
endforeach;
return response()->json(['message' => $request['candidateName'] . ' ' . $request['candidatePatronymic']. ' мы приняли вашу заявку! Будем сообщать вам о всех изменениях статуса вашей заявки на конкурс через указанный вами email и номера телефона!'], Response::HTTP_CREATED);
}
}
namespace App\Entity\Candidate;
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model
{
protected $fillable = ['candidate_name', 'candidate_last_name', 'candidate_patronymic', 'candidate_phone_number', 'department_id', 'candidate_email'];
protected $guarded = [];
public function candidateFiles()
{
return $this->hasMany(CandidateFiles::class,'candidate_id','id');
}
public static function createNewCandidate($candidateName, $candidateLastName, $candidatePatronymic, $candidatePhoneNumber, $departmentName, $candidateEmail)
{
return static::create([
'candidate_name' => $candidateName,
'candidate_last_name' => $candidateLastName,
'candidate_patronymic' => $candidatePatronymic,
'candidate_phone_number' => $candidatePhoneNumber,
'department_id' => $departmentName,
'candidate_email' => $candidateEmail,
]);
}
}
namespace App\Entity\Candidate;
use Illuminate\Database\Eloquent\Model;
class CandidateFiles extends Model
{
public function candidate()
{
return $this->belongsTo(Candidate::class);
}
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCandidatesTable extends Migration
{
public function up()
{
Schema::create('candidates', function (Blueprint $table) {
$table->id();
$table->string('candidate_name');
$table->string('candidate_last_name',255);
$table->string('candidate_patronymic',255)->nullable();
$table->string('candidate_phone_number',255);
$table->string('candidate_avatar',255);
$table->integer('department_id')->references('department_id')->on('departments');
$table->string('candidate_email')->unique()->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('candidates');
}
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCandidateFilesTable extends Migration
{
public function up()
{
Schema::create('candidate_files', function (Blueprint $table) {
$table->id();
$table->integer('candidate_id')->references('id')->on('candidates');
$table->string('candidate_document_file',255)->unique()->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('candidate_files');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question