laravel-5.4如何仅使用按钮更改数据库中的数据或id的状态?

时间:2018-04-16 10:01:28

标签: php laravel laravel-5.4

我这里有一个图像crud的代码,当状态变为1时应该显示图像,默认值为0,直到单击按钮它应该将值更改为1并且它还应该更新数据库和单击刀片上的检查按钮后,应显示刀片状态。

controller AdvertisementController方法或函数apply是一个不能正常工作的。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Advertisement;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

class AdvertisementController extends Controller
{
    public function index()
    {
        $advertisements = Advertisement::all();

        //Load all users on the table and pass the users
        $advertisements = Advertisement::where(['archive'=>1])->orderBy('id')->get();
        return view('adscrud.index')->with('advertisements', $advertisements);      
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $advertisements = new Advertisement;
        $url = '';
        $advertisements->title = $request->adsTitle;      
        $advertisements->detail = $request->adsDetail;

        //image uplod
        if(Input::hasFile('images')){

            $file = Input::file('images');
            $file->move(public_path(). '/uploads-ads/', $file->getClientOriginalName());
            $url = '/uploads-ads/'. $file->getClientOriginalName();


        } 
        $advertisements-> image = $url;
        $advertisements -> save();


        $advertisements = Advertisement::all();
        $advertisements = Advertisement::where(['archive'=>1])->orderBy('id')->first();

        //email verification


        // $thisUser = User::findOrFail($users->id);
        //  $this->sendEmail($thisUser);
        //$users = User::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('adsindex')->with('advertisements', $advertisements);


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $advertisements = Advertisement::where(['id'=>$id])->first();


        // dd($users);

         return view('adscrud.edit',compact('advertisements'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $advertisementsUp = new Advertisement;
        $url = '';

        if(Input::hasFile('images')){

            $file = Input::file('images');
            $file->move(public_path(). '/uploads-ads/', $file->getClientOriginalName());
            $url = '/uploads-ads/'. $file->getClientOriginalName();


        } 


        //dd($request->editadsTitle);

        $advertisementsUp = Advertisement::where('id',$id)
            ->update(['title'=>$request->editadsTitle,'detail'=>$request->editadsDetail, 'image'=>$url]);

        $advertisements = Advertisement::all();
        $advertisements = Advertisement::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('adsindex')->with('advertisements', $advertisements);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $advertisementsDel = Advertisement::where('id',$id)->update(['archive'=>0]);

        $advertisements = Advertisement::all();
        $advertisements = Advertisement::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('adsindex')->with('advertisements', $advertisements);

    }

    public function enableview()
    {
        $advertisements = Advertisement::all();

        //Load all users on the table and pass the users
        $advertisements = Advertisement::where(['archive'=>0])->orderBy('id')->get();
        return view('adscrud.enable')->with('advertisements', $advertisements); 

    }

    public function enable($id)
    {
        $advertisementsEnable = Advertisement::where('id',$id)->update(['archive'=>1]);

        $advertisements = Advertisement::all();
        $advertisements = Advertisement::where(['archive'=>0])->orderBy('id')->get();

        return redirect()->to('adsEnableView')->with('advertisements', $advertisements);

    }

    public function apply(Request $request){
        $id = $request->adsapply_id;

        //Clearing of all Status to zero (0)
        Advertisement::where('status',1)->update(['status'=>'0']);

        //Updating or Applying the running status
        Advertisement::where('id',$id)->update(['status'=>'1']);

        return redirect('/ads');
    }

}

视图

@extends ('layouts.dashboards')

@section('content')


<link rel="stylesheet" type="text/css" href="">

<a  type="button" class="alert alert-warning" href="{{ 'adsEnableView' }}"><i class="fa fa-trash"></i><i class="fa fa-repeat"></i></a>


<button type="button" class="btn btn-primary"  data-toggle="modal" data-target="#exampleModal"><i class="fa fa-user-plus"></i>
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Create New Ads</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>

<div class="modal-body">


<form method="post" action="{{url('adscreate')}}" class="login" enctype="multipart/form-data">
        {{ csrf_field() }}
  <div class="form-group">
    <label for="exampleInputEmail1">Ads Title</label>
    <input name="adsTitle" type="text" class="form-control" placeholder="Enter Ads Title" required maxlength="50" minlength="3">
  </div>

  </div>
  <div class="form-group">
    <label>Upload Image</label>
    <input type="file" name="images" id="images" enctype="multipart/form-data">
  </div>

   <div class="form-group">
    <label for="exampleInputEmail1">Ads Detail</label>
    <input name="adsDetail" type="text" class="form-control" placeholder="Enter Ads Detail" required maxlength="50" minlength="3">
  </div>

  <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</form>       
      </div>
    </div>
  </div>
</div>


<div class="col-md-12">


<div class="card-body table-full-width table-responsive">
  <h2>Advertisement Management</h2>
  <p>Create New Ads, Modify Existing, Delete Existing Ads</p> 
<form action="{{ route('applyAds') }}" method="post">
  {{ csrf_field() }}                
<table id="myTable" ctable class="table table-hover table-striped" >
    <thead>
      <tr>
      <th>Ads Image</th> 
      <th>Title</th>
            <th>Detail</th>
      <th>Status</th>
      <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr>
    @foreach ($advertisements as $value)
        <td>@if(!empty($value->image))
              <img src="{{ $value->image  }}" class="ads-image" alt="" width="70" height="70">
            @else
              <img src={{ url('uploads-ads/avatar.png') }} class="ads-image" alt="" width="70" height="70">
            @endif
        </td>    
        <td>{{ $value->title }}</td>
        <td>{{ $value->detail }}</td>
        <td>@if($value->status=='1')
        Active
        @endif</td>
        <td>
        <button onclick="$('#adsapply_id').val('{{ $value->id }}')" type="submit" class="btn btn-success"><i class="fa fa-check"></i></button>

        <a href="{{ url('adsEdit', $value->id)}}" class="btn btn-primary"><i class="fa fa-edit"></i></a>

        <a href="{{ url('adsDelete', $value->id)}}" class="btn btn-danger"><i class="fa fa-trash"></i></a>
        </td>
      </tr>
    @endforeach
    </tbody>
  </table>
</form>
</div>
</div>


<!-- $(document).ready(function(){
    $('#myTable').DataTable();
});
 -->

@endsection

路线

//applying ads
Route::post('/ads/apply',['uses'=>'AdvertisementController@apply','as'=>'applyAds']);

1 个答案:

答案 0 :(得分:2)

您必须在此处实施 ajax ,并在响应后刷新 数据表 。否则,如果您想从服务器的角度来看,则必须刷新页面。