简体   繁体   中英

php change certain rows value with a button

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM