My view is like this :
<li class="{{ Request::is('users*') ? 'active' : '' }}">
<a href="{!! route('users.index', 2016) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
</li>
<li>
<a href="{!! route('users.index', 2017) !!}">
<i class="fa fa-circle-o"></i> YEAR 2017
</a>
</li>
When I hover over the link to the year 2016, the url will be like this :
localhost/mysystem/public/users?2016
My routes\web.php is like this :
Route::get('users/index?{year}', 'UserController');
Route::resource('users', 'UserController');
And my controller user is like this :
public function index(Request $request)
{
$year = $request->input('year');
echo $year;die()
}
There is exist error like this :
UnexpectedValueException in Route.php line 646: Invalid route action: [App\Http\Controllers\UserController]
Is there any people who can help me?
Your route should be as:
Route::get('users/index/{year}', 'UserController#index')->name('users.index.year');
Your controller as:
public function index(Request $request, $year)
{
echo $year;die()
}
Then you can use it in your view as:
{{ route('users.index.year', ['year' => 2016]) }}
Pass parameter in route like this:
Route::get('users/index/{year}', 'UserController#index');
View:
<a href="{!! route('users.index', ['year' => 2016]) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
Controller (get as argument inside controller method):
public function index($year)
{
echo $year;
die();
}
Or - If you want to pass a parameter as GET params to routes just do this:
Route::get('users/index', 'UserController');
and inside your <a href="">:
<a href="{!! route('users.index', ['year' => 2016]) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
The statement: {!! route('users.index', ['year' => 2016]) !!} will create a route like this: http://website.com/users/index?year=2016
and grab it in controller using request() helper like this:
public function index()
{
$year = request()->get('year');
echo $year;
die();
}
Hope this helps!
You will get it from method arguement
public function index(Request $request,$para_year)
{
$year = para_year;
echo $year;die()
}
and you route should be
Route::get('users/index/{year}', 'UserController#index');
Related
I added a button to change language.
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
class LanguageController extends Controller
{
public function __invoke(String $locale): RedirectResponse
{
app('session')->put('language', $locale);
app()->setLocale($locale);
return redirect()->back();
}
}
My routes:
<?php
use App\Http\Controllers\LanguageController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('auth.login');
});
Route::get('/language/{locale}', LanguageController::class)->name('locale');
require __DIR__.'/auth.php';
And I put this in my blade:
#if (app()->getLocale() == 'es')
<x-responsive-nav-link :href="route('locale', ['locale' => 'en'])">
{{ __('English') }}
</x-responsive-nav-link>
#else
<x-responsive-nav-link :href="route('locale', ['locale' => 'es'])">
{{ __('Spanish') }}
</x-responsive-nav-link>
#endif
I tested it changing the locale directly in the config/app.php file and it works.
Also tried to set the locale in the AppServiceProvider but session variable "language" is not set (even though it is on the controller).
How could I do this?
My version.Right now this is the best way for me.
Route:
Route::get('locale/{locale}', function ($locale) {Session::put('locale', $locale);return redirect()->back();})->name('locale');
HeaderController function index:
public function index(): Factory|View|Application
{
$data['languages'] = Language::where('status', '=', 1)->get();
//Get or Set current language
$get_default_lang = 'uk';
$default_lang = $get_default_lang ?? 'en';
if (session()->get('locale') == null) Session::put('locale', $default_lang);
//Get Current Language
$get_current_lang = Session::get('locale');
$session_language = Language::where('code', $get_current_lang)->first();
$data['current_language_name'] = $session_language['name'];
$data['current_language_image'] = url($session_language['image']);
return view('common/header', ['data' => $data]);
}
Header Blade:
<div class="dropdown">
<button type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="{{ $data['current_language_image'] }}" alt="{{ $data['current_language_name'] }}" style="max-width:20px;margin-right:5px;" />
{{ $data['current_language_name'] }}
</button>
<div class="dropdown-menu">
<div class="p-2">
#foreach($data['languages'] as $language)
<a href="{{ route('locale', ['locale' => $language->code]) }}" class="dropdown-item d-flex align-items-center justify-content-between">
<img src="{{ url($language->image) }}" alt="{{ $language->name }}" style="max-width:20px;" />
<span>{{ $language->name }}</span>
</a>
#endforeach
</div>
</div>
</div>
Language migration:
Schema::create('language', function (Blueprint $table) {
$table->bigIncrements('language_id');
$table->string('name', 32);
$table->string('code', 5);
$table->string('image')->nullable();
$table->tinyInteger('status');
$table->timestamps();
});
I wants to show users list into my dashboard pannel but it shows me this problem :
Too few arguments to function App\Http\Controllers\ProduitController::show(), 0 passed in C:\wamp64\www\Ecommerce\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
Controller File :
public function show($id)
{
$produit = Produit::find($id);
return view('produits', compact('produit'));
}
blade file :
#foreach($produits as $produit)
<div class="product__item__pic set-bg" data-setbg="{{ asset('img/uploads/'.$produit->image)}}">
<ul class="product__item__pic__hover">
<li><i class="fa fa-heart"></i></li>
<li><i class="fa fa-retweet"></i></li>
<li><i class="fa fa-shopping-cart"></i></li>
</ul>
</div>
<div class="product__item__text">
<h6>{{ $produit->designation }}</h6>
<h5>{{ $produit->prix_uni }} Mad</h5>
</div>
#endforeach
Route :
Route::get('/produits','ProduitController#show');
If you have an id in your function definition, this should also be there in route definition. Route variables is defined with {id}.
Route::get('/produits/{id}','ProduitController#show');
For a best practice, use model binding, if you call your route variable the same as the model, you can automatically load it by typehinting it.
Route::get('/produits/{produit}','ProduitController#show');
public function show(Produit $produit)
so i have a problem in my laravel application when i access to the route where i posted the json i get the 500 error so there is the source :
the route :
web.php
Route::get('loadmore', 'MoviesController#loadmore');
The controller :
public function loadmore(Request $request)
{
$movies_list = new Movies;
if ($request->has('page')) {
$movies_list = Movies::where('status', 1)->orderBy('id', 'DESC')->paginate(4);
}
$returnHTML = view('pages.ajax_movie', compact('movies_list'))->render();
return response()->json([
'html' => $returnHTML,
'page' => $movies_list->currentPage(),
'hasMorePages' => $movies_list->hasMorePages(),
])
}
and finally the view :
location : pages/ajax_movies.blade.php
#foreach($movies_list as $movies_data)
<a href="{{ URL::to('movies/'.$movies_data->video_slug.'/'.$movies_data->id) }}" class="movie">
<span class="r"><i class="i-fav rating"><i>{{$movies_data->imdb_rating}}</i></i></span>
<img src="{{URL::to('upload/source/'.$movies_data->video_image_thumb)}}" alt="" title="">
<span class="title">{{$movies_data->video_title}}</span>
<span class="ribbon ">
<span>{{$movies_data->video_access}}</span>
</span>
</a>
#endforeach
everyone can help me ?
thank you in advance
I am new to laravel, I have written a route
Route::resource('contract', 'ContractController');
Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController#data');
});
My controller file is :
public function data(Datatables $datatables)
{
$contracts = $this->contractRepository->getAll()
->get()
->map(function ($contract) {
return [
'id' => $contract->id,
'start_date' => $contract->start_date,
'end_date' => $contract->end_date,
'description' => $contract->description,
'name' => '',
'user' => '',
];
});
return $datatables->collection($contracts)
->addColumn('actions', '#if(Sentinel::getUser()->hasAccess([\'contracts.write\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/edit\' ) }}" title="{{ trans(\'table.edit\') }}">
<i class="fa fa-fw fa-pencil text-warning"></i> </a>
#endif
#if(Sentinel::getUser()->hasAccess([\'contracts.read\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/show\' ) }}" title="{{ trans(\'table.details\') }}" >
<i class="fa fa-fw fa-eye text-primary"></i> </a>
#endif
#if(Sentinel::getUser()->hasAccess([\'contracts.delete\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/delete\' ) }}" title="{{ trans(\'table.delete\') }}">
<i class="fa fa-fw fa-times text-danger"></i></a>
#endif')
->removeColumn('id')
->escapeColumns( [ 'actions' ] )->make();
}
When I am running with url contract/data then I am getting 404 not found error. In console I am getting error also
No query results for model [App\Models\Contract].
Please help me to resolve the issue
Just remove Route::resource('contract', 'ContractController'); or put that after Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController#data');
}); like so:
Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController#data');
});
Route::resource('contract', 'ContractController');
You get 404 on route /contract/data because the router is actually directed into contract/{contract} in ContractController#show from the upper routes on Route::resource('contract', 'ContractController');
I am trying to fetch some data from table in laravel 5.0 like so
public function index()
{
$data = DB::table('modules')->get();
return view("BaseView.home")->with('data',$data);
}
this is my view
#foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
#foreach($moduleCategories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
#endforeach
</div>
</li>
#endforeach
$module->id is obtained from another query result. Now when I try to run this I am getting Array to string conversion. Can someone point out the mistake. The expected output is > 1 in the sense there can be multiple categories names matching that condition.
The problem is that you are trying to put php logic inside an echo {{ ... }}. You are trying to echo out a collection or an array of data, hence the error you are getting, Array to string conversion. The proper way to do this is to do the logic in the controller. But for a quick fix, replace:
{!! $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get() !!}
to
<php $moduleCategories = DB::table('module_categories')->where('modules_id','=',$modules->id)->get(); ?>
Never use {{ ... }} or {!! ... !!} to put logic, those are to echo out a string.
==EDIT==
I suggest to use laravels eloquent relationship methods, this will simplify the code, and seperate the logic from the view.
Note: Expecting if you are using laravel naming convention.
On your Modules model, add a relationship to ModuleCategory like so:
public function module_categories()
{
return $this->hasMany('App\ModuleCategory');
}
On your controller, on the index method replace :
$data = DB::table('modules')->get();
with
$data = Modules::get();
and finally on the view, change it like so:
#foreach($data as $modules)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $modules->module_name }}<i class="plusMinus fa fa-plus-square plusMinusSpacing" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
#foreach($modules->module_categories as $category)
<a class="dropdown-item" href="#">{{ $category->categories_name }}</a>
#endforeach
</div>
</li>
#endforeach
As I commented on Carlos's solution, here is how I solved the same error...
//Controller
$users = User::where('blah', 'blah')->get();
return view('index', [
'users' => $users,
]);
//View
<script type="text/javascript">
angular.module("app").factory("DataService", function() {
return {
users: {!! $users !!},
};
});
</script>
As discussed above, this will result in the Array to string conversion error, since $users is an array.
However, this error won't happen if $users is a `collection'
I.e.
//Controller
$users = User::all();
return view('index', [
'users' => $users,
]);
//This is ok
Or,
//Controller
$users = collect([]);
return view('index', [
'users' => $users,
]);
//This is fine too