Can't navigate the page in laravel project - php

I have created addnewemployee.blade.php in the views folder. In routes, I have mentioned
Route::get('/addnewemployee', function() {
return view('addnewemployee');
});
As well as I have given the path in my enter code herehome page like
<a class="btn cus-button" href="{{url('/addnewemployee')}}"> Add New </a>
Once I click the add new button, 'addnewemployee' is added with my URL but it shows 404 not found.
Can anyone please help me?

The addnewemployee.blade.php should be in hte resources/views/addnewemployee.blade.php in order for it to work.

Try the named route:
Route::get('/addnewemployee', function() {
return view('addnewemployee');
})->name('addnewemployee');
<a class="btn cus-button" href="{{ route('addnewemployee') }}"> Add New </a>

Related

Laravel: href with route in jQuery

I'm trying to create href with route laravel. This route contains a parameter, but it always returns Error 404. I don´t know what I´m doing wrong.
I know that my question is very easy, I´m searching in Google for this, but I think that I'm doing well on my route.
My route:
/** SHOW EVENT INFO */
Route::get('event/{url}', 'Web\EventController#showEvent');
My href in file.js:
<a target="_blank" id="button-event-read" href="'+config.url.base_url+'event/'+eventUrl+'" class="btn color2-bg float-btn">Leer más.<i class="fal fa-angle-right"></i></a>
If I click in href, it goes to the correct route but ends up showing Error 404.
My controller:
public function showEvent($url){
echo "here";
}
you can use route name
add a name to your route:
/** SHOW EVENT INFO */
Route::get('event/{url}', 'Web\EventController#showEvent')->name('MyRouteName');
and use the route name in href:
<a target="_blank" id="button-event-read" href="{{route('MyRouteName', 'MyParameter')}}" class="btn color2-bg float-btn">Leer más.<i class="fal fa-angle-right"></i></a>
Get Host url in variable
var url = window.location.protocol + "//" + window.location.host;
and then use it in href like this.
<a target="_blank" id="button-event-read" href="'+url+'/event/'+eventUrl+'" class="btn color2-bg float-btn">Leer más.<i class="fal fa-angle-right"></i></a>

Not found page when clicked detail to get data based on id

