Laravel5 Pagination Links Getting 404 Error - php

I developed a laravel web application and created a pagination view of business list...
I have two business rows in my business table and collecting each one row in a view give pagination for accessing others...
Controller..
public function businessindex(){
$arr = DB::table('business')->paginate(1);
$arr->setPath('business');
return View::make('admin.SABC.businessindex')->with('data',$arr);
}
Routes.php
Route::get('business',['as'=>'business','uses'=>'SABusinessCustomersController#businessindex']);
View.
<tbody>
<?php $i=1;?>
#foreach($data as $datas)
<tr>
<td>{{$i++}}</td>
<td>{{$datas->ci_busns_name}}</td>
<td>{{$ctgry}}</td>
<td>{{date('M-d-Y',strtotime($datas->created_at))}}</td>
<td><span class="{{$grenclass}}">{{$status}}</span><span class="{{$redclass}}" id="{{$datas->pk_business_id}}">{{$oppostatus}}</span></td>
<td><span class="{{$lgrenclass}}">{{$lglstatus}}</span><span class="{{$lredclass}}" id="{{$datas->pk_business_id}}">{{$opplgl}}</span></td>
<td>
<span><a class="editThis" id="{{Crypt::encrypt($datas->pk_business_id)}}" href="{{URL::route('edit_business_view',[Crypt::encrypt($datas->pk_business_id)])}}"></a></span>
<span><a onclick="return confirm('Are you sure you want to delete this item?');"
class="dltThis" href="{{URL::route('brand_delte',[$datas->pk_business_id])}}"></a></span>
</td>
</tr>
#endforeach
</tbody>
</table>
<span class="totalContspan">Showing 0 to {{$i-1}} of {{$i-1}} entries</span>
<div class="paghdlr">{!! str_replace('/?', '?', $data->render()) !!}</div>
<div class="clearfix"></div>
Now I am getting like above screenshot,, its sending too many ajax request continuously in my page. I can see that from network tab of Firebug.
When Click on the links Its throwing an ajax request and get 404 error, I doesn't try the laravel ajax pagination in my site. Only required the Laravel Pagination.
How can i solve this issue..?

Related

Problem with displaying data that I want to delete with many to many relationship in Laravel

I have many to many relationship between users and products table and I set up a view (welcome.blade.php) that when I click on the product name, which is a link, it is supposed to redirect me to page where my delete form is so I can delete that particular product, but I get 404 not found page. I suspect that error is somewhere in my routes but I can't seem to find the problem. Also when I click on some product my url says project/destroy/1 which I think is good. Here is my code:
web.php:
Route::get('/home', 'HomeController#index')->name('home');
Route::post('/store', 'HomeController#store')->name('store');
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
destroy.blade.php:
<div class="col-md-12">
<form action="destroy/{{ $product->id }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
welcome.blade.php:
#if($products)
<table class="table">
<thead>
<th>#</th>
<th>Product Name</th>
<th>Owner Of The Product</th>
<th>Created At</th>
</thead>
<tbody>
#foreach ($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>
{{ $product->files }}
</td>
<td>
#foreach ($product->users as $user) {{ $user->name }}
#endforeach
</td>
<td>{{ date('M j, Y', strtotime($product->created_at)) }}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
HomeController.php:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class HomeController extends Controller
{
public function destroy(Product $product)
{
$product->users()->detach();
$product->delete();
return view('destroy');
}
}
You defined the following route in your file.
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
This creates a DELETE route. DELETE routes are meant to be accessed by APIs. Links in anchor tags use GET routes. Clicking on
{{ $product->files }}
makes the router try and match GET /destroy/{$id} which is not defined, so it throws either an exception if debug is on or a 404 if not. You can see this in action by looking at the network tab of your browser's developer console.
Unless you add another GET route, you'll keep getting 404s.
Route::get('/destroy/{$id}', 'HomeController#destroy')->name('product.destroy-form');
will make the following link work.
{{ $product->files }}
Also, in the destroy method you're returning the view without passing any variables but in destroy.blade.php you seem to be using $product. Don't forget to add it!
You are nearly there, you just have a step too many.
Good call on the form and using the delete method, this is absolutely what you are supposed to do
However where you are going wrong is in using the link to go to a separate page with the form on to delete. You are better using a little javascript so the link submits a hidden form from the link.
<a onclick="document.getElementById('delete-form-{{$product->id}}').submit();}">{{ $product->files }}</a>
<form id="delete-form-{{$product->id}}" action="{{ route('destroy', ['id' => $product->id]) }}" method="POST" style="display: none;">
#csrf
#method('delete')
</form>
You can do the method you have but you need an additional route::get request to a page that loads the form and then you can submit the form on that page to delete.
I think it is because to delete a product using a new page the first thing to do is navigate to that page which you are not doing. You are instead going directly to the delete function using this
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
You need to define a route that lets you go to that page first which u can do by creating a route like this:
Route::get('/delete/{$id}', 'HomeController#delete')->name('delete');
You should create a new function delete in the HomeController that just returns the destroy.blade.php. Something like this.
$product= Product::find($id);
return view('destroy')->with('product', $product);
where $id is the product id and Product is the model that you are using.

