Actually I want to display admin announcement or any notification on home page so when ever any user will be login with credentials detail so he or she can see announcement or notification..
please help me step by step , if possible with code logic, please do for me thank you very much
as you requested for full step by step process try this
first make form in your admin panel like this
<form method="post" action="{{ route('announcement') }}">
<label>Enter Your Announcement</label>
<textarea class="form-control" name="text">
</textarea>
<select name="active_status">
<option value="0">Deactive</option>
<option value="1">Active</option>
</select>
</form>
open your route/web.php make post method
Route::post('/announcement', [
'uses' => 'AdminController#postAnnouncement',
'as' => announcement
]);
if you have AdminController and its okay if not make it with these commands
php artisan make:controller AdminController
now you can add your function for save your announcement to database
public function postAnnouncement(Request $request){
$announcement = new Announcement;
$announcement->text = $request->text;
$announcement->active = $request->active_status;
$announcement->save();
return back()->with('status', 'Announcement Posted Success');
}
add use App\Announcement; in top of your controller
now you need to make announcement table and model
php artisan make:model Announcement -m
it will generate 2 file model & migration
go to database/migration folder and add this line to announcement migration after
$table->increments('id');
$table->string('text');
$table->boolean('active');
your table is ready to migrate now
php artisan migrate
now you can show in your homepage like this
first goto your homecontroller & add these lines
$announcements = App\Announcement::where('active', true)->get();
return view('home', compact('announcements'));
in your home.blade.php file
//Only for Authenticated Users
#auth
#foreach($announcements as $announcement)
<p>{{ $announcement->text }}</p>
#endforeach
#endauth
//Only for Guest Users
#guest
#foreach($announcements as $announcement)
<p>{{ $announcement->text }}</p>
#endforeach
#endguest
You can do it simply with the help of built-in user model.
First, check if the user is authorized and after that, you can show the announcement section.
All this should be done in the view of your page. you can do something like this
#if (Auth::check())
//show authorized content (Announcements etc.)
#else
//show unauthorized content
#endif
Or you can also do this in laravel 5.6
#auth
// The user is authenticated...
#endauth
#guest
// The user is not authenticated...
#endguest
Also do some research for this there are plenty of examples there on the Internet.
Try This
#auth
// For Logged in users only
<h1> You Are Loged In</h1>
#endauth
#guest
// For Guest Users
<h1> You Are Guest </h1>
#endguest
#auth
//if user logged in
<h1> You Are Loged In</h1>
#else
else guest
<h1> You Are Guest </h1>
#endauth
Related
I have domain for example aaa.com. And I deploy Laravel on my webhost succesfully. Login, pages, all things etc. works fine. but whenever I logout it redirects me to 127.0.0.1 not aaa.com. Of course, I have to point out that I am using Laravel Breeze
and here is what I wrote:
my logout form inside any page.
<form method="POST" action="{{ route('logout') }}">
#csrf
<button type="submit" class="underline text-sm text-gray-600 hover:text-gray-900">
{{ __('Log Out') }}
</button>
</form>
My web.php include require __DIR__.'/auth.php'; . Does not contain any logout redirects.
and inside auth.php
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
and finally AuthenticatedSessionController.php
public function destroy(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
I don't understand why I am being redirected to 127.0.0.1 instead of aaa.com?
Edit:
and forgot to mention my .env file include
APP_URL=https://aaa.com
In .env file, Change APP_URL
APP_URL=http://aaa.com
Thank you to everyone who replied. It fixed itself the next day. It must be something left in the web host's cache, or your browser's. It's not a Laravel thing.
I'm trying to create a ticket management system with laravel jetstream and livewire. In the user page there's a table with all tickets that the user created. When the user clicks on the button to open one specific ticket, it should pass the id of the ticket and redirect to another page where it receives the data of that ticket he clicked, like title, message, etc..
The id is passed through the url, but my main problem is that whenever I try to display that data in the view, nothing shows, no errors either. I think that something might be wrong with my controller.
Here's my route:
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers']);
The button to redirect to that specific ticket:
<a href="{{ url('tickets.answers' . '/'. $ticket->id ) }}" > <x-jet-secondary-button >
See Answer
</x-jet-secondary-button></a>
AnswersController:
public function render(Request $request)
{
$tickets = Ticket::where('id', $request->url('id'));
return view('livewire.tickets.answers', [
'tickets' => $tickets,
]);
}
And how I'm trying to display in my blade:
#foreach($tickets as $key => $ticket)
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
Ticket nÂș {{$ticket->id}} - {{$ticket->title}}
</h3>
</div>
</div>
#endforeach
In your TicketsController you can fetch the id like this
public function answer(Request $request, int $id)
{
// Use the find() method, instead of where(), when searching for the primary key
$tickets = Ticket::find($id);
// .. more
}
In your routes files you specify an answer method, so use this in your TicketsController.
// See how to name a route
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers'])->name('tickets.answers');
Then use the named route in your view like this:
<a href="{{ route('tickets.answers', ['id' => $ticket->id]) }}">
You can see a similar example in the Laravel docs.
I'm making a College Administration website where a professor can log in.
I have a dashboard, where my dynamically generated button should be placed: (right now it just has dummy buttons!)
Generated by this view file, which I will have to modify soon:
<div class="container d-flex flex-column align-items-center justify-content-center">
<h1>IA DASHBOARD</h1>
<br>
<div class="grid2">
SUBCODE 1</button>
SUBCODE 2</button>
SUBCODE 3</button>
</div>
Tables in the Database:
the table iamarks contains the data (student info, and marks) that is to be displayed after /subcode/{subcode} narrows it down to records of just the students that are in the class assigned to current logged-in professor.
classroom_mappers is a table used to map a professor to a classroom with a subject. It makes sure that one classroom only has one professor for a particular subject.
the routes currently in my web.php:
route::get('/ia', 'IAController#show')->middleware('auth');
Route::get('/subcode/{subcode}', 'IAController#showTable')->middleware('auth');
...and these are the methods inside my controller:
//shows buttons to the user:
public function show(){
$subcodes = DB::table('classroom_mappers')
->select('subcode')
->where([['PID','=', auth()->user()->PID]])
->get();
return view('ia',compact('subcodes'));
}
//when user clicks a button, subcode is to be generated and a table is to be shown:
//it works, I tried it by manually typing in subcode value in URL.
public function showTable($subcode){
$sem = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['subcode','=',$subcode]])
->pluck('semester');
$division = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['semester','=',$sem],
['subcode','=',$subcode]])
->pluck('division');
$data = DB::table('iamarks')
->where([['semester','=',$sem],
['division','=',$division],
['subcode','=',$subcode]])
->get();
return view('subcode',compact('data'));
}
My Problem:
To be able to generate the {subcode} in the URL dynamically, I want to create buttons in the dashboard using the data $subcodes. The controller hands over the $subcodes (an array of subject codes which belong to logged in professor) which are to be made into buttons from the show() method.
The buttons should have the name {subcode} and when clicked, should append the same subject code in the URL as {subcode}.
How do I make use of $subcodes and make the buttons dynamically?
How do I make sure the buttons made for one user are not visible to another user?
I managed to find the solution, thanks to Air Petr.
Apparently, you can't nest blade syntax like {{some_stuff {{ more_stuff }} }} and it generates a wrong php code. I modified the solution by Air Petr to:
<div class="grid2">
#foreach ($subcodes as $subcode)
<a href="<?php echo e(url('/subcode/'.$subcode->subcode));?>">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">
<?php
echo e($subcode->subcode);
?>
</button>
</a>
#endforeach
</div>
It generates the buttons perfectly. The buttons for one user are not visible to another, since I'm using PID constraint in a query (['PID','=', auth()->user()->PID]).
Pass the passcodes array to view:
$subcodes = []; // Array retrieved from DB
return view('subcode', compact('subcodes'));
And in subcode.blade.php, loop through each subcode:
<div class="grid2">
#foreach($subcodes as $subcode)
<a href="{{ url('/subcode/' . $subcode->subcode) }}">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">SUBCODE {{ $subcode->subcode }}</button>
</a>
#endforeach
</div>
You can loop your codes to create buttons. Something like this (it's for "blade" template engine):
<div class="grid2">
#foreach ($subcodes as $subcode)
{{ $subcode->subcode }}</button>
#endforeach
</div>
Since you're using PID constrain in a query (['PID','=', auth()->user()->PID]), you'll get buttons for that specific PID. So there's no problem.
I want to delete a record but I haven't been successful, apparently my code is wrong. Solutions i came across say i have to use a post in my form method and add the method_field helper. This would mean my view having a form in it, i want to avoid this if possible. Is it then possible to do my delete another way. Below is my code
snippet of my view
<div class="backbtn">
<a class="btn btn-savvy-delete" href="/tasks/{{$task->id}}" data-toggle="tooltip" title="Delete"><i class="fa fa-trash-o" aria-hidden="true"> Delete</i></a>
</div>
<div class="panel-body">
<p><strong>Owner:</strong> {{ ucfirst($task->employee->firstname) }} {{" "}} {{ ucfirst($task->employee->lastname) }}</p>
<p><strong>Task:</strong> {{ $task->title }}</p>
<p><strong>Description:</strong> {{ $task->description }}</p>
</div>
TaskController
public function destroy($id)
{
Task::destroy($id);
Session::flash('status', "Task was successfully deleted.");
return redirect('/tasks');
}
web.php
Route::delete('/tasks/{id}', 'TaskController#delete');
Im not sure what error you are getting, but i can point out a few things. For one use Route::get instead of ::delete, you are calling it via a link not a form method.
Secondly to delete follow what the laravel doc says here eg.
$task = App\Task::find(1);
$task->delete();
I'm currently trying to find out if you can add a Gate Fascade to a Policy or if there's a better way to handle my situation.
I have a list of users with each having ONE role and each role can have MANY permissions. All of this is stored in my database with the correct relationships established in my models.
I am trying to show or not show a delete icon based on whether a user can delete another user in my HTML list of users.
Example:
Lets say user1 has a role of 3 which is a site-admin. Site admins have the permission to delete users, however they are NOT able to delete other users who have the same role as them or a role higher than theirs.
When user1 accesses the /users uri they are shown the HTML table of users in the database and as the last table column are the actions that can be performed on the row for that user row. The available action icons are edit and delete. For the delete icon I want a policy to be ran to make sure than the authenicated user can delete users first but also pass the current row's user object and see if the user has the same role id or higher in which case it will NOT display that icon.
<?php
namespace App\Policies;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class UserPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* #return void
*/
public function __construct()
{
//
}
public function delete(User $user) {
return Auth::user()->role->permission and $user->role_id < Auth::user()->role_id and $user->id != Auth::user()->id;
}
}
/reources/views/partials/tables/actions.blade.php
<td class="actions">
<i class="icon wb-edit" aria-hidden="true"></i>
<i class="icon wb-eye" aria-hidden="true"></i>
#can('delete-user', $user)
<form class="inline" method="POST" action="{{ route('users.delete', [$user->id]) }}">
{{ method_field('DELETE') }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-sm btn-icon btn-pure btn-default on-default" data-toggle="tooltip" data-original-title="Delete"><i class="icon wb-trash" aria-hidden="true"></i></button>
</form>
#endcan
</td>
Rewrite your policy method like this (I used your own syntax and assumed it's true):
public function delete(User $authUser, User $user) {
return $authUser->role->permission and
$user->role_id < $authUser->role_id and
$user->id != $authUser->id;
}
In your policy methods, first argument is always your authenticated user, and other arguments are what you pass through methods like #can or Gate::allow.