I set default value on my migration but When I am create and not giving any value it's showing error - laravel

I set commission default as 0.00. But when I insert the data but don't give any on commission. it's giving an error that commission can't be null.
*Here is my migration code:*
public function up()
{
Schema::create('pc_lists', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->decimal('commission')->default('0.00');
$table->tinyInteger('status')->default('1');
});
}
*My blade code is here:*
<div class="card-body">
<form action="" method="POST"
enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col">
<label for="commission">Commission (%)</label>
<input type="number" min="0" name="commission"
class="form-control" >
</div>
<div class="form-row">
<div class="col" style="margin-top: 29px !important;">
<input type="submit" class="btn btn-success"
value="Add Personal" >
</div>
</div>
</form>
</div>

You are setting string to integer and decimal don't use '' on default.
public function up()
{
Schema::create('pc_lists', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->decimal('commission')->default(0.00);
$table->tinyInteger('status')->default(1);
});
}

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>

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

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

Value Displays Blank when I tried to edit in Laravel

I want to edit in laravel
In my controller I have this
public function edit(Book $book)
{
return view ('admin.book.edit')->with('book', $book)->with('authors', Author::all())->with('categories', Category::all());
}
In my index view for all books, I have this
<tbody>
#foreach($books as $book)
...
<td> {{ $book -> name }} </td>
<td>
<a href="{{ route('admin.book.edit',$book->id) }}">
<button class="btn btn-success">Edit</button>
</a>
</td>
...
My Edit view is like this
<form action="#" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="name">Book Name</label>
<input type="text" class="form-control" name="name" id="name" value="{{ $book->name }}" >
</div>
<div class="form-group">
<label for="about">About the Book </label>
<textarea name="about" id="about" cols="5" rows="5" class="form-control">{{ $book->about }}</textarea>
</div>
In my web.php
Route::get('/admin/book/edit', 'BooksController#edit')->name('admin.book.edit');
My table schema
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('about');
...
});
I was thinking this value="{{ $book->name }}" will return the value but it returned blank. Probably I am doing something wrong here.
Hi you don't define in your route Model bindings:
Change it to, so Laravel knows that the ID you pass in route should return Book Model. That you use in your edit function edit(Book $book)
Route::get('/admin/book/edit/{book}', 'BooksController#edit')->name('admin.book.edit');
Also take a look into Resource Controllers:
Route::resource('books', 'BooksController');
This will generate all routes you need view/edit/update/delete Check your routes bu running php artisan route:list
Read Docs for more info
They was an error in my route, so I change to this
Route::get('/admin/book/edit/{book}', 'BooksController#edit')->name('admin.book.edit');

adding data in table show me error when i add the relation ships

