Laravel use all Entries from DB in Select Box - laravel

YOu can create a select Box like this:
{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
Lets say I want to have a select box of all my users (saved in the DB). How can I do that? I've got a User-Model.

In your controller
use App\Entities\User;
public function getUsers(User $user)
{
$allUsers = $user->lists('name', 'id');
return view('users.view',compact('allUsers'));
}
In your blade
{!! Form::select('users[]', $allUsers,null , ['multiple'=>'multiple', 'class' => 'form-control']) !!}

Related

Laravel 5.5 - Missing required parameters for [Route:]

What I'm trying to achieve is very simple, but I'm just making this a lot harder than it needs to be. So am seeking help for what is apparently, my lack of Laravel experience.
All I want to do is have a form that can update database entries via a text input. This has to be dynamic as it's being used for a few databases and I don't want to have multiple files for them.
Sorry in advance for the probable messy/crap code...
Here's the routes I have:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')->name('greeting.update');
The PlayerProfileController
// Display greeting message
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
And finally the form
{{ Form::open(['action' => ['PlayerProfileController#updateGreeting', $greetInfo->greeting], 'method' => 'POST']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::hidden('_method', 'PUT') }}
<br>
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Any and all help is appreciated. Thank you in advace.
The ErrorException you're seeing is caused by the missing argument for the route you're trying to call. Your route is expecting server and playerId, wheres you're only sending it $greetInfo->greeting.
As you're using named route, my suggestion would be to use route() helper like so:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')
->name('greeting.update');
class PlayerProfileController extends Controller {
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
}
{{ Form::open(['action' => route('greeting.update', [$server, $playerId]), 'method' => 'post']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Please replace $server and $playerId variables with the ones representing the arguments you want to send with the request.
You can see I've also removed {{ Form::hidden('_method', 'PUT') }}, as this was trying to overwrite the post method on the Form::open method.

Missing required parameters for in a Update Form Laravel 5.2

I've been working on a webapp recently in laravel and i wanted to have a eddit function within tthe application. but im getting this error Missing required parameters for [Route: producten.update] [URI: producten/{producten}], and i dont know what i've done wrong.
This is the Routes im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);
This is the controller function im using for showing the edit page and updating.
The Edit function
public function edit(Request $request, Product $product)
{
// $product = Product::FindorFail($id);
// Product is a table with all products, with sellprice and buy price
// fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
The Update Function:
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
and this is the view i use for it.
{{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController#update', $model ]) }}
{{ Form::label('naam:')}}
{{ Form::text('naam') }} <br>
{{ Form::label('inkoopPrijs:')}}
{{ Form::text('inkoopPrijs') }} <br>
{{ Form::label('verkoopPrijs:') }}
{{ Form::text('verkoopPrijs') }} <br>
{{Form::label('Fabrieken', 'Fabrieken Id:') }}
{{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
{{ Form::submit('edit')}}
{{ Form::close() }}
if there is anything else that i need to add to the question just let me know and i'll add it
Missing thing is the id you are not getting id there in your edit function
your edit function should as i am assuming that you are just showing the form from this method where user can edit
public function edit($id)
{
$product = Product::FindorFail($id);
//Product is a table with all products, with sellprice and buy price
//fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
your update method should seem like this
public function update(Request $request, $id)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
your routes should like this no need for only
Route::resource('/producten', 'productionController');
edit route will be as
<a href="{{ route('production.edit', $model->id) }}">
Try this hope it will help

Laravel 5.2 form model binding - displaying model with() empty relationship

SOLVED, something else was causing the error, not an empty relationship.
I am having some trouble making this work. I have a large form that is combining 4 tables. For my example I will just use 3. Here is what I send to the view:
$student = Student::with('primaryInsurance')->with('secondaryInsurance')->findOrFail($student_id);
The form works fine if the student has both primaryInsurance and secondaryInsurance but I get a "Trying to get property of non-object" if one or both are not in the table. How can I avoid this?
Here are a couple fields from my form:
{{ Form::text('last_name', null, ['class' => 'form-control required']) }}
{{ Form::text('primaryInsurance[insured_name]', null, ['class' => 'form-control']) }}
{{ Form::text('secondaryInsurance[insured_name]', null, ['class' => 'form-control']) }}
From the student model:
public function primaryInsurance() {
return $this->hasOne(StudentInsurance::class, 'student_id', 'student_id')->where('is_primary', '=', 1);
}
public function SecondaryInsurance() {
return $this->hasOne(StudentInsurance::class, 'student_id', 'student_id')->where('is_primary', '=', 1);
}
First, combine your with query to make your code cleaner. Like so:
$student = Student::with('primaryInsurance', 'secondaryInsurance')->findOrFail($student_id);
Next, in your view check that the object is set (I think you should be using an object here versus array - that's what's causing the error I believe). You'll have to test. Your issue can also be that you're wrapping the object in ' '. Don't. I'd need to see your controller logic to get a better sense if this doesn't work.
{{ Form::text($primaryInsurance->insured_name, null, ['class' => 'form-control']) }}
You can also do a ternary on this as well to default to null if doesn't exist:
{{ Form::text((($primaryInsurance->insured_name) ?: null), null, ['class' => 'form-control']) }}

Laravel 5 Relationship Not Working?

In my app I have few models: User and Profile. The User model is only for companies, my app is for companies only. When a user registers, they only fill in their name, email address and password. My Profile model has columns for company name, address etc. My profile form does not work; not saving to the database. Here is the setup:
Controller for the form:
public function update($company_name)
{
$user = User::whereCompanyName($company_name)->firstOrFail();
$user->fill(Input::all());
$user->save();
flash('You have successfully edited your profile');
return redirect('/');
}
User.php:
public function profile()
{
return $this->hasOne('Profile');
}
Profile.php:
protected $fillable = ['company_name', 'company_logo', 'company_founded'];
public function user()
{
return $this->belongsTo('App\User', 'user_id','ID');
}
The Form:
{!! Form::model($user, array('method' => 'PATCH', 'route' => array('profile.update', $user->company_name), 'files' => true)) !!}
{!! Form::hidden('user_id', Auth::user()->id) !!}
// more fields
<div class="form-group">
{!! Form::label('company_name', 'Company Name') !!}
{!! Form::text('company_name', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Have I set the relationship correct? Nothing is saving to the database.
You’re updating the user model and the user model only. You need to also set the attributes in the profile relation:
$user->update(Input::all());
$user->profile->update(Input::all());
Your controller action could also be tidied up a bit, by using route–model binding to inject your User model instance, and also use the service container to provide a Request instance too so you’re not using the Input façade:
public function update(User $user, Request $request)
{
$user->update($request->all());
$user->profile->update($request->all());
flash('You have successfully updated your profile.');
return redirect('/');
}
I want to comment but do not have enough reputation :( A few days ago I found a little problem with this approach:
$user->update(Input::all());
$user->profile->update(Input::all());
In this case the mutators in related model (profile in the example) like this are not invoked (may be a bug):
public function setLoremIpsumAttribute($attr)
{
# code
}
In controller I tried another approach and it worked:
$user->update($request->all());
$user->profile->fill($request->all()['profile'])->push();
In Laravel 5 when you want to chain with relation, you need for exemple (Post with comment related) use the method from your comment.
Post::find(id)->comment()->where(your where statement)
Docs from Laravel:
If you need to add further constraints to which comments are retrieved, you may call the comments method and continue chaining conditions:
$comments = Post::find(1)->comments()->where('title', '=', 'foo')->first();

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Resources