How to store multi select values in Laravel - php

I have many to many relationship between categories and movies. When I check multiple values for categories and insert movie the result on home page selects only one category not all. I tried many things but I wasn't able to resolve this. Here is my code.
upload.blade.php:
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>
Controller:
public function index(Request $request)
{
$categories = Category::pluck('category_name', 'id')->all();
return view('movies.upload', compact('categories'));
}
public function upload(MovieUploadRequest $request)
{
DB::beginTransaction();
try {
$movie = Movie::create($request->all());
$movie->categories()->attach($request->get('category_id'));
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
return redirect('home');
}

To enable multiple selects, you first of need to change the name of the select input from category_id to category_id[]. This enables posting of multiple variables as an array.
The code should then look like:
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id[]', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>
In your controller, you will then need to loop through the posted id's:
public function upload(...){
foreach( $request->input('category_id') AS $category_id ){
$movie = Movie::create($request->all());
$movie->categories()->attach($category_id);
...
}
}
Edit:
The method attach() also accepts an array of ids as an argument. Thereby, you don't need to loop through the input as above, but can simply use:
public function upload(...){
$movie = Movie::create($request->all());
$movie->categories()->attach($request->input('category_id'));
...
}

In your view, use a name like :
<select name="yourfiled[]"></select>.
In controller, to store it in database, just write
$movies->categories = json_encode(yourfield);
Done.

You can write this. hopefully this will solve your problem
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id[]', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>

Related

How to display the last inserted id in db using Laravel after creating

Patient Controller:
public function store(Request $request)
{
$input = request()->all();
Patient::create($input);
dd($input->id);
// return redirect()->route('medical.create',compact('input'));
}
This is my medical.create view
{!! Form::model($input, [
'method' => 'POST',
'action' => ['MedicalController#store', $input->id]
]) !!}
<div class="row">
<div class="col-md-4">
<div class="form-group">
{{Form::label('patient_id','Patient no:')}}
{{Form::text('patient_id', null, array('class' => 'form-control') )}}
</div>
</div>
</div>
{!! Form::close() !!}
I want to get my last inserted id after storing, and display the last id in the next form, but this is the error appear in my screen:
Trying to get property 'id' of non-object
This is my Patient table:
You can do that by saving the Patient object in a variable when creating it:
public function store(Request $request)
{
$input = request()->all();
$patient = Patient::create($input); // Save it in variable
dd($patient->id); //Now you can access patient id here
// return redirect()->route('medical.create',compact('patient')); //Also you can pass it to your view
}
you can use id like Code below with least Changes in your code
$input = request()->all();
$input = Patient::create($input);
dd($input->id);
// return redirect()->route('medical.create',compact('input'));
you can retrive id after saving data by mass assignments :
public function store(Request $request)
{
$input = request()->all();
$patient = new Patient($input); // fill model with mass assignments
$patient->save() // save instant
$id = $patient->id; //retrive id
}
You can't use compact while redirecting. Try this:
Patient Controller:
public function store(Request $request)
{
$input = request()->all();
$patient = Patient::create($input);
$patient = DB::table('patients')->get()->last();
return redirect()->route('medical.create')->with('patient_id', $patient->id);
}
This is my medical.create view
{!! Form::model($input, [
'method' => 'POST',
'action' => ['MedicalController#store', session('patient_id')]
]) !!}
<div class="row">
<div class="col-md-4">
<div class="form-group">
{{Form::label('patient_id','Patient no:')}}
{{Form::text('patient_id', null, array('class' => 'form-control') )}}
</div>
</div>
</div>
{!! Form::close() !!}

Getting MethodNotAllowedHttpException error in Laravel

I'm learning Laravel. I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc).
This form sends the data to a "orders" table. With the code that I have now, I'm not able to post the order on the order table. it gets the message: "MethodNotAllowedHttpException in RouteCollection.php line 233:"
This is the code
Web.php
Route::get('catalog', 'carController#catalog');
Route::get('orders/', 'CarController#orders')->name('orders');
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders(Request $request) {
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
#foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
{{ $car->Model }}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
#endforeach
The table orders has following structure:
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('Model');
$table->date('Fabrication_date');
$table->integer('Colour_id')->unsigned();
$table->foreign('Colour_id')->references('id')->on('colours')->onDelete('cascade')->onUpdate('cascade');
$table->integer('Order_status_id')->unsigned();
$table->foreign('Order_status_id')->references('id')->on('order_status')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
Update your orders function
public function orders(Request $request)
{
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
Catalog.blade.php
Form::hidden('modelesc', $car->Model)
The value of your hidden input will be read by request->modelesc and you will endup with a URL that looks like this http://mysite.dev/orders?modelesc=toyota.
Some advice
You using a form to only submit a hidden input. No user input. It seems to me it would be a easier to use simple anchors <a></a>.
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
Same result.
Make it nice with pretty URLs. Change your route definition
Route::get('orders/{modelesc}', 'CarController#orders')->name('orders');
And the anchor
{{ $car->Model }}
With named route
{{ $car->Model }}
And the function
public function orders($modelesc)
{
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
change your route to:
Route::get('orders/{model}', 'CarController#orders')->name('orders');
after this change your Controller to:
public function orders($model) {
$cars = DB::table('cars')->where('Model', $model)->get();
if(!count($cars)){
abort(404);
}
$colours = DB::table('colours')->get()->pluck('Colour');
//$status = DB::select('select * from order_status where id = ?', [1])->get(); //this variable is never used
return view('orders', compact('cars', 'colours'));
}
and now change your Catalog.blade.php
in your case you don't need a form for go in your order page
#foreach($cars as $car)
{{ $car->Model }}
#endforeach

How to customize a field of the model before storing in database

I have a model like this :
class Article extends Model {
protected $fillable = [
'title',
'body',
'img_profile', // store name of image
];
}
And a controller like this:
public function store(ArticleRequest $request){
$article = Auth::user()->articles()->create($request->all());
return redirect('articles');
}
And a form like this:
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
When I submit the form, the img_profile field is stored default cached file which is /private/var/tmp/phpceBMP2.But I just want to update file_name in img_profile.
How can I change the value of the img_profile request before storing it in database?
You should use mutator for Article model this way:
public function setImgProfileAttribute($value)
{
$this->attributes['img_profile'] = '/private/var/tmp/phpceBMP2/'. $value;
}
However you should think if you really want to store the path in DB this way. What if you will want to change it in future? You will have to update all the records?

Laravel Form-Model Binding multi select default values

I'm trying to bind a default value to a select tag. (in a "edit view").
I know this should be easy, but I think I'm missing something.
I have:
User.php (my user model)
...
public function groups()
{
return $this->belongsToMany('App\Group');
}
public function getGroupListAttribute()
{
return $this->groups->lists('id');
}
...
UserController.php (my controller)
...
public function edit(User $user)
{
$groups = Group::lists('name', 'id');
return view('users.admin.edit', compact('user', 'groups'));
}
...
edit.blade.php (the view)
...
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['UserController#update', $user->id]]) !!}
...
...
// the form should be binded by the attribute 'group_list' created
// at the second block of 'User.php'
// performing a $user->group_list gets me the correct values
{!! Form::select('group_list[]', $groups, null, [
'class' => 'form-control',
'id' => 'grouplist',
'multiple' => true
]) !!}
...
I did a dummy test in my blade, and have gotten the correct results:
#foreach ($user->group_list as $item)
{{ $item }}
#endforeach
This lists the values that should be selected by default..
I also tried to put $user->group_list as third parameter from the Form::select, but this didnt work ether...
I have no clue what i'm doing wrong.. any hints on this one?
edit
when I do:
public function getGroupListAttribute()
{
//return $this->groups->lists('id');
return [1,5];
}
The item are correctly selected,
now i know i have to grab an array from the collection..
digging deeper.. :)
found it
User.php:
...
public function getGroupListAttribute()
{
return $this->groups->lists('id')->toArray();
}
...
could it be easier?
Nice regards,
Kristof
You shouldn't put null in the selected defaults (3rd) argument.
{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
{!! Form::select(
'group_list[]',
$groups,
$user->group_list,
['multiple' => true]
)
!!}

Laravel - Resource route passing in wrong parametre (not the id)

I have an issue with my the update method in my controller.
Normally everything works and it takes care of itself, but this time its not working.
My controller:
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, Vehicle::$rules, Vehicle::$messages);
if ($validation->passes())
{
$this->vehicle->update($id, $input);
return Redirect::route('admin.vehicles.index')->with('success', 'Car Updated');
}
return Redirect::back()
->withInput()
->withErrors($validation);
}
Repository:
public function update($id, $input)
{
print_r($id);
die();
}
This prints out:
{vehicle}
from the URI:
http://localhost/admin/vehicles/1/edit
My form:
{{ Form::open(['route' => 'admin.vehicles.update', 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}
// inputs
<div class="form-group">
{{ Form::submit('Update Vehicle', ['class' => 'btn btn-success']) }}
</div>
{{ Form::close() }}
Route:
Route::resource('/admin/vehicles', 'VehiclesController');
Where is this going wrong? How can I get this form to send the ID not the word vehicle?

Categories