Why data isn't storing with controller#store method - php

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;
}

Related

ErrorException Undefined variable: toplam (View: C:\wamp64\www\laravel\resources\views\form.blade.php)

Variable toplam cannot be found
i'm using laravel latest version
HomeController like this
public function get_form()
{
return view('form');
}
public function post_form(Request $request)
{
$bDegisken=$request->birinci;
$iDegisken=$request->ikinci;
$toplam=$bDegisken+$iDegisken;
return view('form')->with('toplam',$toplam);
}
web.php my file is like this
//get
Route::get('/form' , [HomeController::class, 'get_form']);
//post
Route::post('/form' , [HomeController::class, 'post_form']);
form.blade.php
<form action="" method="post">
{{csrf_field()}}
<div class="col-md-6">
<input type="text" name="birinci">
</div>
<div class="col-md-6">
<input type="text" name="ikinci">
</div>
<input type="submit">
</form><br><br><br>
{{"sonuç"." = ".$toplam}}
you have 2 option
1st send data from get method
2nd make option data in view file
Step 1
public function get_form()
{
$toplam = "";
return view('form')->with('toplam', $toplam);
}
step 2
<form action="" method="post">
{{csrf_field()}}
<div class="col-md-6">
<input type="text" name="birinci">
</div>
<div class="col-md-6">
<input type="text" name="ikinci">
</div>
<input type="submit">
</form><br><br><br>
{{"sonuç"." = ".$toplam ?? ""}}<< --------------- it is optional here

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

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();
});

How to display the form's content

Problem with displaying content entered in the form in laravel.
What code to add to display the value entered in the form?
--web.php
Route::get('/show-name', ['uses' => 'NameController#show-name', 'middleware' => 'auth']);
--
NameContoller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name()
{
return view('show-name');
}
}
--
show-name.blade.php
<?php
print_r($_POST);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Message after using the button - MethodNotAllowedHttpException.
I will be grateful for your help.
You're sending a POST request, not a GET request.
Route::post('/show-name', [
'uses' => 'NameController#show-name',
'middleware' => 'auth'
]);
The documentation tells you how to get all the data
https://laravel.com/docs/5.8/requests#retrieving-input
The documentation tells you have to pass data to a view https://laravel.com/docs/5.8/views#passing-data-to-views
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name(Request $request)
{
$input = $request->all();
return view('show-name')->with('data', $input);
}
}
The documentation tells you how to access data passed into a view https://laravel.com/docs/5.8/blade#displaying-data
#php
echo print_r($data);
#endphp
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Further more, the form won't work without CSFR https://laravel.com/docs/5.8/blade#forms
Inputs are self closing, meaning there is no need for </input>
Further more, there is no need for <?php echo $_SERVER['PHP_SELF']; ?> because with Laravel, you can specifically define the name of the route.
<form method="post" action="/show-name" class="">
Watch some basic tutorials on Laravel because you're going about this all incorrectly.

Laravel 5.5 - $request->file returns null

I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.
MY HTML FORM
<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
<label class="control-label"> </label>
<div class="controls">
<div class="control-group text-center">
<label class="btn btn-primary" for="file-selector">
<input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required> Upload List </label>
<span class='label label-default' id="upload-file-info"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
</div>
</div>
</form>
MY CONTROLLER: I am using the import method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::with('roles')->where('id', Auth::id())->get();
$types = Type::where('user_id', Auth::id())
->where('active',1)->get();
return view('view_import',compact('user','types'));
}
public function import(ImportTypeRequest $request)
{
$template_upload = $request->file('template_upload');
dd($template_upload);
}
}
Here are some suggested ways trying to solve this.
First of all in your import method add dd($request->all()) at its top and see what's the response. You should see all your form data and of course template_upload file. That's how you make sure that you see all the coming data from your form to your controller method.
Then try to get rid of ImportTypeRequest and just use Illuminate\Http\Request to see what will you get. If you got different result then the problem is in ImportTypeRequest class.
Then why don't you just use $request->template_upload?! It's cleaner I guess.

Laravel : How to use a parameters in a Form POST to be use in a Route::post?

This my current POST route.
Route::post('/eAPI', 'ApiController#eAPI');
I wanted to make it like
Route::post('/q={$number}', 'ApiController#eAPI');
But in my form.
<form action="{{url('/eAPI')}}" method="post" id="search">
<div class="form-group">
<label for="number" class="col-md-4 control-label">Telephone Number to search :</label>
<div class="col-md-6">
<input class="form-control" id="number" name="number" placeholder="Phone (eg. 5551234567)" required>
</div>
</div>
<div class="col-md-2">
<input type="submit" name="name" value="Find" class="btn btn-success">
</div>
</form>
Now, I want to put a variable in this part, something like this.
<form action="{{url('/?q=$number')}}" method="post" id="search">
In post request you should do it like this:
Route::post('/eAPI/{q}', 'ApiController#eAPI')->name('my_route');
And in HTML Form:
<form action="{{ route('my_route', ['q' => '4']) }}" method="post" id="search">
</form>
And inside controller you can retrieve it as:
Class ApiController {
public function eAPI($q) {
// Use $q here ...
}
}
Hope this helps!
This works for me [method post and url has ?q=someValue] :
public function eApi(Request $request){
$q = $request['q'];
}
This code will get all params in post and get method
$request->all()
Hope it helps!
I never did and will never do this with post requests, but it works with get requests:
$q = request()->q;
And you don't need to add this to the route: q={$number}, just add parameters to url: ?q=value1&s=value2&c=value3

Categories