0
votes

I have here a code for an image crud that should show an image when status is change to 1, the default value is 0 until the button is clicked it should change the value to 1 and it should also update on the database and active should show on the status in blade upon click the check button on the blade.

controller AdvertisementController the method or function apply is the one not working right.

<?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');
    }

}

view

@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

route

//applying ads
Route::post('/ads/apply',['uses'=>'AdvertisementController@apply','as'=>'applyAds']);
1
is the edit intended for the post only or is the edit related to the answer or help that I'm asking. thanks btw kakashi - yobab77

1 Answers

2
votes

You have to implement the ajax in this and refresh the datatable after the response. Otherwise if you want to do from the server point of view, you have to refresh the page.