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');
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();
});
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 create edit page to edit the form. When I debug it. It's showing error which is Missing required parameters. I already try many ways but i can't solve it. Anyone can help on this?
<td class="text-right">
<a href='{{ route("email.edit",["id"=>$mailTemplate->id]) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
route file
Route::get('api/email/create', ['as' => 'email.create', 'uses' => 'Havence\AutoMailController#create']);
Route::get('automail/mail', 'Havence\AutoMailController#mail');
Route::get('automail/index',['as'=>'email.index','uses' => 'Havence\AutoMailController#index']);
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Route::get('automail/delete',['as'=>'email.delete','uses' => 'Havence\AutoMailController#destroy']);
Controller
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit')->with('mailTemplates', $mailTemplates);
}
You could do the following:
<td class="text-right">
<a href='{{ route("email.edit", $mailTemplate) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
The route:
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Then in the controller you can get it:
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit', compact('mailTemplates'));
}
I am trying to fetch data using datatable in laravel. My
view is :
<script type="text/javascript">
var oTable;
$(document).ready(function () {
oTable = $('#data').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"columns":[
{"data":"start_date"},
{"data":"end_date"},
{"data":"client_id"},
{"data":"created_at"},
{"data":"actions"}
],
"ajax": "{{ url('contract') }}" + ((typeof $('#data').attr('data-id') != "undefined") ? "/" + $('#id').val() + "/" + $('#data').attr('data-id') : "/data")
});
});
function deleteClient(id){
}
</script>
public function data(Datatables $datatables)
{
$contObj = new Contract;
$conttt = $contObj->get()
->map(function ($conttt) {
return [
'id' => $conttt->id,
'start_date' => $conttt->start_date,
'end_date' => $conttt->end_date,
'client_id' => $conttt->user_id,
'created_at' => $conttt->created_at
];
});
return $datatables->collection($conttt)
->addColumn('actions', '#if(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
<a href="{{ url(\'contract/\' . $id . \'/show\' ) }}" title="{{ trans(\'table.details\') }}" >
<i class="fa fa-fw fa-eye text-primary"></i> </a>
')
->removeColumn('id')
->rawColumns(['actions'])->make();
}
but when I am getting this data as :
"data":[{"start_date":null,"end_date":null,"client_id":"3","created_at":{"date":"2018-08-07 10:13:46.000000","timezone_type":"3","timezone":"UTC"},"actions":" <a href=\"http:\/\/billing.mj.milesweb.cloud\/files\/public\/contract\/1\/edit\" title=\"Edit\">\n <i class=\"fa fa-fw fa-pencil text-warning \"><\/i> <\/a>\n <a href=\"http:\/\/billing.mj.milesweb.cloud\/files\/public\/contract\/1\/show\" title=\"Show\" >\n <i class=\"fa fa-fw fa-eye text-primary\"><\/i> <\/a>\n "}
When I see this data in datatable then I see in field created_at the data shown there is [object Object] while in json it is OK. Please help me to show the proper data in datatable
It's because created_at is a Carbon object and you need a string. For 'created_at', try returning the following:
\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $conttt->created_at)->format('m/d/y h:i A')
By default created_at and updated_at are the carbon class object. You need to convert it into your own format:
$conttt = $contObj->get()
->map(function ($conttt) {
return [
'id' => $conttt->id,
'start_date' => $conttt->start_date,
'end_date' => $conttt->end_date,
'client_id' => $conttt->user_id,
'created_at' => $conttt->created_at->format('d-m-Y')
];
});
You also can write this code into Your model class. For that you need to user Laravel mutators.
Good Luck!!!
you can editColoumn in datatable like this:
return Datatables::of($conttt)
->editColumn('created_at', function($conttt) {
return $conttt->created_at->format('d M Y H:i:s');
})
->make(true);
try it
1)
$contObj = new Contract;
$conttt = $contObj->get()
->map(function ($conttt) {
return [
'id' => $conttt->id,
'start_date' => $conttt->start_date,
'end_date' => $conttt->end_date,
'client_id' => $conttt->user_id,
'created_at' => date('Y-m-d h:s:i', strtotime($conttt->created_at))
];
});
2)
$contObj = Contract::all();
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');