Laravel 5.8 Field 'name doesn't have a default value - php

Hello im developing a students CRUD in laravel but i have a problem saving the data in my db.
Here is the problem that laravel returns. SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
My store function.
public function store(Request $request)
{
$alumno = Alumno::create();
$alumno->fill($request->all());
$alumno->save();
return redirect('/alumnos');
}
My model:
class Alumno extends Model
{
protected $fillable = ['name','apellido','matricula','correo'];
}
My form:
<form action="/alumnos" method="post">
#csrf
<fieldset class="form-fieldset">
<div class="form-group">
<label class="form-label">Nombre<span class="form-required">*</span></label>
<input type="text" class="form-control" name="name" required/>
</div>
<div class="form-group">
<label class="form-label">Apellido<span class="form-required">*</span></label>
<input type="text" class="form-control" name="apellido" required/>
</div>
<div class="form-group">
<label class="form-label">Matricula<span class="form-required">*</span></label>
<input type="number" class="form-control" required name="matricula" />
</div>
<div class="form-group mb-0">
<label class="form-label">Correo Electronico<span class="form-required">*</span></label>
<input type="email" class="form-control" name="correo" required />
</div>
</fieldset>
<input type="submit" class="btn btn-primary" value="Guardar" />
</form>
What im doing wrong? Please help and thank you!!! :)

The problem is from the form action URL try to change the action to {{ action('YourController#store') }}

You should save data as below:
public function store(Request $request)
{
$alumno = new Alumno();
$alumno = $alumno->create($request->all());
return redirect('/alumnos');
}
and it will work fine.

You should add line to migration
$table->string('name')->nullable();
set form as above
<form action="{{action('YourController#store')}}">
For insert all request inputs
Alumno::create($request->all());

You solve this problem in two different ways
Your database structure has to be changed like below. Default field must be Null. Where description field is your database field.
Like below this
Open your model database/migrations/your_model then use like this code
Schema::table('your_model', function (Blueprint $table) {
$table->string('name')->nullable();
});

Related

Laravel, data update Error. There is no error reminder, but the data does not insert into database

I encounter a problem that the data cannot be stored in the database. when I use the save function to update a column, there is no error reminder and does not insert as well. I have no idea. Help me, Bro.
I have a supervisor table that includes the id, user_id, name, email, expertise, and initial letter columns. the primary key is user_id.
this is my route for an edit view
Route::get('supervisor/profile/{id}/edit','UserController#supProfileEdit');
this is the view to post the new data.
Route::post('supervisor/profile/{id}','UserController#supProfileUpdate');
a blade is a form :
<div class="card">
<div class="card-header bg-primary text-white">Profile</div>
<div class="card-body">
#foreach($supervisor as $data)
<form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->user_id)}}"
enctype="multipart/form-data">
#csrf
<div>
<label for="name"> Name : </label>
<input id="name" disabled value="{{$data->name}}">
</div>
<div>
<label for="email">Email : </label>
<input id="email" disabled value="{{$data->email}}">
</div>
<div>
<label class="inline" for="expertise">Expertise : </label>
<input type="text" id="expertise" name="expertise" value="{{$data->expertise}}">
</div>
<div>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
</div>
</div>
#endforeach
Then, this is my function in Controller: I just wanna update the expertise.
public function supProfileUpdate(Supervisor $supervisor)
{
$this->validate(request(),[
'expertise'=>'required',
]);
$supervisor->expertise = request('expertise');
$supervisor->save();
return redirect()->back();
}
I think the problem caused by
Route::post('supervisor/profile/{id}','UserController#supProfileUpdate');
where the Supervisor model primary key is id while you trying to get it using the user_id key.
So I will suggest that you either implement getRouteKeyName in Supervisor model as follows
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'user_id';
}
or by using the id when sending it to the form
<form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->id)}}"

Unable to store in database while using Laravel (version 7.25)