I am a beginner in laravel I made a relationship one to many between the table of father and the table of eleve after that when i try to add a a new student he shows this error
SQLSTATE[HY000]: General error: 1364 Field 'father_id' doesn't have a default value (SQL: insert into eleves (nom, prenom, adresse, date_naiss, sexe, nationnalite, niveau_scolaire, updated_at, created_at) values (mohamed, ferchichi, tunis, 2018-07-22, Un garçon, tunisen, 1, 2019-05-13 10:56:28, 2019-05-13 10:56:28))
how I can correct this problem i need your help
this is the table of eleve
public function up()
{
Schema::create('eleves', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('father_id');
$table->string('nom');
$table->string('prenom');
$table->date('date_naiss');
$table->string('sexe');
$table->string('nationnalite');
$table->string('niveau_scolaire');
$table->string('adresse');
$table->foreign('father_id')->references('id')->on('fathers')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
the model of eleve
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Eleve extends Model
{
protected $fillable = ['nom', 'father_id', 'prenom', 'date_naiss', 'sexe', 'nationnalite', 'niveau_scolaire', 'adresse'];
public function father()
{
return $this->belongsTo('App\Father');
}
}
the model of father
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Father extends Model
{
protected $fillable = ['nom', 'prenom', 'adresse', 'num_tel', 'email', 'login', 'date_naissance ', 'password'];
public function user()
{
return $this->belongsTo(User::class);
}
public function eleve()
{
return $this->hasMany('App\Eleve');
}
and the blade of eleve
<section id="no-more-tables">
<table class="table table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>id-eleve</th>
<th>Nom</th>
<th>Prenom</th>
<th>Adresse</th>
<th>Age</th>
<th>Sexe</th>
<th>Nationnalité</th>
<th>Niveau scolaire </th>
<th>les actions</th>
</tr>
</thead>
<tbody>
#foreach($eleves as $eleve)
<tr>
<td class="numeric" data-title="id-parent">{{$eleve->id}}</td>
<td class="numeric" data-title="Nom">{{$eleve->nom}}</td>
<td class="numeric" data-title="Prenom">{{$eleve->prenom}}</td>
<td class="numeric" data-title="Adresse">{{$eleve->adresse}}</td>
<td class="numeric" data-title="Numéro telephone">{{$eleve->date_naiss}}</td>
<td class="numeric" data-title="Email">{{$eleve->sexe}}</td>
<td class="numeric" data-title="Login">{{$eleve->nationnalite}}</td>
<td class="numeric" data-title="Password">{{$eleve->niveau_scolaire}}</td>
<td>
<button href="#editEmployeeModal" class="btn btn-theme" data-target="#editEmployeeModal "
data-mytitle="{{$eleve->nom}}" data-myprenom="{{$eleve->prenom}}"
data-myadresse="{{$eleve->adresse}}" data-myage="{{$eleve->date_naiss}}"
data-mysexe="{{$eleve->sexe}}" data-mynationalite="{{$eleve->nationnalite}}"
data-myniveau="{{$eleve->niveau_scolaire}}" data-catid={{$eleve->id}} class="edit"
data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Edit"></i>
</button>
<button href="#deleteEmployeeModal" class="btn btn-theme" data-target="#deleteEmployeeModal"
data-catid={{$eleve->id}} class="delete" data-toggle="modal"> <i class="material-icons"
data-toggle="tooltip" title="Delete"></i> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
<div class="text-center">
{{ $eleves->links() }}
</div>
<div class="clearfix">
<div class="hint-text">Affichage de <b>5</b> sur <b>25</b> entrées</div>
<div id="addEmployeeModal" href="create" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('eleves.store')}}" method="post">
{{csrf_field()}}
<div class="modal-header">
<h4 class="modal-title">Ajouter un éléve</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>nom</label>
<input type="text" id="nom" name="nom" class="form-control" required>
</div>
<div class="form-group">
<label>prenom</label>
<input type="text" id="prenom" name="prenom" class="form-control" required>
</div>
<div class="form-group">
<label>adresse</label>
<textarea name="adresse" id="adresse" class="form-control" required></textarea>
</div>
<div class="form-group">
<label for="start">Date Naissance</label>
<input type="date" id="date_naiss" name="date_naiss" value="2018-07-22" min="2018-01-01"
max="2030-12-31">
<!-- <label>Date Naissance</label>
<input type="text" name=" date_naiss" id="date_naiss" class="form-control" required>
</div> -->
</div>
<div class="form-group">
<div>
<input type="radio" id="sexe" name="sexe" value="une fille" checked>
<label for="sexe">une fille</label>
</div>
<div>
<input type="radio" id="sexe" name="sexe" value="Un garçon">
<label for="sexe">Un garçon</label>
</div>
</div>
<div class="form-group">
<label>Nationnalité</label>
<input type="text" name="nationnalite" id="nationnalite" class="form-control" required>
</div>
<div class="form-group">
<label>Niveau Scolaire</label>
<input type="text" name="niveau_scolaire" id="niveau_scolaire" class="form-control"
required>
</div>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Annuler">
<input type="submit" class="btn btn-success" value="Ajouter">
</div>
</form>
</div>
</div>
</div>
</div>
</section>
the controller of student
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Eleve;
class EleveController extends Controller
{
public function index()
{
$eleves = Eleve::paginate(5);
return view('admin.eleves',compact('eleves'));
}
public function store(Request $request)
{
Eleve::create($request->all());
session()->flash('success',' Cet nouvel éléve a été enregistré avec succés');
return redirect()->back();
}
public function update(Request $request, $id)
{
$eleve = Eleve::findOrFail($request->eleve_id);
$eleve->update($request->all());
session()->flash('success','Cet éléve a été modifié avec succés');
return redirect()->back();
}
public function destroy(Request $request)
{
$eleve = Eleve::findOrFail($request->eleve_id);
$eleve->delete();
session()->flash('success','Cet éleve a été supprimé avec succés');
return redirect()->back();
}
}
The father_id field in the eleves table is required. You either have to make it optional or, more likely, add it to your form.
To make the field optional replace this line
$table->unsignedBigInteger('father_id');
with
$table->unsignedBigInteger('father_id')->nullable();
To add it to the form you would probably use a <select>, something like:
<form ...>
<select name="father_id">
#foreach(\App\Father::all() as $father)
<option name="{{ $father->id }}">{{ $father->nom }}</option>
#endforeach
</select>
Note: It's bad practice to call the database inside a view file. A better approach would be to load the Fathers inside the controller and pass it to the view:
public function create()
{
$fathers = Father::all();
return view('...', ['fathers' => $fathers]);
}
and in the view:
<form ...>
<select name="father_id">
#foreach($fathers as $father)
<option name="{{ $father->id }}">{{ $father->nom }}</option>
#endforeach
</select>
in the EleveController i add use App\Father; and i add this function
public function create()
{
$fathers= Father::all();
return view('admin.eleves',compact('fathers'));
}
and in the eleve blade.php i add this
<select name="father_id">
#foreach($fathers as $father)
<option name="{{ $father->id }}">{{ $father->nom }}</option>
#endforeach
</select>
the problem is that shows me the same error "Undefined variable: fathers "

displaying multiple images from database in laravel

i am new to laravel and i want to ask how to display multiple images that is located in the database.
i have already inserted images in my database through this code
here is my try.blade.php.
<h3>Upload Image</h3>
<form name="form" action="imageupload" method="POST" enctype="multipart/form-
data">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="file" name="img[]" multiple="true">
<br><br>
<input type="submit" name="ok" value="Upload">
</form>
#if(Session::has('msg'))
{{ Session::get('msg')}}
#endif
and here is my web.php
Route::get('imageup','userLog#imageup');
Route::post('imageupload','userLog#imageupup');
and here is my controller
public function imageup(){
return view('try');
}
public function imageupup(Request $request){
if($request->hasFile('img')){
$image_array = $request->file('img');
$array_len = count($image_array);
for($i=0; $i<$array_len; $i++){
$image_size = $image_array[$i]->getClientSize();
$image_ext = $image_array[$i]->getClientOriginalExtension();
$new_image_name = rand(1234,9999).".".$image_ext;
$destination_path = public_path('/images');
$image_array[$i]->move($destination_path,$new_image_name);
$tab = new Tab1;
$tab->image_size = $image_size;
$tab->image_name = $new_image_name;
$tab->save();
}
return back()->with('msg','Images Uploade Successfully');
}
else{
return back()->with('msg','Please choose any image file');
}
}
this is the code in my table
Schema::create('tab1s', function (Blueprint $table) {
$table->increments('id');
$table->string('image_name');
$table->string('image_size');
$table->timestamps();
});
i hope someone can help me.
#foreach(json_decode($posts->images, true) as $images)
<div class="col-lg-2 col-md-2 col-sm-2">
<a href="{{ URL::to('img/offers/'.$images)}}"
class="portfolio-box">
<img src="{{ URL::to('img/offers/'.$images)}}"
class="img-responsive" alt="--">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<span class="glyphicon glyphicon-zoom-in"
style="font-size: 80px"></span>
</div>
</div>
</a>
</div>
#endforeach

Resources