storing data with name of author - laravel 5.2 - laravel

I have hasMany relation to my model user and reports.
I want to set author name for the reports. (Like a blog-post author)
my model User:
public function reports() {
return $this->hasMany('App\Report', 'author_id');
}
model Report
public function user() {
return $this->belongsTo('App\User', 'author_id');
}
and my controller:
public function create()
{
$category = Category::lists('title','id');
return view('dash.reports.create')->with('category', $category);
}
/**
* Store a newly created resource in storage.
*
* #return void
*/
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
Report::create($request->all());
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}
I'm able to set in in phpmyadmin, but how can i set it with my controller?
edit: my view:
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('title', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-2">
{!! Form::selectRange('day', 1, 31, null, ['class' => 'form-control']) !!}
{!! $errors->first('day', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::selectMonth('month', null, ['class' => 'form-control']) !!}
{!! $errors->first('month', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null, ['class' => 'form-control']) !!}
{!! $errors->first('year', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
{!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
{!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
{!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}

Very easy. Replace Report::create... with this.
$user = Auth::user();
$report = new Report($request->all());
$report->author()->associate($user);
$report->save();
Make sure you use Auth; up at the top.
This uses the Auth object to get the current user,
Builds a new Report using the $request data without saving,
Tells the report we're associating $user as the author for the model,
Saves the report with the authorship information.

solution:
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
$user = Auth::user()->id;
$report = new Report($request->all());
$report->author_id = $user;
$report->save();
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}

Related

Why i have POST method if everywhere is GET method?

I want make search by parameters. But it shows i have mixing GET and POST methods. (Error message: MethodNotAllowedHttpException
No message). Blade form by default have POST. i changed to GET. Route have GET method. Maybe you can see what i am doing wrong. This is my VIEW:
{!! Form::open([ 'action' => ['HomePageController#index', 'method' => 'get']]) !!}
<div class="container">
<div class="col-xs-2 form-inline">
{!! Form::label('city_id', trans('quickadmin.companies.fields.city').'', ['class' => 'control-label']) !!}
{!! Form::select('city_id', $cities, old('city_id'), ['class' => 'form-control select2') !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('categories', trans('quickadmin.companies.fields.categories').'', ['class' => 'control-label']) !!}
{!! Form::select('categories', $categories, old('categories'), ['class' => 'form-control select2']) !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('search', trans('quickadmin.companies.fields.name').'', ['class' => 'control-label']) !!}
{!! Form::text('search', old('search'), ['class' => 'form-control', 'placeholder' => 'Search']) !!}
</div>
<div class="form-inline">
<div class="col-xs-2">
<button type="submit"
class="btn btn-primary">
Search
</button>
</div>
</div>
</div>
{!! Form::close() !!}
My controller:
public function index( Request $request)
{
$cities = \App\City::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$categories = \App\Category::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$name = $request->input('city_id');
$companies = \App\Company::All()->where('city_id', '=', $name);
return view('table', compact('companies', $companies, 'cities', $cities, 'categories', $categories));
My route:
Route::get('/', 'HomePageController#index');
Thank you for your help.
There is a problem in the form open, try it like this :
{!! Form::open([ 'action' => 'HomePageController#index', 'method' => 'get']) !!}

laravel how to use star rating plugin on a form text input

My project is on storing reviews on products. I am using Laravel framework here. The review has a field for rating the product. I am using the jquery 'starrr' plugin for the rating.
Form :
{!! Form::open(['url'=>'/admin/reviews', 'role' => 'form', 'class' => 'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('title', "Title:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::text('title', null, ['class'=>'form-control'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('product_id', "Product:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::select('product_id', $products, null, ['class'=>'form-control product_list'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('rating', "Rating:", ['class'=>'control-label col-md-2']) !!}
<div class="starrr"></div>
{!! Form::hidden('rating', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
<div class="col-md-3 col-md-offset-4">
{!! Form::button('<i class = "fa fa-plus-circle"></i> Add New Review', ['type' => 'submit', 'class'=>'btn btn-primary btn-lg']) !!}
</div>
</div>
{!! Form::close() !!}
controller store method:
public function store(Request $request){
$rules = [
'product_id' => 'required',
'title' => 'required',
'content' => 'required',
];
$this->validate($request, $rules);
create($request->all());
return redirect('admin/reviews')->withSuccess('Review is created');
}
My question is how to get the rating value from the star rating, into the request object to store on the database?
Thanks for your help in advance
$('.starrr').starrr({
change: function(e, value){
$('input[name="value"]').val(value)
}
})
This will set the input to the value of the starrr plugin whenever it is changed. Then the input value will be sent to the server

Form model binding laravel 5.1 for multiple models

I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.
Form:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
Controller:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
Routes:
Route::get('/admin/post/edit/{id}', 'Admin\PostController#edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController#update'
]);
It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.
I solved it. Mass Assignment. explains what to do if using update or create
So, the update method is:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}

Laravel 5 MethodNotAllowedHttpException issues

I am trying to make a simple CRUD for Clients. At the moment I can view clients and get to the edit page. However, if I try to update or create a client, I get a MethodNotAllowedHttpException.
My routes look like the following
Route::model('clients', 'Client');
Route::bind('clients', function($value, $route) {
return App\Client::whereSlug($value)->first();
});
Route::resource('clients', 'ClientsController');
My controller is like so
<?php
namespace App\Http\Controllers;
use App\Client;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Redirect;
use Illuminate\Http\Request;
class ClientsController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$clients = Client::all();
return view('clients.index', compact('clients'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('clients.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
$input = Input::all();
Client::create( $input );
return Redirect::route('clients.index')->with('message', 'Client created');
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Client $client
* #return Response
*/
public function edit(Client $client)
{
return view('clients.edit', compact('client'));
}
/**
* Update the specified resource in storage.
*
* #param \App\Client $client
* #return Response
*/
public function update(Client $client)
{
$input = array_except(Input::all(), '_method');
$client->update($input);
return Redirect::route('clients.index', $client->slug)->with('message', 'Client updated.');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Client $client
* #return Response
*/
public function destroy(Client $client)
{
$client->delete();
return Redirect::route('clients.index')->with('message', 'Client deleted.');
}
}
I then have a form partial like so
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('clientName', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('slug', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit($submit_text, ['class'=>'btn btn-default']) !!}
</div>
And my edit.blade.php is like so
<h2>Edit Client</h2>
{!! Form::model($client, ['class'=>'form-horizontal'], ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
#include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}
I have researched this error and a lot of people refer to javascript, but I am not using any javascript at the moment.
Why would I be getting this error? As I say, I get it when I try to create a client as well.
Thanks
As an update, if I remove the form partial and add this to ed.it.blade.php instead, it seems to work
{!! Form::model($client, [
'method' => 'PATCH',
'route' => ['clients.update', $client->slug]
]) !!}
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('clientName', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('slug', null, array('class' => 'form-control')) !!}
</div>
</div>
{!! Form::submit('Update Client', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Why is that?

Resources