display fetch data in dropdown menu with "where" value selected in laravel - laravel

I am trying to display the currencies in the dropdown menu but I need the data with a value of default=1 to be selected. upon searching, i found a sample and tried to applied it to my controller, here's what I came up,
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
it doesn't work. the error message said
Call to undefined method Illuminate\Database\Query\Builder::lists()
also I read a comment that the list() is already obsolete in laravel.
how can I achieve this?
here's from my create function in controller
public function create()
{
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
return view ('orders.create')->with('currencies', $currencies);
}
here's from create blade
{{ Form::select('currency_id', $currencies, Input::old('currency_id'),null, ['class' => 'form-control input-lg','required']) }}
thank you so much in advance!

Try using ->pluck(),
$currencies = \DB::table('currencies')->pluck('currency_name','id');
// In blade
{{ Form::select('currency_id', $currencies, null, ['class' => 'form-control input-lg','required']) }}
Read more about pluck here.

Related

laravel prefil form inputs from controller

Hi I'm using laravel forms.
{!! Form::text('product_name', null, array('class' => 'form-control date_pick')) !!}
Is there a way to set this default input from controller?. Think we can do it with Flash but I couldn't find an example. I want to take vlues from a model and prepopulate.
$products = Products::all();
It would be great if someone know how to do this. What is the easiest way to do it?
This is done with a basic edit route where you simply use:
In ModelController you would call the edit page:
public function edit(ModelName $model) {
return view('name.of.the.blade.view', compact('model')); // if using compact then without dollar symbol
}
In blade view simply make the form with all the input fields you have for that model:
{!! Form::model($model, ['method' => 'PATCH', 'route' => ['model.update', $model->id],]) !!}
Now all the form fields will have the values from that model.
{!! Form::close() !!}
And the edit and update route (inside routes/web.php) would be like this:
Route::get('/model/{model}/edit', 'ModelController#edit')->name('model.edit');
Route::patch('/model/{model}', 'ModelController#update')->name('model.update');

How to clear form input after save?

How can I clear form input on form submit?
After URL is saved I return the view for creating a form so I can add new URL.
The problem is that this time URL is populated from previous save.
How can I empty it after save?
Isn't that the point of second parameter which is set to null?
Using laravel version 5.4.36.
In the "create" iew I have a form with URL input:
{!! Form::open(['route' => 'urls.store']) !!}
{{ Form::url('url', null, array('class' => 'form-control')) }}
{{ Form::submit('Add domain', array('class' => 'btn' )) }}
Store method saves URL to database and returns the same view:
public function store(Request $request)
{
// URL Validation
$this->validate($request, array(
'url' => 'required|url|unique:urls'
));
// Save
$url = new Url;
$url->url = $request->url;
$url->save()
return view('urls.create');
}
Your form view should be handled by a get request on Route and submitting form is a post request .So after submitting the form you should redirect the same page not just returning the view . Check the web.php is the Routes are ok or not . Try to
add it to the store method ,I think it may help you
return redirect()->back();
or with flash message
return redirect('urls.create')->with('success', 'URL has been added');
you can make value = some values come from the controller with empty values when it's the parameters is set i.e use isset function
or
you can make a flag in the view when it sends to controller check it if it is true means you submit the form, in this case, make the values with empty

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

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 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