I am trying to get hold of Laravel framework and got stuck while retrieving the values from the page in a Database.
These are the snippets of my controller, routes, view part which might help in understanding what I am doing
web.php (for routes):
<?php
use Illuminate\Support\Facades\Route;
Route::resource('posts', 'PostController');
Postcontroller.php (just a snippet)
<?php
public function create()
{
//
return view('posts.create');
}
public function store(Request $request)
{
//
$this->validate($request , array(
'title' => 'required',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
create.blade.php(for view)
<div class="row">
<div class="col-mid-8 col-md-offset-2" >
<h1>Create New Post</h1>
<hr>
<form route='posts.store' >
#csrf
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
The problem with your code is that you don't have name properties on your input, so nothing gets passed to your backend code.
Add name to your inputs like this (they need to be different, else they would get overwritten):
<input type="text" class="form-control" id="your-unique-input-id" name"your_unique_input_name">
Then, when you submit your form, the all the inputs that have name attribute in them will get passed to your backend method. You can then retrieve their value, by using the name you gave them earlier. So in this example:
$request->your_unique_input_name // Which will return anything that user typed in that field

Why data isn't storing with controller#store method

I used method store from a controller and data doesn't store in my database.
I want to insert NOM_ARTICLE, PHOTO_ARTICLE, TYPE, DESCRIPTION_ARTICLE in three tables from a form in native HTML, but I don't know what action to do.
I am using ArticleController where there is a method store and three table models.
public function create()
{
return view('addarticle');
}
public function store(Request $request)
{
$article = new article;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->LABEL_TYPE = $request->LABEL_TYPE;
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
Here are my tables from database:
article ('ID_ARTICLE','NOM_ARTICLE','ID_TYPE,'DESCRIPTION_ARTICLE')
photo_articles('ID_PHOTO','ID_ARTICLE','PHOTO_ARTICLE')
type('ID_TYPE','TYPE')
My HTML form:
<form method="post" action="" class="contact_form text-center" id="contact_form">
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required" />
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE"placeholder="Description" required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
And I have my route in web.php:
Route::resource('addarticle','ArticleController');
And this is my Article model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class article extends Model {
public $table = 'article';
public $primaryKey ='ID_ARTICLE';
return $this->belongsTo('photo_articles');
}
After I click on the submit button it shows a URL like this: http://localhost/testprojet/public/addarticle?NOM_ARTICLE=test&ID_TYPE=book&MAX_FILE_SIZE=250000&PHOTO_ARTICLE=villle+icon.jpg&DESCRIPTION_ARTICLE=ss
And redirecting to my view addarticle but nothing gets added in the database.
Yaa You clearly missed csrf token to pass with the form. If you want to bubmit any form in Laravel, you should pass csrf token.
use #csrf and blade will pass the unique token to the form submit
https://laravel.com/docs/5.8/csrf
Example:
<form method="post" action="" class="contact_form text-center" id="contact_form">
#csrf
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
What I see from your form is that you didn't put any action in it, you just left it empty:
<form method="post" action="" class="contact_form text-center" id="contact_form">
You should be putting in a route to your store method in your controller, so that when the form is submitted, the data will be passed into your controller.
Also, providing a link to your localhost isn't helping us to understand your problem as we can't access to it. A solid screenshot will do.
You should put the action according to the resource pattern.
Your web.php
Route::resource('articles','ArticleController');
Your form:
<form method="post" action="{{route('articles.store')}}" class="contact_form text-center" id="contact_form">
#csrf
You should include the #csrf blade directive, as well.
In you controller:
public function store(Request $request)
{
$article = new Article();
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
You're adding the PHOTO_ARTICLE to the article but you dont have this column in your articles table as you said, so it dont make sense to use the line below:
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
Also, you're not receiving any LABEL_TYPE from your request, and you dont have this column in your articles table too. So you must remove this line:
$article->LABEL_TYPE = $request->LABEL_TYPE;
Try this: I hope it will help you.
Controller
public function store(Request $request)
{
$articleObj = new article;
$articleObj->add($request);
//second table
$employerObj = new Employer();
$employerObj->add($request);
}
Create three model according to your datatables and paste this code for all.
function add($request)
{
$this->NOM_ARTICLE = $request->NOM_ARTICLE;
$this->LABEL_TYPE = $request->LABEL_TYPE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->save();
return $this->id;
}

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

Laravel insert into Database

I have the following problem:
I want to insert into my database, with input fields.
Here is my html:
<div class="row">
<form action="{{ action('test#store') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-md-2 col-sm-2">
<label for="MtrNr">MtrNr:</label>
<input type="text" name="MtrNr" class="form-control">
</div>
<div class="col-md-2 col-sm-3">
<label for="Vorname">Vorname:</label>
<input type="text" name="Vorname" class="form-control">
</div>
<div class="col-md-2 col-sm-3">
<label for="Nachname">Nachname:</label>
<input type="text" name="Nachname" class="form-control" >
</div>
<div class="col-md-2 col-sm-2">
<label for="Klassenname">Klassenname:</label>
<input type="text" name="Klassenname" class="form-control">
</div>
<div class="col-md-2 col-sm-2">
<button class="btn btn-primary option-button button_rowAllign" type="submit">Schüler hinzufügen</button>
</div>
</form>
</div>
My controller:
public function store(Request $request)
{
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
And here my routes:
Route::post("{{ action('test#store') }}", "test#store");
The thing is, that I already inserted into my database. But when i tried to
reproduce it, it doesn't work.
For local server I use xampp.
I hope you can help me.
The problem is that you only created a new Schueler object and gave the data to it. But you didn't save it in the database. You only made local changes.
Use this method to save.
public function store(Request $request) {
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
Try changing your routes like this
Route::post("/test", "test#store"); // change /test to /anything you want
Before all make sure you have the database setup and there are all tables in that database like schuelers - For that do php artisan migrate
If you want to Insert record into the database you miss to add $user->save() in your controller
public function store(Request $request)
{
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
I think something wrong with your route.
Route::post("{{ action('test#store') }}", "test#store");
Route should be something like below:
Route::post("schueler/store", "yourcontrollername#store");
//This is sample code, Please update with your one.

Categories