With the following code, I'm trying to pass a wildcard to my controller but I'm not sure how I can pass the URL dynamically and I'm not sure how to do this without creating a route for each url, which would take forever. Currently, I'm attempting to do it like so:
<a href="{{ route('purchase-get') }}/$item->name">
Here is the rest of the code
<tbody class="text-center">
#foreach (array_chunk($items->all(), 3) as $item_each)
<tr>
#foreach($item_each as $item)
<td>
<a href="{{ route('purchase-get') }}/$item->name">
{{ HTML::image($item->image_url, 'item-image', array('class' => 'item-image-row')) }}
<h4>{{ $item->item }}</h4>
<span class="text-muted">{{ $item->cost }}</span>
</a>
</td>
#endforeach
</tr>
#endforeach
</tbody>
You can link to a wildcard with named routes:
Route::get('/', array('as' => 'index', function()
{
$slug = 'product-1';
return 'link to product';
}));
Then catch the names route with the wildcard slug:
Route::get('products/{slug}', array('as' => 'products', function($slug)
{
$product = Product::where('slug','=',$slug)->first();
return $product;
}));
Related
I want to edit information by id :
<td>
<a href="{{ route('administration.edit',$student->id) }}" class="btn btn-success">
Edit
</a>
</td>
This is my route:
Route::get('/administration/edit/{$id}','AdministrationController#editstudent')->name('administration.edit');
This is my controller method:
public function editstudent($id){
//
}
<td>
<a href="{{route('administration.edit',['id' => $student->id])}}">
Edit
</a>
Try this,
<td>
Edit
</td>
OR
<td>Edit
</td>
Change the route.The $ is not required in the route.
Route::get('/administration/edit/{id}','AdministrationController#editstudent')->name('administration.edit');
try these
Route::get('administration/edit/{id}',[
'uses' => 'AdministrationController#editstudent',
'as' => 'editAdmin'
]);
public function editstudent($id){
dd($id);
}
In Blade
Edit
I am using filter by serial number in table,, but when I click the button error like this
" method links does not exist"
this is my controller
public function show(Request $request)
{
$instrument = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instrument->appends($request->only('serial_number'));
return view('settinginstrument.index', compact('settinginstrument'));
}
this is my blade:
<table class="table">
#foreach ($instruments as $instrument)
<tr>
<td>
Serial Number : {{ $instrument->serial_number }}
#if($instrument->customer !== NULL)
<div class="text-muted"><i class="fa fa-hospital-o" aria-hidden="true"></i> {{ $instrument->customer->name }}</div>
#endif
#if($instrument->contractType !== NULL)
<div class="text-muted"><i class="fa fa-compress" aria-hidden="true"></i> {{ $instrument->contractType->name }}</div>
#endif
</td>
<td>
<div class="pull-right">
<a type="button" href="{{ route('instrument.edit', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
class="btn btn-success ">
<i class="fa fa-pencil"></i> Edit
</a>
#if($instrument->customer == NULL || $instrument->contractType == NULL)
<a href="{{ route('instrument.destroy', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
data-method="delete" data-confirm="" class="btn btn-danger">
<i class="fa fa-trash"></i> <span class="hidden-xs">Delete</span>
</a>
#endif
</div>
</td>
</tr>
#endforeach
</table>
<div class="paginator text-center">{{ $instruments->links() }}</div>
#else
....................................
where wrong with my code?
You are not passing the param to the view:
public function show(Request $request)
{
$instruments = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instrument->appends($request->only('serial_number'));
return view('settinginstrument.index',['instruments' => $instruments);
}
Then on the blade:
{{$instruments->links()}}
When you have zero counts of record and you are trying to access the links() then such issue occurs
Add on condition to check the record exists.
#if($instruments)
<div class="paginator text-center">{{ $instruments->links() }}</div>
#endif
Also in the controller you have one typo error $instruments and $instrument
I recommend that add the condition above the foreach
and on blank result show some message that record not found.
Update
Also pass the $instruments to the balde
$instruments = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instruments->appends($request->only('serial_number'));
return view('settinginstrument.index', compact('instruments'));
I'm getting this error. This error comes in when I hit the delete button
MethodNotAllowedHttpException in RouteCollection.php line 251:
the problem is as far as I know my routes is correct.
My cart.blade.php
#foreach($cartItems as $cartItem)
<tr>
<td>
<img src="{!! asset("product_images/$cartItem->img") !!}" alt="..." class="img-responsive">
</td>
<td>{!! $cartItem->name !!}</td>
<td>{!! $cartItem->qty !!}</td>
<td>R {!! $cartItem->qty * $cartItem->price !!}</td>
<td>
{!! Form::open(array('method' => 'Delete', 'route' => array('deleting', $cartItem->rowId))) !!}
<button class="btn btn-warning">Delete 2</button>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes
Route::put('/product/deleting/{id}', [
'uses' => 'OpenController#deleting',
'as' => 'deleting'
]);
my controller
public function deleting($id)
{
echo "string";
}
You are using method DELETE on your form:
Form::open(array('method' => 'Delete',
but defined the method PUT in your route:
Route::put('/product/deleting/{id}', [
Try to change the route to
Route::delete('/product/deleting/{id}', [
Try using 'delete' instead of 'put' in your routes file.
I have created a demo application in Laravel 4, for some learning experience. The routes.php is as follows:
Route::get('/', function()
{
return View::make('hello');
});
Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
$layout = View::make('users.dashboard');
$layout->title = "Dashboard";
$layout->content = View::make('users.dashboard');
return $layout;
});
Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController#all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController#create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController#getLogout']);
Route::resource('/','UserController');
});
Route::controller('users', 'UserController');
Here, I prefixed the "users". However, Trying logging in with/out correct login details, it shows only the login page instead of showing up the error message on the login page or dashboard.
Any suggestion? FYI,
The index.blade.php in app\views\users is below:
#extends('users.user')
#section('title', 'User Listing Page')
#section('description', 'This is the user listing page')
#section('main')
<h1>All Users</h1>
<p>{{ link_to_route('users.create', 'Add new user') }}</p>
<h1>Login</h1>
<p>{{ link_to_route('users.login', 'Login') }}</p>
<table>
<tr>
<td>{{ $users->links() }}</td>
</tr>
</table>
#if ($users->count())
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->phone }}</td>
<td>{{ $user->name }}</td>
<td>{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}</td>
<td>{{ Form::open(array('method' => 'DELETE', 'route' => array('users.destroy', $user->id))) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
</table>
<table>
<tr>
<td>{{ $users->links() }}</td>
</tr>
</table>
#else
There are no users
#endif
#stop
The script login.blade.php (on the same location as above) as follows:
#extends('users.user')
#section('main')
<h1>Login</h1>
{{ Form::open(array('route' => 'users.login')) }}
<ul>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#if ($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
#endif
#stop
There is no users.edit route defined in your routes file. Hence, you are bound to receive that error. There's no users.edit route in your code.
Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
$layout = View::make('users.dashboard');
$layout->title = "Dashboard";
$layout->content = View::make('users.dashboard');
return $layout;
});
Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController#all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController#create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController#getLogout']);
Route::resource('/','UserController');
});
Add the following route somewhere in your routes file and the error should disappear:
Route::get('/edit/', [ 'as' => 'users.edit', 'uses' => 'UserController#edit']);
Note: You need to replace the controller method name edit to whatever you have defined.
UPDATE 1
That is because, you have 'before' => 'auth' parameter passed in your Route::group() method. Just remove users from edit route and I hope that should do the job for you.
Just A Side note
I presume that you are new in laravel and have less amount of experience with the framework. I would recommend you to go with this only in order to get your hands dirty with the basics. Then once you start gaining the experience and know how things work behind the scenes, you can jump off to the next level.
If I were at your place, I wouldn't have gone with the code that you have provided, instead I would have kept it way too simple for now. My routes.php file would be like this:
/* Routes for users not logged in. */
Route::get('/all', 'UserController#all');
Route::get('/create', 'UserController#create');
Route::get('users/login', 'UserController#getLogin');
Route::post('users/login', 'UserController#postSignin');
Route::get('users/logout', 'UserController#getLogout');
/* Routes for logged in users */
Route::get('/users/dashboard', 'UserSessionsController#dashboard');
Route::get('/users/edit/{id}', 'UserSessionsController#edit');
And then in your view file:
EDIT
Then I would have used the middleware only inside the UserSessionsController file like this:
/**
* Check if the user is authenticated.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth'); // or any other middleware you have.
}
UPDATE 2:
Learn more about the middleware from the following links:
For version: 5
Official Docs
Matt Stauffer On Middleware
For version: 5.1
Official Docs
Youtube Video
For version: 5.2
Official Docs
Hope this helps you out. Happy Coding. Cheers.
in Laravel 8 it's a bit different
Route::get('/users/edit',[UserController::class, 'edit']);
In Laravel 5 we can send slugs to urls, something like this:
#foreach($records as $record)
<div class="btn-group-sm">
<a href="{{ url('business/'.$record->id.'/edit') }}" class="btn btn-warning"><i class="fa fa-edit"></i></a >
</div>
#endforeach
Would it be the same as this:
#foreach($records as $record)
<div class="btn-group-sm">
<a href="{{ route('businessEdit', $record->id) }}" class="btn btn-warning"><i class="fa fa-edit"></i></a >
</div>
#endforeach
Here's the route:
Route::get('/business/{id}/edit', [
'as' => 'businessEdit',
'uses' => 'BusinessCtrl#edit'
]);
Edit 1
Using the url way i can send the parameter and the address looks like domain.tld/business/4/edit but when using the route it looks like domain.tld/business?4 and it doesn't work.
Here's the edit func at BusinessCtrl:
public function edit($id)
{
$record = Business::find($id);
return view('Center.business.edit')->with('record', $record);
}
The route helper accepts an array as the second argument, so you will want to do this:
{{ route('businessEdit', [$record->id]) }}