SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' doesn't exist (SQL: insert into `classes_student` (`classes_id`, - laravel

i dont know why it is going to store in mkstudent.classes_student table , i mean i created many to many table class_student and when i tray to store a a student that have a class this error is happening any one who can help me please?
the code is here
student controller
public function store(createstudentrequest $request)
{
$student= Student::create([
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'phone_number'=>$request->phone_number
]);
if($request->class){
$student->classe()->attach($request->class);
}
session()->flash('success','student added successfully');
return redirect(route('students.index'));
}
student.create
<form action="{{route('students.store')}}" method="POST" enctype="multipart/form-data" >
#csrf
<div class="form-group">
<label for="title">First Name</label>
<input type="text" name="first_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Last Name</label>
<input type="text" name="last_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Phone Number</label>
<input type="text" name="phone_number" class="form-control" value="">
</div>
<div class="form-group">
<label for="Class">Class</label>
#if($classes->count() > 0)
<select name="class[]" id="class" multiple="true" class="form-control">
#foreach($classes as $class)
<option value="{{$class->id}}">
{{$class->name}}
</option>
#endforeach
</select>
#endif
</div>
and the migration for class_student is
public function up()
{
Schema::create('class_student', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('classes_id');
$table->integer('student_id');
$table->timestamps();
});
}
and in the student model
class Student extends Model
{
protected $fillable = [
'first_name','last_name','phone_number'
];
public function classe()
{
return $this->belongsToMany(Classes::class);
}
}
in the classes model
public function students()
{
return $this->belongsToMany(Student::class);
}

You can pass table name as second parameter in 'classe' relation like this:
public function classe()
{
return $this->belongsToMany(Classes::class, 'class_student');
}

In relations (in both models) as second parameter you must provide table name class_student because you don't follow convention because you model name is Classes

Related

Field 'vaccine_id' doesn't have a default value

why i'm getting this error?
First view
` #foreach ($appointments as $appointment)
<div class="card m-2 p-2">
<div class="card-body">
<h6 class="card-title">Vaccine: {{ $appointment->vaccine }}</h6>
<div class="mb-6" style="float: right; margin-top: 10px">
<form action="/appointment/{{ $appointment->id }}">
<input type="submit" class="bg-black text-white rounded py-2 px-4 hover:bg-black"
value="Schedule Now">
</form>
</div>
</div>
</div>
#endforeach`
First view Controller
` public function showAppointment()
{
return view('appointmentForm');
}`
Second View
` <form action="/appointment/create" enctype="multipart/form-data" method="POST">
#csrf
<div class="name">
<label for="name" class="form-label">Name:</label>
<input type="text" name="name" id="name" placeholder="Enter name">
</div>
</form>`
Route
`Route::get('/appointment/{id}', [AppointmentController::class, 'showAppointment'])->middleware('guest');`
` public function showAppointment()
{
return view('appointmentForm');
}`
i have two migration file
` Schema::create('appointments', function (Blueprint $table) {
$table->id();
$table->string('vaccine');
$table->timestamps();
});`
` Schema::create('usersappointments', function (Blueprint $table) {
$table->id();
$table->foreignId('vaccine_id')->constrained('appointments')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->timestamps();
});`
Model
` protected $fillable = [
'vaccine_id', 'name'
];`
Controller
` public function createAppointment(Request $request)
{
$data = $request->validate([
'name' => 'required'
]);
usersappointments::create($data);
return redirect('/');
}`
This is how it works; the user select vaccines he want then he will redirect to a page which he will input his name but i'm getting this error
You need to set input vaccine_id in second view in order to send data to controller
<form action="/appointment/create" enctype="multipart/form-data" method="POST">
#csrf
<div class="name">
<label for="name" class="form-label">Name:</label>
<input type="hidden" name="vaccine_id" value="" /> // here get the vaccine_id in value which is selected in first view
<input type="text" name="name" id="name" placeholder="Enter name">
</div>
</form>

multi Input search form

First, I try to check if the two fields 'id' and 'flowid' in form are in the same row or not. If they are in the same row, bring all the data in the row into the view. If not the same, then return and display a message that your inputs don't match. I'm having issues with this. Please advise.
Migration
Schema::create('letters', function (Blueprint $table) {
$table->id()->unique();
$table->string('flowid');
$table->string('id_number');
$table->string('letter_type');
$table->string('name_en');
$table->string('name_ar');
$table->string('nationality_en');
$table->string('nationality_ar');
$table->string('jobt_en');
$table->string('jobt_ar');
$table->string('to_en');
$table->string('to_ar');
$table->date('date_issue');
$table->date('hdate');
$table->date('salary_next');
$table->string('email');
$table->string('iban')->nullable();
$table->string('endser')->nullable();
$table->string('endser1')->nullable();
$table->string('salary_b_l')->nullable();
$table->string('housing_l')->nullable();
$table->string('trans_l')->nullable();
$table->string('mob_all_l')->nullable();
$table->string('other_l')->nullable();
$table->string('total_salary_l')->nullable();
$table->timestamps();
});
Model
class Letter extends Model
{
use HasFactory;
protected $guarded=[];
}
Controller
public function verify(Request $request)
{
$id = $request->id;
$flowid = $request->flowid;
if ($id != "") {
$letter = Letter::where('flowid', 'LIKE', '%' . $id . '%')->get();
if (!empty($flowid)) {
$letter->where('flowid', '=', $flowid);
}
$letter = Letter::findOrFail($letter());
return view('layouts.verify', compact('letter'));
}
}
View
<form action="{{ route('letter.verfiy') }}" method="GET">
{{-- #csrf --}}
<div class="form-group">
<label class="control-label col-sm-2" for="id">
Certificate Code:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="id"
placeholder="Type certificate Code">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="flowid">
Flow Employee ID:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="flowid"
placeholder="Type Flow ID">
</div>
</div>
<button type="submit" class="btn btn-primary">check</button>
</form>
Route
Route::any('/verfiy', [LetterController::class, 'verify',])->name('letter.verfiy');
Try that
$id = $request->id;
$flowid = $request->flowid;
if (!empty($id) {
$letter = Letter::find($id);
if (!empty($flowid)) {
if($letter->flowid === $flowid){
return view('layouts.verify', compact('letter'));
}
return 'Error'; // return error or redirect
}
}
check if the two fields 'id' and 'flowid' in form are in the same row or not
I believe a basic where clause should do the job for you here because you are checking id and flowid in one row.
$letter = Letter::where('id', $id)->where('flowid', $flowId)->first();
if (! $letter) {
return "Letter not found";
}
return $letter;

Laravel - How to Make Lesson aggregate score not more than course max score

I am developing a web application on Student Course Management using Laravel-5.8
Models
class Lesson extends Model
{
protected $table = 'Lessons';
protected $fillable = [
'lesson_name',
'course_id',
'student_id',
'score_obtained',
];
public function gradelevel()
{
return $this->belongsTo('App\Models\Course','course_id');
}
public function student()
{
return $this->belongsTo('App\Models\Student','student_id');
}
}
class Course extends Model
{
protected $table = 'courses';
protected $fillable = [
'course_code',
'course_name',
'max_score',
];
}
Controller
class LessonController extends Controller
{
public function create()
{
$courses = Course::all();
$students = Student::all();
return view('lessons.create')->with('courses', $courses)->with('students', $students);
}
public function store(StoreLessonRequest $request)
{
try {
$lesson = Lesson::create([
'lesson_name' => $request->lesson_name,
'course_id' => $request->course_id,
'lesson_id' => $request->lesson_id,
'score_obtained' => $request->score_obtained,
]);
Session::flash('success', 'Lesson is created successfully');
return redirect()->route('lessons.index');
} catch (Exception $exception) {
Session::flash('danger', 'Lesson creation failed!');
return redirect()->route('lessons.index');
}
}
}
create.blade
<form action="{{route('lessons.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Course</label>
<select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="course_id" style="width: 100%;">>
<option value="">Select Course</option>
#if($courses->count() > 0)
#foreach($courses as $course)
<option value="{{$course->id}}">{{$course->course_name}}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Student</label>
<select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="student_id" style="width: 100%;">>
<option value="">Select Student</option>
#if($students->count() > 0)
#foreach($students as $student)
<option value="{{$student->id}}">{{$student->student_name}}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Lesson<span style="color:red;">*</span></label>
<input type="text" name="lesson_name" placeholder="Enter lesson here" class="form-control" value="{{old('lesson_name')}}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Obtained Score<span style="color:red;">*</span></label>
<input type="text" name="score_obtained" placeholder="Enter score obtained here" class="form-control" value="{{old('score_obtained')}}">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" id="submit_create" class="btn btn-primary">Score</button>
</div>
</form>
Each core have maximum score allocated and this is done at the setting. For each course, students have lesson and scores are obtained for each lesson.
What I want to achieve is that on the lesson create form, what student score_obtained are being entered the application should sum up all the scores (from the lesson table and the just entered one) the student obtained for that particular course including the one in the textbox, if its more that what is in the max_score in the courses table for that particular course the application should display a message and shouldn't allow the user to submit.
How do I achieve this?
Thank you.
In store method, you can write below code:
$maxScore = Course::find($request->course_id)->max_score;
$scoresObtained = Lession::where('course_id' , $request->course_id)->sum('score_obtained');
$totalScore = intval($scoresObtained) + intval($request->score_obtained);
if($totalScore > $maxScore)
{
return redirect()->back()->withErrors([__('Total score exceeds max score for selected course')]);
}
You can use floatval() in place of intval() in case your scores are in decimals

data isn't saving into db using eloquent relation

hi m trying to save data into db using Eloquent ORM one-to-one but it is not saving and not showing any error to fix it (m unable to find any solution about it because it is not showing any error),
public function store(Request $request)
{
$request->validate([
'owner_id' => 'required',
'phone_name' => 'required|unique:phones'
]);
$phone = new Phone;
$phone->owner_id = $request->owner_id;
$phone->phone_name = $request->phone_name;
$phone->save();
return redirect()->route('phone.index')->with('flash_messages_success', 'Phone has been added successfully');
}
protected $fillable = [
'pphone_name', 'owner_id'
];
public function owner()
{
return $this->belongsTo('App\Owner');
}
<form method="POST" action="{{ route('phone.store') }}">
#csrf
<div class="form-group">
<label for="">Phone Title</label>
<input type="text" name="phone_name" class="form-control" id="" placeholder="Phone Name">
</div>
<div class="form-group">
<label>Select Owner</label>
<select class="form-control" name="category_id">
<option selected="">Under Owner</option>
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->owner_name }}</option>
#endforeach
</select>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
The issue in select name you named category_id so the view should be:
<select class="form-control" name="owner_id">

laravel edit and update doesn't work

I have a CRUD with simple date. But I can't get the update method to work.
This is my controller
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, Customer
$customer,$id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
and that is my view
<form method="POST" action="{{route('customer.update',$customer->id ) }}">
{{--{{dd($customer)}}--}}
{{method_field('PUT')}}
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">Voornaam</label>
<input type="text" class="form-control" name="firstName" value="{{$customer->firstName}}">
</div>
<div class="form-group">
<label for="lastName">Achternaam</label>
<input type="text" class="form-control" name="lastName" value="{{$customer->lastName}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" value="{{$customer->email}}">
</div>
<div class="form-group">
<label for="phone">Telefoonnummer</label>
<input type="text" class="form-control" name="phone" value="{{$customer->phone}}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
I can go to the edit page. But, after changing the data nothing change it look like that I miss save somewhere but I don't know, now I get that that error too few arguments to function New Controller ::update () 2 passed and exactly 3 expected.
any help will be appreciated.
Change:
public function update(CustomerRequest $request, Customer
$customer,$id)
To:
public function update(CustomerRequest $request, $id)
Or:
public function update(CustomerRequest $request, Customer
$customer)
With the last one you can remove Customer::find($id) and just use $customer
EDIT
If you look at the update route: route('customer.update',$customer->id ) you see it only takes 1 argument. The controller expects 2 because one is the request and the other is the ID.

Resources