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.
Related
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');
)};
i have this view and when i request it it shows me a strange error, well strange for me i guess.
my create.blade.php
#extends('layouts.app')
#section('content')
<div class="col-md-6">
{!! Form::open([action('PropertiesController#update', $property->id), 'files'=>true]) !!}
<div class="form-group">
{!! From::label('category', 'category') !!}
{!! Form::select('category', {{ $categories }},['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('city', 'located city') !!}
{!! Form::select('city', {{ $citys }},['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('street', 'Street adress: ') !!}
{!! Form::text('street', 'Adress', ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::file('images') !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Add description') !!}
{!! Form::textarea('description', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Done', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
the error is:
ErrorException in 83c90d4b665ecd8385b3b6b2e7203d1ef06a1339.php line 8:
Parse error: syntax error, unexpected '<' (View:
/home/vagrant/Code/housing/resources/views/properties/create_property.blade.php)
please help am stuck :(
You are trying to use blade syntax within blade syntax, which you don't need to do.
{!! Form::select('category', {{ $categories }},['class'=>'form-control']) !!}
It can simply be
{!! Form::select('category', $categories, ['class'=>'form-control']) !!}
You've done it in a couple locations, so make sure to fix them all up. You'll notice the error mentions line 8, which is the line immediately after this error occurs.
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']) !!}
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.
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?