Showing only first and single record from database in laravel 5.5

I am trying to get all record which are available in my database table currently there are 3 records available with id 1,2 and 3 and i am only getting record related to id 1.
My Controller method:
public function EmailList()
{
$listing = Buyer::all();
return view('email-list',compact('listing'));
}
and my route:
$router->get('/List-of-Emails', 'AjaxController#EmailList');
and in my view i am passing foreach loop
#foreach($listing as $list)
<tr>
<input type="hidden" id="rwid" value="{{$list->id}}"/>
<td>
<input id="{{$list->id}}" class="checkbox-custom" name="{{$list->id}}" type="checkbox"
value="{{$list->id}}">
<label for="{{$list->id}}" class="checkbox-custom-label"></label>
</td>
<td>
<p data-placement="top" data-toggle="tooltip" title="" data-original-title="Tolltip">
{{$list->name}}
</p>
</td>
<td>
<a href="" class="pglst-lnk">
{{$list->email}}
</a>
</td>
<td>
{{$list->created_at}}
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
i am getting only first and only single record however i want to get all records from table where i am doing wrong.
Any help would be highly appreciated!
I think this is a DOM parsing error.
Can you put the #endforeach just below the closing </tr> tag?
#foreach($listing as $list)
<tr>
<!-- data -->
</tr>
#endforeach
EDIT: Explanation
For those interested: the browser is incapable of parsing the HTML as given by OP. This is because after one record, he closes the table and a couple of divs. Then the foreach-loop starts again, in which he suddenly introduces new <tr>s and is closing <table>s and <div>s that don't exist. It will probably put an error message in the console, but not everybody looks there as it's not directly obvious that the outputted HTML is wrong.
Use Some query like
$user = DB::table('users')->where('name', 'John')->orderby('id','desc')->limit('the record you want')->first();

Laravel "No query results for model [App\Schedule]" error when not calling model