I have been fixing this trivial problem a few days, but it didn't solve either.
When I click the detail button on the table, it will display a 404 or not found message.
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik');
}
Route
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail']);
Blade
<a class="btn btn-success btn-sm" href="{{ url('admin/lirik-lagu/detail', $data->id) }}">Detail</a>
I will try to fix what i think it's wrong in your code.
First you should give the $data to your blade file
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik',['data'=>$data]);
}
Second, your should omit the $ in your Route id parameters
Route
Route::get('lirik-lagu/detail/{id}', [LirikLaguController::class, 'detail']);
Third, when you generate the url url(...) it seems to have an admin prefix but not your route declaration.
Blade
<a class="btn btn-success btn-sm" href="{{ url('lirik-lagu/detail', $data->id) }}">Detail</a>
You are not passing the variable to the view. This is how you do it:
return view('admin.detail-lirik', $data);
If you need to pass multiple variables you can pass an array in the second paramter like this:
return view('admin.detail-lirik, ["varname" => $var]);
I see several problems here, so I'm going to point them out. First one, you never returned the data to your view:
return view ('admin.detail-lirik', $data);
The other is the URL in your blade file. You called admin/lirik-lagu/detail but you defined route without admin in your web.php file. You can remove the admin from your url, or create a name for your route and call it that way:
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail'])->name('lirik-langu');
And then use it like this
<a class="btn btn-success btn-sm" href="{{ route('lirik-langu', $data->id) }}">Detail</a>

How to populate view with database information after AJAX request

Hi there once again SO community. I've been developing a site and so far it's going pretty well. But today after a long day searching for a solution I can't understand nor find what the right path is...
I want to click on a button and a profile page where you can edit the fields appear. I can redirect to the page I want but I don't know how to send the user data so I can populate the fields.
Here is my button code on my view
<button class="btn btn-xs btn-warning dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false" style="border-color: black" id="dados_{{ $user->username }}"> Alterar Dados Pessoais
<i class="glyphicon glyphicon-cog"></i>
</button>
Here is the button AJAX request handler
if((this.id).indexOf("dados") != -1){
var content = this.id.replace("dados_", "");
$.get('callPermissions', {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
And here is my callPermission Controller
public function callPermissions(Request $request)
{
if($request->ajax()){
$usernames = Input::get('usernameSend');
if(isset($usernames)){
$user = User::Where('username', '=', $usernames)->first();
$returnHTML = view('userOptions.settings')->render();
return view('userOptions.settings');
}else{
Log::warning("Username não existe na base de dados.");
}
}
}
and here my Settings Controller
public function settings(Request $request)
{
return view('userOptions.settings');
}
And here is the route
Route::get('/callPermissions', 'SidebarController#callPermissions');
I know the controller is wrong and from what I've read I should verify if the AJAX is successful and if it is handle i on the AJAX request. But from what I've understand I'm not using the Controller at all (even though it goes there). How should I send the user information (in this case the username, then I can get everything from the database) and then send it to the view? I've been searching and trying out stuff that doesn't work...since the return view("your_view") on the Controller doesn't work.
Sorry if I've been confusing and if you need additional information feel free to ask!
Thanks for your help!!
Edit: If I return this on the controller
return view('userOptions.settings', compact('user'));
and do a replace with the Ajax request as show above and add this to the settings view
<p> {{ $user->name }} </p>
I get the following error Undefined variable: user (View: C:\wamp64\www\siteXL\ideiasxl\resources\views\userOptions\settings.blade.php)
Is there anyway to send the parameters with a compact alike or I need to send it through the link? Was avoiding to show the username on the url.
Edit2: For further clarification, this works as intended
<button onclick="window.location='{{url('/settings/' . $user->username)}}'" type="button" id="dadosPessoais" class="btn btn-default">Alterar Dados Pessoais
<i class="glyphicon glyphicon-wrench"></i>
</button>
but I was trying not to send id's and usernames through the URL.
If this is not achievable it's ok, but if there's a way I can't find it, that's why I'm asking
I think you have to add a parameter in the Route and receive the data in the controller function. I'd do something like this:
Route:
Route::get('/callPermissions/{user}', 'SidebarController#callPermissions');
Controller:
public function callPermissions(Request $request, $user)
{
//get data related to $user
}
Ajax call:
$.get('callPermissions/'+userIdVariable, {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
This would send the user id through the route.
To get the user id with JavaScript, you can make a hidden field in the Blade file and set the user id as the value. For example, if you using Form helper:
{{ Form::hidden('user_id', $user->id, array('id' => 'js-user-id')) }}
And then, in the JavaScript, you can get the value using something like this:
var userIdVariable = $('#js-user-id')->val();

Laravel sessions with sweet alert Session Loops

I'm just confused about my code. But I really thought that my code is correct. I'm trying to use with() method in Laravel 5.1 and then return to a view, then the sweet alert appears if the session that has been set is exists. Please see my code below:
PageController.php
return redirect()->route('list.view')->with('sweetalert', 'List has been created!');
view.blade.php
#extends('layout.master')
#section('container')
#foreach($lists as $list)
<li>{{ $list->name }}</li>
#endforeach
#stop
master.blade.php
<div class="container">
// some markup here...
</div>
#if(Session::has('sweetalert'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
#endif
I only want it to appear once, but if I try to click the back button, the message appears again. I have also tried the ff. code but nothings change:
#if(Session::has('sweet'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
<?php Session::forget('sweetalert'); ?>
#endif
Little help here?
a flash message has to be trigerred otherwise it will not make sense as you will set it everytime for the view
however you can use this code wherever the trigger is
Please Note :- I am just trigerring it everytime
Route::get('/', function () {
session()->flash('testing', 'I see this'); // Please have this line inside the trigger so the session does not get created everytime the view is called
return view('welcome');
});
Hope this helps

Incorrect routing with laravel

I have a routing problem with a laravel application.
I have the following route:
Route::get('home', array
('as' => 'home',
'uses' => 'HomeController#getHome'
));
which leads to the following url:
192.168.2.22/laravel/public/home
all is fine with this.
Then I have a second route like this:
Route::get('users/{id?}', function($id) {
$user = Users::find($id);
return View::make('Account.profile')->with('user', $user);
});
which leads to the following url:
192.168.2.22/laravel/public/users/1
Now when I click the Home link again from this page I get :
192.168.2.22/laravel/public/users/home
which of course gives me an error.
How can I fix this?
Using either {{ URL::to('home') }} (the route's URL) or {{ URL::route('home') }} (the route's name) as your link's href attribute will solve this.
i.e.:
<a class="navbar-brand" href="{{ URL::route('home') }}">home</a>
Don't use handmade links...
do it like this:
<a class="navbar-brand" href="<?php echo link_to_route("home");?>">home</a>
this will work if you use php OR blade but i would recommend to do it like that for blade:
<a class="navbar-brand" href="{{link_to_route("home")}}">home</a>
Use some Laravel Helpers to be sure your links are always valid:
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());

Categories