Laravel 5 storing empty row in database? - php

I'm currently working on a simple blog system for my website, I'm using Laravel 5.5 for this, but I'm having a problem. Whenever I create a new article using the form I created, it is storing a empty row in my database instead of the data from the form...
This is my function to store a article:
public function newsStore()
{
$input = Request::all();
Article::create($input);
return $input;
}
According to the tutorial that I'm following should this do the trick.. This is the form that I'm currently using:
{!! Form::open(['url' => 'news']) !!}
<!-- Article Title -->
<div class="form-group row">
{!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Description -->
<div class="form-group row">
{!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Excerpt -->
<div class="form-group row">
{!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Image -->
<div class="form-group row">
{!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::file('image') !!}
</div>
</div>
<!-- Article Message -->
<div class="form-group row">
{!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
</div>
</div>
<!-- Article Submit -->
<div class="form-group">
{!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
</div>
{!! Form::close() !!}
This is the route I'm using:
Route::post('news', 'DashboardController#newsStore'); // The articles store route
This is my model:
class Article extends Model
{
protected $fillable = [
'title',
'description',
'excerpt',
'image',
'body',
'published_at'
];
}
I have done this once before and have tried to redo it all but can't seem to figure out what is going wrong.
Thanks in advance!

To make create() work make sure you're using columns with same names. For example, if your column name is title, you need to use title instead of news-title in the form too:
{!! Form::text('title', null, ['class' => 'form-control']) !!}

As you use this Article::create(); it means all your input names should be same as table column name.
Ex:
<input name="title" ... same be same to column call news-title in respective table
If not same
Use Query Builder
DB::table('table_name')->insert(
[
'name' => $input->news-title
.....
]
);
Read More about Hyphens in column names in MySQL DB

Related

Laravel blade views not showing updated content

I have a Laravel site running on a GoDaddy share hosting plan. I'm on PHP version 7.1. It has functioned fine up until yesterday. Now the blade views no longer show changes that occur on the database.
I see that the POST functions are working, as the changes are reflected in the database. They're just not showing up in the views themselves.
I've tried to clear the view cache as suggested here: Blade view not reflecting changes
I have also changed the timezone in config/app to 'America/Phoenix' to try and match godaddy's servers: https://www.godaddy.com/community/cPanel-Hosting/How-To-Change-Timezone-on-a-shared-server/td-p/102712
I've contacted GoDaddy's tech support and they couldn't find anything they believed could cause it.
Example Route:
//Resource
Route::resource('beer', 'BeerController');
Example Controller:
public function update(Request $request, Beer $beer)
{
$this->validate($request, ['name' => 'required']);
$beer->update(request(['name', 'beer_style_id', 'style', 'abv', 'ibus', 'srm', 'brewery_id', 'on_tap']));
return view('beers.show', compact('beer'));
}
Example View
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-sm-12">
<h1>Edit {{ $beer->name }}</h1>
<hr />
{!! Form::model($beer, ['route' => ['beer.update', $beer->id], 'method' => 'patch']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('brewery_id', 'Brewery') !!}<br />
{!! Form::select('brewery_id', $breweries, null, ['placeholder' => 'Select Brewery', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="row" style="margin-bottom: 1rem;">
<div class="col">
{!! Form::label('beer_style_id', 'Style Family') !!}<br />
{!! Form::select('beer_style_id', $beerstyles, null, ['placeholder' => 'Select Style', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="col">
{!! Form::label('style', 'Style') !!}
{!! Form::text('style', $value = null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 1.5rem;">
<div class="col">
{!! Form::label('abv', 'ABV') !!}
<div class="input-group">
{!! Form::number('abv', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
<span class="input-group-addon">%</span>
</div>
</div>
<div class="col">
{!! Form::label('ibus', 'IBUs') !!}
{!! Form::number('ibus', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col">
{!! Form::label('srm', 'SRM') !!}
{!! Form::number('srm', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
</div>
</div>
<div class="form-group" id="on-tap-checkbox">
{!! Form::checkbox('on_tap', '1') !!} On Tap
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
#include('layouts.errors')
{!! Form::close() !!}
</div>
</div>
#endsection

Laravel validation with unique fields

I have been trying unique validation in laravel 5.7. Below is my code for validation.
$this->validate($request, [
'name' => 'required|unique:permissions,name',
'slug' => 'required|unique:permissions,slug',
]);
And the html is below:
<div class='container'>
{!! Form::open(array('route' => 'permission.save','method'=>'POST')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Slug:</strong>
{!! Form::text('slug', null, array('placeholder' => 'Slug','class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
Unique validation for 'name' is working fine but it is not working for 'slug'. It's really weird i can't get it. Please provide suggestions to fix this issue. Any help would be great.
You need to specify column name from database table in unique validation rule
https://laravel.com/docs/5.6/validation#rule-unique
for example your column name for slug is : column_slug
$this->validate($request, [
'name' => 'required|unique:permissions,name',
'slug' => 'required|unique:permissions,column_slug', // column_slug may be different in your case
]);
And ensure that you have set unique key for that column in database table
Hope it helps !

Laravel form with <div> tags in the middle

I am trying to use a form with div tags in the middle. However, it doesnt seem to work, the form works fine when used without them. Is there any way to stylise the form and use it ?
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('', '', ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
<div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
</div>
</div>
</div>
{!! Form::close() !!}
You have some unwanted </div> in your code which leads form to close in a wrong place, fix the <div> and it will start working again. Basically, this is an error due to the non-well-formatted code.
<div>
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('', '', ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
</div>
</div>
And don't forget to add name and other parameters in your code to make your form worthy.
{{Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Product'])}}
first the div tag is not proprly closed ..you have two unwanted tag in first div .
you can style according to your design need .
.
<div class="container">
<div class="well">
{!! Form::open(['action' => 'ProductController#store', 'class' => 'form-inline' , 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<fieldset>
<div class="form-group">
{!! Form::label('product', 'Product:', ['class' => 'col-lg-2 control-
label']) !!}
<div class="col-lg-10">
{!! Form::text('product', $value = null, ['class' => 'form-
control', 'placeholder' => 'product']) !!}
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
{!! Form::submit('Submit', ['class' => 'btn btn-info '] ) !!}
</div>
</div>
</fieldset>
{!! Form::close() !!}
</div>
</div>
you can use bootsrap html form for easy and better design since they have different form-class for different design .
https://www.w3schools.com/bootstrap/bootstrap_forms.asp
if u find this helpful
The form close tag should be at the same depth as the form open tag:
without them. Is there any way to stylise the form and use it ?
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div>
<div>
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('product', $value=null, ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
</div>
</div>
<div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
</div>
</div>
</div>
{!! Form::close() !!}

mismatch token laravel on add form

I have this form to create a new article. User inputs everything correctly but i get a return of TokenMismatchException in VerifyCsrfToken.php line 53:. I saw another post saying that because i used the open form Tag it will already add this for me and to take it out. How do i take this out? or is there some other problem that im overlooking?
here is my controller
public function store( AddArticleController $request)
{
$request= $request->all();
$request['user_id']=Auth::id();
Article::create($request->all());
return redirect('/user');
}
and here is the html
<form class="form-horizontal" role="form" method="POST" action="{{ url('/article/add') }}">
{!! Form::open(
array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
<div class = "form-group">
{!! Form::label('title','Title:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('title',null, ['class' => 'form-control']), !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('title','Image:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::file('image') !!}
</div>
</div>
<div class="form-group">
{!! Form::label('title','Type:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-lg-1">
{!! Form::select('type', array('select' => 'select','fashion' => 'Fashion', 'music' => 'Music', 'dance' => 'Dance', 'event' => 'Event'))!!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body','Comment:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::textarea('body',null, ['class' => 'form-control']) !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body',' ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form:: submit('Sumbit', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
</form>
First, which version of Laravel do you use?
The Laravel's HTML Helper has been removed in the version 5 of Laravel and is now not updated. If you still want to use this helper I recommend you to use the version updated by the community https://github.com/LaravelCollective/html.
For your problem, if you don't use the helper, you need to add the token manualy by adding {!! csrf_token() !!} in your form. You can find more information in the documentation: http://laravel.com/docs/5.1/routing#csrf-protection
You should get rid of either the <form> tag or from {{ Form::open() }} from your code.
The Laravel's form generator (which is depricated since Laravel 5) allows you to write that syntax {{ Form::open() }} which is rendered to plain html tag <form>.
so in your case the right way to go would be either
<form class="form form-horizontal" novalidate="novalidate" enctype="multipart/form-data" role="form" method="POST" action="{{ url('/article/add') }}">
{!! csrf_token() !!} // note that in this case we need to put csrf token manuall
......
</form>
OR
{!! Form::open(['action' => url('/article/add'), 'novalidate' => 'novalidate' 'files' => true, 'class' => 'form form-horizontal']) !!}
... //your fields and you don't have to explicitly include csrf token. Form::open() will do that for you
{!! Form::close() !!}

Patch update Authenticated User in Laravel

I'm trying to edit/update profile information into my Users table.
My idea is for the authenticated user to be able to edit his own profile in the Users table.
During the registration, you are only required to fill out a couple specific items (username, name, lastname, email, password) but I've also added a couple extra columns to the User table (city, country, phone, twitter, facebook).
I have a profile user page (route= '/profile') where all the information is shown. Of course all columns that aren't required during the registration are not filled in:
I also have an edit page where the columns that need info added are editable:
Here is the code for this editprofile.blade.php (where I try to send out a PATCH method):
...
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
<div class="form-group form-horizontal">
<div class="form-group">
{!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->username}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->email}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>
</div>
</div>
<br />
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('city', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('country', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('phone', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
...
I have this is my Http/routes.php:
# Profile
Route::get('/profile', 'PagesController#profile');
Route::get('/profile/edit', 'ProfileController#edit');
Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController#update');
I have an Http/Requests/UpdateUserRequest.php to validate the request:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends Request {
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'city' => 'max:30',
'country' => 'max:30',
'phone' => 'max:30',
'twitter' => 'max:30',
'facebook' => 'max:30'
];
}
}
And my Http/Controllers/ProfileController.php with the update fuction:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller {
public function edit()
{
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->fill($request->all());
$user->save();
return redirect()->view('pages.editprofile')->withUser($user);
}
}
At the moment it seems I can't even open my 'editprofile.blade.php' page anymore, unless I remove the 'route' & 'methode' from my form.
I keep getting this error:
Could anyone guide me on how I should exactly trigger the PATCH methode?
change your form opening tag to
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
you forgot '->id'
UPDATE
Change you Route
from
Route::patch('user/{user}', 'ProfileController#update');
to
Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController#update');
and your form opening tag to
{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}
You need to change:
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
into:
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
At the moment you create URL in your form with User object converted to Json - that's why it doesn't work.

Categories