I need to update specific one id / row on a separate field on another table. how is the logic to that? so far i have this pseudo code sql query.
table name: settings
current table: aircrafts
UPDATE settings SET description id = 4
i need to update id 4 on my database in database settings
how can i integrate this code into laravel syntax?
assuming i have this controller
public function editAirReg(Request $request, $id)
{
$descr = $request->input('descr_id');
DB::update('update settings set description = ,'$descr', where id = 4',[$name,$id]);
return redirect('/admin/aircrafts')->with('success', 'Settings Updated');
}
and also this view
{!! Form::open(['action' => 'Admin\AircraftsController#getdata', 'method' => 'POST']) !!}
<input type="hidden" class="form-control" name="settings" value="{{$aircraft->air_line}}">
{{Form::submit('BIND', ['class'=>'btn btn-primary btn-block btn-lg', 'name'=>'submit'])}}
{!! Form::close() !!}
First of all, getData() doesn't make sense if you're updating something in mysql. It should be called store() or save or whatever you want to call so that the function name is appropriate.
Let's change the form so that it can pass the id to the controller.
{ Form::open(array('action' => 'Admin\AircraftsController#getdata', $id)) }}
{{ Form::hidden('settings', $aircraft->air_line) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
What I did is that change your input to the laravel syntax. Also make sure you pass the id from form to controller. take a look at my example. look where I put $id field.
Now in the controller, this is what you do.
$setting = Setting::findOrFail($id); // you must have a model or you can use DB facade
$setting->description = $request->settings; //name that you called it in form
$setting->save();
Related
I have abstracts table in my database with the Abstract_Status_ID column, I am trying to create a button that changes every Status from '2' to '3'
I have tried to do this:-
My controller
public function publish($A_ID)
{
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
my route
Route::put( '/projects', 'AbstractsController#publish');
Route::post( '/projects', 'AbstractsController#publish');
my view (projects)
Tried with the csrf token and without as eloquent docs says any post/put will be restricted without it.
#if (count ($abstracts)> 0)
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{!! Form::open(['action' => ['AbstractsController#publish' , $abstract->A_ID], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
#endif
#endforeach
The error I am getting when clicking the button
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\AbstractsController::publish()
Also, the code above will show more than one button, any suggestions to make one button change all of them ?
A: If you want to have a button for each ABSTRACTS just change your route to :
Route::put( '/projects/{A_ID}', 'AbstractsController#publish');
B: If you want to have only one button that change all , you can echo their ID in hidden input then send form
so your view would be:
#if (count ($abstracts)> 0)
{!! Form::open(['action' => ['AbstractsController#publish'], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{{Form::hidden('abstract_ids[]', $abstract->A_ID) }}
#endif
#endforeach
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
And Your Controller :
public function publish()
{
foreach(request('abstract_ids') as $A_ID){
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
}
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
You are using form method="POST". I think it should be method="PUT".
The main problem in your code is the definition of your routes, You are passing an argument ($A_ID) to the controllers publish method,
But in your routes, you did never mention that you are passing any argument through your route. So for passing the argument, you can use {$A_ID} in your route URL.
One thing you should always remember is that you should always give your route a name and call it by using its name and route() Laravel helper method.
The benefit of using route name is that if you decide to change the URL-Structure of your route later in the development process, you'll never have to change it everywhere in your application because you did not call it by its URL.
And route name is less error prone
In total, You should define your routes like this
Route::put('projects/{A_ID}/update', 'AbstractsController#publish')->name('projects.update');
Route::post('projects/{A_ID}/post', 'AbstractsController#publish')->name('projects.store');
Then in you blade files now you can call it like
{!! Form::open(['action' => route('projects.store', $abstract->A_ID), 'method' => 'post' ]) !!}
Hope this will clarify and resolve your problem.
I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);
I'm implementing bootstrap-slider in my CRUD, I have implemented it successfully in Create, the problem is when I try to edit it,
I want to get the current value from the Model. Idk how to do this.
This is for PATCH.
<div class="form-group">
<h3 class='box-title text-info'>Percentage</h3>
{!! Form::input('text','percentage',null,['id'=>'ex8', 'data-slider-id'=>'ex1Slider', 'data-slider-min'=>'0', 'data-slider-max'=>'100', 'data-slider-step'=>'5', 'data-slider-value'=>'50']) !!}
</div>
In your form instead of creating a new form. You will bind the form to the model.
{!! Form::model('modelname', [options here] !!}
All the fields will math the model's property values.
Edit
Here is an example
You must used something like this to create a EDIT FORM
{{ Form::model($smartphones, ['method' => 'PATCH', 'url' => 'smartphones/'.$smartphones->id]) }}
You get by using $(your_model)['inputID']...You can use in "data-slider-value"... Something like this
{{ Form::input('text','mem_ram', null, ['id' => 'mem_ram', 'data-slider-value'=>$smartphones['mem_ram']]) }}
I am using form model binding and have the following select input :
{!! Form::select('user_id', $users, old('user_id') ?: Auth::id(), ['class' => 'form-control select2 users']) !!}
I would like to accomplish the following:
In the creation form: select the option where the user_id equals the authenticated user's ID, but if there was old input in the session, select that option instead.
In the edit form: always select the option that is stored in the model.
old('user_id') ?: Auth::id() doesn't seem to work when editing, because it always selects the option of the authenticated user and not the one that is stored in the model.
I believe you already have this line,
{!! Form::Model($users, ['route' => ['admin.user.update', $user->id], 'method' => 'PUT']) !!}
then your code should like this:
{!! Form::select('user_id', $users, isset($selectedUser) ? $selectedUser : null) !!}
at Create function do not pass $selectedUser to view but pass the id in your session,and in your edit function you should pass $selectedUser to view ,which is Auth::user()->id
I want to create a two buttons buyer and seller in the home page, and on click of these buttons, I want the guest to get directed register page with a ~hidden input~ placed in the form depending on the value of the button pressed.
I am using Laravel 5.1
For what I am trying to achieve, do I need to register a new route and a new function?
First, do I need to use a {!! link_to !!} or {!! Form submit !!}. I thought form submit would work:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'seller')) !!}
{!! Form::submit('Seller', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
My routes are:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
auth/register and AuthController is the one that comes with Laravel default. I also didn't register a new public function. Is this the reason? Do I need to send the data from view to controller and then other view (view->controller->view). I am really confused on this one.
This is how it seems in the view-source:
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="seller"><input name="_token" type="hidden" value="Cw4Het1A1M6020oQL45Cy2Q0ct46TSe6ba2g4r4C">
<input class="btn btn-warning" type="submit" value="Seller">
</form>
Edit:
I think I can't send variables between views. So do I need to register a completely new route and a controller, and two methods in it? I am really confused on this stage.
Check out my answer to your previous similar question for details on the implementation:
Laravel Passing Data From One View to Another View
To answer the questions you are posing
You can use two forms one for buyer and one for seller
{!! Form::open(['route' => ['type_path']]) !!}
{!! Form::hidden('type', 'buyer') !!}
{!! Form::submit('Buyer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!
{!! Form::open(['route' => ['type_path']]) !!}
{!! Form::hidden('type', 'seller') !!}
{!! Form::submit('Seller', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!
on your landing page and then have both of this forms direct to a same named route (in this case 'type_path')
Then in your routes.php file you would reference only one controller and one method and handle the logic in that method. Again see the link to my answer to see a full implementation of this for the example of writer and reader.
Essentially as you say, you'd be sending as the hidden inputs from the view to the controller to the second view.
You could register a new route like:
Route::get('auth/{TYPE}/register', 'Auth\AuthController#getRegisterType')
Your home page would have something like this:
seller
Your controller method would be something like this:
public function getRegisterType($type)
{
Session::put('registerType', $type);
return redirect('/auth/register');
}
Then you can access you set session variable from the controller methods getRegister & postRegister
Have you two buttons direct to the register page with a query string parameter:
Buyer
Seller
And then just check for the query string parameter in your register.blade.php view:
{!! Form::hidden('account_type', Request::query('type')) !!}
If you need to validate one of the two is present, then you could create a middleware class you apply to the login route only:
public function handle($request, Closure $next)
{
if (! in_array($request->query('type'), ['buyer', 'seller'])) {
// Error
}
return $next($request);
}