Laravel current route name contains - laravel

I have a group of routes like so:
Route::group(['prefix' => 'messages'], function () {
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessagesController#store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessagesController#show']);
Route::put('{id}', ['as' => 'messages.update', 'uses' => 'MessagesController#update']);
Route::get('{id}/read', ['as' => 'messages.read', 'uses' => 'MessagesController#read']); // ajax + Pusher
});
In my side bar I'd like to highlight the area the user is in when they click on any of these routes:
<li class="d-flex flex-column {{ (Route::currentRouteName() == 'messages') ? 'active' : '' }}">
This would work if the route was 'messages'. is there away I can do it to be like Route::currentRouteNameContains... Obviously that's not a Laravel method but how can I have it active if it CONTAINS messages?

See str_contains helper function
<li class="d-flex flex-column {{ str_contains(Route::currentRouteName(), 'messages') ? 'active' : '' }}">
Beware that this would return true even if the route is /user/chat/messages/unread which is probably not what you want
You may look at starts_with
<li class="d-flex flex-column {{ starts_with(Route::currentRouteName(), 'messages') ? 'active' : '' }}">
Laravel 6+ users
The helper functions no longer ship with the default installation
You need to install the laravel/helpers package
composer require laravel/helpers

You can use getPrefix() helper function, too.
{{ request()->route()->getPrefix() === '/messages' ? 'active' : '' }}
Also you can use Route facades to do this. result will same with first
{{ Route::current()->getPrefix() === '/messages' ? 'active' : '' }}
Or combine basename() like below
{{ basename(request()->route()->getPrefix()) === 'messages' ? 'active' : '' }}

Related

Missing Keys: title

I installed Bagisto and everything run smoothly until I added Laravel AliExpress Dropshipping.
There's error :
Webkul\Ui\Exceptions\ActionKeyException
Missing Keys: title (View: /Applications/MAMP/htdocs/bagisto/packages/Webkul/Dropship/src/Resources/views/admin/products/index.blade.php)
What does Missing Keys: title means
#extends('admin::layouts.content')
#section('page_title')
{{ __('dropship::app.admin.products.title') }}
#stop
#section('content')
<h1>
{{ __('dropship::app.admin.products.title') }}
</h1>
<div class="page-action">
<button class="btn btn-lg btn-primary" style="display: none">
{{ __('dropship::app.admin.products.import-btn-title') }}
</button>
</div>
I had a similar problem with Bagisto Bulk Upload extension. To fix it I added the 'title' key to both addAction at extension's DataGrid class.
public function prepareActions() {
$this->addAction([
'type' => 'Edit',
'method' => 'GET', // use GET request only for redirect purposes
'route' => 'marketplace.bulkupload.profile.edit',
'icon' => 'icon pencil-lg-icon',
'title' => ''
]);
$this->addAction([
'type' => 'Delete',
'method' => 'GET', // use GET request only for redirect purposes
'route' => 'marketplace.bulkupload.profile.delete',
'icon' => 'icon trash-icon',
'title' => ''
]);
$this->enableAction = true;
}
Source

Route in Laravel Blade show double prefix

I'm new in Laravel and now I'm trying to make link in my blade files.
I read that in many tutorials, you can just use href="{{route('mm-admin/blog')}}" to make it works.
And by works I mean the list should be like this "mm-admin/blog".
But what i get using that code is "mm-admin/mm-admin/blog".
I tried to remove the mm-admin from this code "route('mm-admin/blog')" and it returns error saying
blog isn't defined.
what is wrong with my code??
this is my blade file
<li class="{{ request()->is('mm-admin/dashboard') ? 'active' : '' }}">
<a href="{{route('dashboard')}}">
<i class="fas fa-home"></i> <span>Dashboard</span>
</a>
</li>
and this is my web route
Route::group(['prefix' => 'mm-admin', 'as' => 'mm-admin.'], function () {
Route::get('/', 'Admin\LoginController#showLoginForm')->name('login');
Route::get('/login', 'Admin\LoginController#showLoginForm')->name('login');
Route::post('/proseslogin', 'Admin\LoginController#login');
Route::get('/blog', [
'as' => 'blog',
'uses' => 'Admin\BlogController#index', 'middleware' => 'admin',
]);
});
This should work:
href="{{route('mm-admin.blog')}}"