I have a perplexing issue that I cannot seem to figure out. I am trying to load a webpage from a button, but it keeps poping up the error No query results for model [App\Schedule]. I have no idea why it's doing that, as there is nowhere I am trying to access that model when trying to load the webpage. Below is what I have so far (it's a work in progress)
Here is the link:
<a class="btn btn-primary" role="button" href="/schedule/availability">Set Availability</a>
Here is the Route:
Route::get('/schedule/availability', 'ScheduleController#getAvailabilityCalendar');
Here is the method inside of the ScheduleController:
public function getAvailabilityCalendar(){
$dt = Carbon::now('America/Los_Angeles');
$return['DayOfWeek'] = $dt->dayOfWeek;
$return['DisplayDays'] = (($dt->daysInMonth + $return['DayOfWeek'] + 1) <= 35) ? 34:41;
return view('contractors.availability', compact('return'));
}
And here is the view that is returned:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Sunday </th>
<th>Monday </th>
<th>Tuesday </th>
<th>Wednesday </th>
<th>Thursday </th>
<th>Friday </th>
<th>Saturday </th>
</tr>
</thead>
<tbody>
<tr>
#for ($i = 0, $j=-$return['DayOfWeek']; $i <= $return['DisplayDays']; $i++, $j++)
#if($i < $return['DayOfWeek'])
<td></td>
#else
#if($i %7 == 0)
</tr><tr>
#endif
#if($j < 30)
<td>{{\Carbon\Carbon::now('America/Los_Angeles')->addDays($i-$return['DayOfWeek']+1)->format('F jS')}}
<hr>
<div class="form-group">
<label data-toggle="popover" data-trigger="hover" data-content="Add Times available in the following format: 10:00am-4:00pm. You may also add multiple blocks of time separated by comma. Example: 10:00am-12:00pm, 2:00pm-4:00pm">Available Time</label>
<input type="text" class="form-control availability" />
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="all-day-check" />All Day</label>
</div>
</td>
#else
<td></td>
#endif
#endif
#endfor
</tr>
</tbody>
</table>
</div>
<script>
$(document).on('change', '.all-day-check', function(){
if (this.checked) {
$(this).closest("input.availability").prop("disabled", true);
}else{
$(this).closest("input.availability").prop("disabled", false);
}
});
</script>
I can't find anywhere that would need a reference to the App\Schedule model, which is a model in my project. I am at a total loss as to what it could be or why it's trying to run something I never asked it to run.
Edit: There is also no other instance of /schedule/availability in the routes file as well, I triple checked.
Update: Here is the Constructor for ScheduleController:
public function __construct()
{
$this->middleware('auth'); //Check is user is logged in
}
Here is the stacktrace I received (For some reason, it didn't show up in my error log so I only have the screenshot)
As it turns out in the discussion in comments above. The culprit is narrowed down to a conflicting route.
Route:something('/schedule', ...)
was found before
Route::get('/schedule/availability', 'ScheduleController#getAvailabilityCalendar');
and the Route:something fires before the desired route. It should be noted that route defined first will be the first one to serve. This is the reason why Route:resource should be put after your custom Route:get or another routes to avoid confusion.
Next item in the list when changing routes but it does not updated, alwasy fire a php artisan route:clear. It will clear the route caches - can check in the docs.

Dynamic buttons href tests with phpunit

I'm initiating my tests using phpunit so, I have doubts about how to test things dynamically. I created a table dynamically as the image bellow
Here are my view:
<div class="panel-body">
#if(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
<table class="table">
<th>Unity Name</th>
<th>Phone Number</th>
<th>Actions</th>
<tbody>
#foreach($companies as $company)
<tr>
<td>{{$company->name}}</td>
<td>{{$company->owner_number}}</td>
<td>
{{Form::open(['method' => 'DELETE', 'url'=>'/admin/company/'.$company->id, 'style' => 'display:inline'])}}
<button type="submit" class="btn btn-default fa fa-trash-o"></button>
{{Form::close()}}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
So, how can I test the href tag if I haven't previously the tag id?
I did some tests like these one in Symfony projects.
In dev environment, I insert some data in a dev database. After this step, I launch functionnal tests. In these tests, I analyze tag's content. Here is an example from your data :
$client = static::createClient();
$crawler = $client->request('GET', '/yourUrl');
$node = $crawler->filter('#show-company-1');
self::assertNotEmpty($node, "Node #show-company-1 does not exists");
self::assertEmpty( $node->html(), "#show-company-1 content is not empty");
self::assertContains('/admin/company/1', $node->attr('href'), "a#show-company-1.href has not a good value");
Before request, you can add some logic to determine the id of your company.

Laravel Destroy Option not deleting

Why do this occurs? This is my index lists of all accounts. I want to just delete a specific category by that category.destroy route but it's
index.blade.php
#extends('layouts.master')
#section('title','All Categories')
#section('contents')
<div class="row">
<div class="col-md-8 col-sm-4 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">All Categories</div>
<div class="panel-body">
<article>
<div class="table-responsive-vertical shadow-z-1">
<!-- Table starts here -->
<table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>ID No</th>
<th>Category</th>
<th>Edit/Delete</th>
<th>Status</th>
</tr>
</thead>
#foreach($categories as $category)
<tbody>
<tr>
<td data-title="ID">{{$category->id}}</td>
<td data-title="Name">{{$category->name}}</td>
<td>Edit
&nbspDelete
</td>
</tr>
</tbody>
#endforeach
</table>
</div>
</article>
</div>
</div>
</div>
</div>
#endsection
#section('js')
{!!Html::script('assets/js/jquery.min.js')!!}
{!!Html::script('assets/js/bootstrap.min.js') !!}
<script>
$('#flash-overlay-modal').modal();
</script>
<script>
$('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>
#endsection
CategoryController.php
public function destroy($id){
$category = Category::findOrFail($id);
$category->delete();
Session::flash('flash_message', 'Task successfully deleted!');
return redirect()->route('category.index');
}
instead it just displays the view specific entry of the category. It's not deleting or something
To access your destroy route, you have to use the DELETE HTTP request verb. HTML links only allow GET requests.
You should either change your HTML link to an HTML form that spoofs the DELETE method, or look into using something like restfulizer.js that will automatically convert your delete links to delete forms.
As has been suggested, you could also create a GET route for the delete functionality, but there are potential consequences to this. GET and HEAD requests should generally be thought of as "read only" requests, and should not modify any data. POST, PUT, PATCH, and DELETE requests are generally thought of as "write" requests. A web spider may crawl your delete links and end up deleting all your data, or a web browser may pre-fetch all the GET requests on a page, so the delete link is accessed even though no one clicked the delete button. There are a lot of potential nasty things that can happen when you start allowing GET requests to modify data. There is some good information in this answer.
Try with this route:
Route::get('category/{category}/destroy',[
'uses'=>'CategoryController#destroy',
'as' => 'category.destroy'
]);

Categories