Laravel 5 form model binding with hasMany - php

I have two models: Category and CategoryValue.
Inside my Category model I have:
public function values()
{
return $this->hasMany('App\CategoryValue');
}
Inside my CategoryValue I have:
function category()
{
return $this->belongsTo('App\Category');
}
In my edit view, I have my form model binding like so:
{!! Form::model($category, ['route' => ['category.update', $category->slug], 'method' => 'PATCH']) !!}
#include('partials.categoryForm')
{!! Form::close() !!}
Where $category is a chosen Category Model.
The categoryForm partial looks like:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
{!! Form::label('slug', 'Slug') !!}
{!! Form::text('slug', null, ['class' => 'form-control']) !!}
{!! $errors->first('slug', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group">
{!! Form::submit('Save Page', ['class' => 'btn btn-primary']) !!}
</div>
That's all working, but when editing the Category, I'd like to be able to edit (add/remove) associate CategoryValues. To start, I want the items returned from $category->values to be inside a textarea with each CategoryValue on a new line.
I can think of a couple of ways to do this: use a #foreach and generate the textarea, or popular a variable and use that in the binding. I'm not sure which of these, if either, is the "Laravel" way to do it. I'm also not sure if either of those can be used to update the records in the DB properly. How should this be done?

Related

Update the user

I want to update the user using this Form:
{!! Form::model($user ,['method'=>'PATCH', 'action'=>['AdminController#update',$user->id], 'files'=>true]) !!}
<div class="form-group">
{!! Form::label('first_name','First Name:') !!}
{!! Form::text('first_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name','Last Name:') !!}
{!! Form::text('last_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('username','Username (Student ID):') !!}
{!! Form::number('username',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email','Email:') !!}
{!! Form::email('email',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('address','Address:') !!}
{!! Form::text('address','null',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone_number','Phone Number:') !!}
{!! Form::number('phone_number','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::submit('Update User', ['class'=>'btn btn-primary col-sm-6']) !!}
</div>
{!! Form::close() !!}
and this is my Route:
Route::prefix('admin')->group(function(){
Route::post('/users/{id}','AdminController#update');
)};
when I press update button I receive this error:
Please help me, I am Using Laravel 5.4
try following
{{ Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT')) }}
// form fiedls come here
{!! Form::close() !!}
in route file add following route:
Route::resource('users', "UserController");
and in user controller add following code:
public function update(Request $request, $id){
// Add form validation here
$user = User::find($id);
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->save();
Session::Flash('success', 'The User have been updated successfully!');
return redirect()->route('users.index');
}
don't user AdminController to manage users.
Please use PATCH in routes for example :
Route::patch('/users/{id}','AdminController#update');
your method is post , but you send patch request
you should change to this
'method'=>'post'
you can use this code to fix every thing
form blade
{{ Form::model($user, array('route' => array('update-user', $user->id), 'method' => 'PUT')) }}
route
Route::prefix('admin')->group(function(){
Route::PUT('/users/{id}','AdminController#update')->name('update-user');
)};

Form model binding isn't working inside section

Parent blade template:
#extends('layouts.master')
#section('content')
<div class="panel-body">
#if(count($errors))
#include('common.errors')
#endif
{!! Form::model($entry, ['method' => $method, 'class' => 'form-horizontal', 'route' => [$route]]) !!}
#section('fields')
#foreach (['title', 'url', 'content', 'meta_tags', 'meta_description', 'is_active'] as $field)
#include('entries.fields.' . $field)
#endforeach
#show
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
#section('submit')
{!! Form::submit('Добавить', ['class' => 'btn btn-default']) !!}
#show
</div>
</div>
{!! Form::close() !!}
</div>
#endsection
Child template:
#extends('entries.create')
#section('title')
Добавить статью / {{ config('site.site_name') }}
#endsection
#section('fields')
#foreach ([
'entries.fields.title',
'entries.fields.url',
'articles.fields.description',
'entries.fields.content',
'entries.fields.meta_description',
'entries.fields.is_active',
'articles.fields.enable_comments',
'articles.fields.show_on_main',
'articles.fields.rank',
] as $field)
#include($field)
#endforeach
#endsection
Model binding works for parent template, but doesn't work in child template.
For example, I have enable_comments checkbox:
{!! Form::label('enable_comments', 'Включить комментарии', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::checkbox('enable_comments', null, null, ['class' => 'checkbox']) !!}
</div>
</div>
And it is always unchecked, while the $entry->enable_comments === true
I don't know why this is happening. Maybe anyone can go through the code and find the problem.
Thanks in advance for your help.
The problem is that form model binding works only inside the current template (with included files), but doesn't fill the values in child temlates. So the only solution is to write form start ( {!! Form::model(... )again, in the child template

Issue with Form model binding in Laravel 5.4

This usually doesn't occur when I'm form model binding in Laravel, but for some reason, every time I pull up my binded form, I'm getting the exact same record.
{!! Form::model($contact, ['method'=>'PUT', 'route'=>['contact.update', $contact->id]]) !!}
{!! Form::label('firstname', 'First Name:') !!}
{!! Form::text('firstname', null, ['class'=>'form-control']) !!}
{!! Form::label('lastname', 'Last Name:') !!}
{!! Form::text('lastname', null, ['class'=>'form-control']) !!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class'=>'form-control']) !!}
{!! Form::label('address', 'Address:') !!}
{!! Form::text('address', null, ['class'=>'form-control']) !!}
{!! Form::label('phone_number', 'Phone:') !!}
{!! Form::text('phone_number', null, ['class'=>'form-control']) !!}
{!! Form::submit('Update Contact', ['class'=>'btn btn-primary']) !!}
{!! Form::button('Close', ['class'=>'btn btn-default', 'data-dismiss'=>'modal']) !!}
{!! Form::close() !!}
Controller:
public function index()
{
$user = Auth::user();
$contacts = $user->contacts()->get();
return view('contacts.index', compact('contacts','user'));
}
When I click this button, that form is popping up as a modal
<button class="btn btn-default editContact" data-toggle="modal" data-target="#editModal">Edit Contact</button>
I've used this same format before and it usually gives me different records with each click. But for some reason every record I click on to display the update form, it registers with the same record every time. Any ideas on how to fix this?
I think you can try this :
public function index()
{
$user = Auth::user();
$contacts = Contacts::where('user_id',$user->id)->first();
return view('contacts.index', compact('contacts','user'));
}
Hope this help for you !!!
In addition to what has been said,
I am under the impression that the $contact in:
{!!
Form::model($contact, [
'method'=>'PUT',
'route'=>['contact.update', $contact->id]
])
!!}
Should be $contacts.

Laravel MethodNotAllowedHttpException is being thrown

I am watching a Laracast on forms, and my code is quite basic, and yet so is my knowledge of the framework. My code is below, and what your looking at, if you haven't taken a guess, is that I wish to create a list of values to be stored in a database, and to "Redirect" the user to there newly stored values. The "store" part seems to be the area of issue. Whenever I click "add food items" I am not redirected, yet this error appears, MethodNotAllowedHttpException.
Route::get('food/create', 'FoodController#create');
Route::post('fond/post', 'FoodController#store');
Route::get('food/{id}', 'FoodController#index');
namespace App\Http\Controllers;
use App\Food;
use Illuminate\Http\Request;
class FoodController extends Controller
{
public function index($id)
{
$food = Food::find($id);
return view('index')->with('food', $food);
}
public function create()
{
return view('vendor.create');
}
public function store(Request $request)
{
$input = Request::all();
$food = Food::create($input);
return redirect('food/'.$food->id);
}
}
#extends('app')
#section('body')
<h1>Foods!</h1>
<h2>{{ $food->vegetables }}</h2>
<h2>{{ $food->fruit }}</h2>
<h2>{{ $food->grains }}</h2>
<h2>{{ $food->meat }}</h2>
<h2>{{ $food->sugar }}</h2>
#stop('body')
#extends('app')
#section('body')
{!! Form::open(['url'=>'food/store']) !!}
<div class="form-group">
{!! Form::label('vegetables', 'Vegetable item:') !!}
{!! Form::text('vegetables', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('fruit', 'Fruit item:') !!}
{!! Form::text('fruit', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('meat', 'Meat item:') !!}
{!! Form::text('meat', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('grains', 'Grain item:') !!}
{!! Form::text('grains', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('sugar', 'Sugar item:') !!}
{!! Form::text('sugar', null, ['class' => 'form-control']) !!}
</div>
<div>
{!! Form::submit('Add Food items', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
</div>
#stop('body')
Add the get method in you form opening tag. It should look like this:
{!! Form::open(['url'=>'food/store','method'=>'get']) !!}

display checked values on another page in laravel

I am new at Laravel. I am building a code that displays the records that have been checked.
Following is my code.
{!! Form::open(array('action' => 'ClientsController#store','method' => 'POST', 'name' => 'f1'))!!}
#foreach($clients as $client)
{!! Form::checkbox('agree', $client->email, null, ['class' =>'questionCheckBox']) !!}
<article>
{{ $client->user_name }}
{{ $client->email }}
</article>
#endforeach
{!! Form::close() !!}
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
<div class="form-group">
{!! Form::submit('show-selected ',['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
I have to display $client->email when I click button "show-selected".
My controller method is,
public function display() {
$client = Client::all();
dd(Input::has('agree'));
$client = Input::has('agree') ? true : false;
return $client->email;
}
This should return true, but it is returning false, I am not getting how to get the values of checked check boxes on another page.
This Worked.
My blade file is:
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_1', null) !!} <p>email_1</p>
</div>
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_2', null) !!} <p>email_2</p>
</div>
<div class="form-group">
{!! Form::checkbox("agree[]", 'email_3', null) !!} <p>email_3</p>
</div>
<div class="form-group">
{!! Form::submit('Show-Selected',['class' => 'btn']) !!}
</div>
{!! Form::close() !!}
And the display method is:
public function display(){
$data = Input::get('agree');
$count = count ($data);
echo "Selected email is/are:" . $count;
echo "<br/>";
foreach ($data as $big_name){
echo $big_name;
echo "<br/>";
}
}
After clicking button "Show-selected",the value of checked check-boxes will display.

Categories