laravel 5.4 MethodNotAllowedHttpException in RouteCollection.php (line 251)

I'm an amateur in laravel. I use laravel 5.4. so I want to make process delete without form binding but I have an error message like this. Please tell me how to solving this.
route:
Route::delete('test/{id}','TestController#destroy');
My Form:
<td><button type="button" class="btn"><a href="{{ action('TestController#destroy', $post['id']) }}" method="post" >Hapus</button>{{ csrf_field() }}{{ method_field('DELETE') }}
</td>
My Controller:
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect()->to('coba/test');`
}
Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
Delete
{{ Form::open(['url' => 'test/'.$post->id, 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
Edit
{{ Form::open(['url' => 'coba/test/'.$post->id.'/edit', 'method' => 'POST']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding id of the post so you dont have multiple html forms in your code.

How to add form attribute in laravel

I know that to insert a attribute in forms in laravel we do something like below
{{ Form::open(array('url' => 'comment', 'method' => 'post')) }}
But what i want to do is insert a attribute like this and not in commas
<form data-abide>
Because I'm using foundation as a front end framework so need to use its validation.
You can add it similar way you add method :
{{ Form::open(array('url' => 'comment', 'method' => 'post', 'data-abide' => 'formclass', 'onSubmit' => 'return false;')) }}

Laravel : I am trying to pass a variable from view to controller but it returns null

This is My View code When i am executing the application it only returns me NULL value, basically my $office_category is not being passed, i need the office category to query the database
<div class="box-body">
{{ Form::open(['route' => 'office.index','class' => 'form-horizontal office-form']) }}
<div class="form-body">
<div class="form-group">
<div class="col-md-3">
{{ Form::select('office_category', [
null=>'Please Select',
'Software' => 'Software',
'Computer Hardware' => 'Computer Hardware',
'Survey Instruments' => 'Survey Instruments',
'Office Equipments' => 'Office Equipments'
], isset($office_category) ? $office_category : '', ['class' => 'form-control input-xlarge select2me', 'placeholder' => 'Project Type', 'id' => 'office_category'] ) }}
</div>
{{ Form::hidden('office_category', $office_category) }}
{{ Form::submit('Search Equipment',['class' => 'btn green']) }}
</div>
</div>
{{ Form::close() }}
My Controller Code: I want the Office category thats it
Class OfficeController extends BaseController{
public function index(){
$office_category = Input::get('office_category');
if($office_category=='')
$offices = Office::orderBy('office_category', 'asc')
->get();
else
$offices = Office::where('office_category','=',$office_category)
->get();
$assets = ['table','datepicker'];
$users = User::where('client_id','=','')
->orderBy('name','asc')
->lists('name','username');
return View::make('office.index',[
'office_category' => $office_category,
'offices' => $offices,
'users' => $users,
'assets' => $assets
]);
}
Where am i going wrong please help.
You have a hidden field directly after your select that has the same name as the select. The value of this hidden field (empty) is what is getting sent to the server.
Delete this line:
{{ Form::hidden('office_category', $office_category) }}
Or rename this hidden field.
By default Form::open creates a POST request and your index method on Controller are expecting a GET request.
You need to add a new route on routes.php to match this POST request.
Route::post('index', 'OfficeController#index');
Or if you don't mind, you can set index to listen any kind of request:
Route::any('index', 'OfficeController#index');
In most of the case, above answer will solve your problem. If not, you can inspect your web request from browser and confirm value in $office_category variable.

Resources