So, i have some categories. And in each category you can add posts.
But in form page for adding posts, how to get the value of that category i.e. of previous page?
This is my form:
<div class="container">
{!! Form::open(['action' => 'TopicController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title of the Post']) }}
</div>
<div class="form-group">
{{ Form::label('desc', 'Desc') }}
{{ Form::textarea('desc', '', ['class' => 'form-control', 'placeholder' => 'Description of the Post']) }}
</div>
{{ Form::submit('Submit', ['class' => 'btn btn-default']) }}
{!! Form::close() !!}
</div>
Link in category page to form:
Create New Post
Controller:
$this->validate($request, [
'title' => 'required',
'desc' => 'required',
])
$topic = new Topic;
$topic->topic_title = $request->input('title');
$topic->topic_body = $request->input('desc');
$topic->user_id = auth()->user()->id;
$topic->save();
return redirect('/')->with('Seccess', 'Topic Created');
show.blade.php contains link to this form page. But to get the id of the category page that is referring to this form??
You need to pass category_id into your link as route parameter:
Create New Post
Catch category_id in /topics/create/category_id route:
Route::post('/topics/create/{category}', 'TopicsController#create');
And then use it to create a hidden field in your form:
<div class="container">
{!! Form::open(['action' => 'TopicController#store', 'method' => 'POST']) !!}
{{ Form::hidden('category_id', $category_id) }}
...
</div>
And then in your controller:
...
$topic->category_id = $request->input('category_id');
$topic->user_id = auth()->user()->id;
$topic->save();
...
Related
Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}
I have created a form to change the user name and its role in the website.
#extends('layout/layoutAdmin')
#section('content')
<div>
<h1>{{ $user -> name }}<h1>
<p>{{ $user -> email }}<p>
</div>
{!! Form::model($user, ['url' => 'admin/menu/user_profiles/' . $user->id, 'method' => 'PATCH']) !!}
<div class="row">
<div class ="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', $user->name,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('role', 'Role:') !!}
{!! Form::select('role', array('admin' => 'admin', 'super_admin' => 'super admin',
'super_researcher' => 'super researcher', 'researcher' => 'researcher',
'consultant' => 'consultant', 'user' => 'user'), $user->role)
!!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
#stop
Everything is correct with the controller and I can change the name just fine, but I cannot save a new value for the role. it always stays the same.
Could anyone tell me how to save the role value?
EDIT!!!!
I did not have role field as fillable!
y have this Controller with GET vars:
localhost/ordersys/public/admin/orders?provid=220001&price=500
{!! Form::open(array('action' => array('Admin\OrdersController#filter'), 'role'=>'search', 'method' => 'GET')) !!}
{!! Form::text('provid', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'Search by Provid here...')) !!}
{!! Form::text('price', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'Price max ...')) !!}
{!! Form::submit('Search', array('class' => 'btn btn-default search-bar-btn')) !!}
{!! Form::close() !!}
$varprovid = Input::get('provid');
$varprice = Input::get('price');
$collection = DB::table('orders')
->where('cod_prov', $varprovid)
->where('price', '<', $varprice)
->paginate(15);
This work, but how can catch the Inputs Input::get('provid'), Input::get('price') from Form and filter Collection using Where clauses dynamically. I can build array and use foreach loop? Any idea please, thanks.
So my create news form is very simple:
<div class="row padding-10">
{!! Form::open(array('class' => 'form-horizontal margin-top-10')) !!}
<div class="form-group">
{!! Form::label('title', 'Title', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('body', 'Body', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-offset-5 col-md-3">
{!! Form::submit('Submit News', ['class' => 'btn btn-primary form-control', 'onclick' => 'this.disabled=true;this.value="Sending, please wait...";this.form.submit();']) !!}
</div>
{!! Form::close() !!}
This is processed by NewsProvider:
public function store()
{
$validator = Validator::make($data = Input::all(), array(
'title' => 'required|min:8',
'body' => 'required|min:8',
));
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
News::create($data);
return Redirect::to('/news');
}
But i have another field, not only title and text body in database, which is author_id and I have no idea how to add info, like the user id from currently authenticated user which wasnt supplied by form. I know how to add hidden input to form with user id, but then someone could change hidden field value. How do I do that correct way?
Maybe I have to edit my news eloquent model in some way, which is:
use Illuminate\Database\Eloquent\Model as Eloquent;
class News extends Eloquent {
// Add your validation rules here
public static $rules = [
'title' => 'required|min:8',
'body' => 'required|min:8',
];
// Don't forget to fill this array
protected $fillable = array('title', 'body');
}
You can always get the current authenticated user by Auth::user(). And you can also modify the $data array before passing it to create. Here's how you do it:
$data['author_id'] = Auth::user()->id;
News::create($data);
Also don't forget to add author_id to the fillable attributes
protected $fillable = array('title', 'body', 'author_id);
I'm not sure why it's not working...
I'm trying to update a user and I keep getting method not allowed error exception.
-- routes
Route::get('superadmin/users', ['as' => 'superadmin.users', 'uses' => 'SuperAdminController#usersIndex']);
Route::post('superadmin/users/{id}', ['as' => 'superadmin.editUser', 'uses' => 'SuperAdminController#editUser']);
-- controller
public function usersIndex()
{
$users = User::all();
return View::make('superadmin.users',compact('users'));
}
public function editUser($id)
{
$user = User::findOrFail($id);
$user->email = Input::get('email');
$user->save();
return Redirect::route('superadmin.users')->with('alertsuccess', 'User has been updated.');
}
-- view
{{ Form::model($user, ['method' => 'PATCH', 'route' => ['superadmin.editUser', $user->id], 'class' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'Email:', ['class' => 'placeholder-hidden']) }}
{{ Form::text('email', Input::old('email'), ['class' => 'form-control']) }}
</div>
{{ Form::submit('Update User', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}
This is most probably as you need to set up a resource controller in order to use the PATCH method. Try using POST instead.