I have a problem in my project laravel, I want to get route parameter 'id_project' on route 'project' to use in route 'modulproject'. This route in one view.
This is my Route:
Route::get('project/{id_project}','ProjectDetailController#project');
Route::get('modulproject','ProjectDetailController#modulproject');
This is my Controller:
public function project($id_project)
{
$project=Project::where('id','=',$id_project);
return($project);
}
public function modulproject($id_project)
{
$modulproject=Modul::where('id_project','=',$id_project);
return($modulproject);
}
You can used Session to get that id_project.
use Session;
public function project($id_project)
{
Session::put('id_project',$id_project);
$project=Project::where('id','=',$id_project);
return($project);
}
public function modulproject()
{
if(Session::has('id_project')){
$modulproject=Modul::where('id_project','=',Session::get('id_project'));
Session::forget('id_project');
return($modulproject);
}
else{
return 'redirect to other page.. (custom)';
}
}
Related
First of all I have Book DB field and BookReview DB field
Book Model:
public function reviews()
{
return $this->hasMany(BookReview::class);
}
BookReview Model:
public function book()
{
return $this->belongsTo(Book::class);
}
What I need to do is to delete a BooksReview, but it consists in a relationship with Book field
This is the route
Route::delete('/books/{bookId}/reviews/{reviewId}');
And this is the controller:
public function destroy(int $bookId, int $reviewId, Request $request)
{
// TODO: implement
$check_bookReview = BookReview::firstWhere('id', $reviewId);
if ($check_bookReview) {
BookReview::destroy($id);
return response()->noContent();
} else {
abort(404);
}
}
I've never tried to delete data with a relation before, how do I accomplish this?
Thank you in advance
You can leverage Route Model Binding to have Laravel automatically attempt to find and return the relevant models from your database based on the URI parameters defined in your route and associated function:
Route::delete('/books/{bookId}/reviews/{reviewId}', [BookReviewController::class, 'destroy');
public function destroy(Request $requst, Book $bookId, BookReview $reviewId)
{
$bookId->delete();
return response()->json([], 204);
}
Laravel will automatically attempt to find and return the relevant Book and BookReview models from the database based on the value of each parameter. If it can't find the relevant model it will fail with a 404 but otherwise, the value of each parameter will be that of a model. Note that the name of the parameters defined in the Route and function definitions must match for route model binding to work.
Instead of:❌
public function destroy(int $bookId, int $reviewId, Request $request)
{
// #TODO implement
$check_bookReview = BookReview::firstWhere('id', $reviewId);
if ($check_bookReview) {
BookReview::destroy($id);
return response()->noContent();
}
else {
abort(404);
}
}
Use this: ✅
public function destroy(int $bookId, int $reviewId, Request $request)
{
if (BookReview::destroy($reviewId)) {
return response()->noContent();
} else {
abort(404);
}
}
\Illuminate\Database\Eloquent\Model::destroy($ids) returns a total count of destroyed models for the given IDs.
I want to redirect logged in users to the '/' route (like example.com, without anything behind it)
This works:
Route::get('/', Home::class)->name('home');
class Home extends Controller
{
public function __invoke()
{
if(Auth::check()) {
return view('dashboard');
}
else {
return view('welcome');
}
}
}
But now I need to add middleware to this route / controller.
Documentation suggests adding $this->middleware(['auth', 'verified']) to the __constructor in my Home controller.
This doesn't work because it also affects the view for guests (return view('welcome');)
I also tried:
Route::get('/', [Home::class, 'index'])->name('home');
class Home extends Controller
{
public function __construct()
{
$this->middleware(['auth', 'verified'])->only('dashboard');
}
public function index()
{
if(Auth::check()) {
$this->dashboard();
}
else {
$this->welcome();
}
}
public function welcome()
{
return view('welcome');
}
public function dashboard()
{
return view('dashboard');
}
}
But this doesn't work either. Any ideas?
I found this question when Googling for something similar, so I'm writing this for such people, sorry.
This looks like a very old version of Laravel that I'm not familiar with (and it's probably no longer supported), so I'll give my answer for Laravel 9.
To return different views, depending on whether they're logged in, use
// routes/web.php
Route::get('/home', function() {
if (Auth::check()) {
return view('dashboard');
} else {
return view('welcome');
}
});
If you have to use the result of some other policy and call controller methods, use
// routes/web.php
use App\Http\Controllers;
use Illuminate\Support\Facades\Gate;
Route::get('/settings', function () {
if (Gate::allows('manageSystem')) {
return (new Controllers\SettingsController)->index();
} else {
return (new Controllers\UserController)->settings();
}
});
I haven't had a change to test this, but I hope this helps someone
This is all my routes inside web.php
Route::get('/', function(){
return view('welcome-body');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('userdashboard')->middleware('auth');
// Route::post('logout', 'Auth\LoginController#logout')->name('logout');
// Route::get('logout', 'Auth\LoginController#logout');
Route::get('logout','Auth\LoginController#logout');
Route::group(['middleware' => ['guest']], function ()
{
// About dropdown pages route
Route::get('/office-mayor', 'WebController#officemayor_about_page');
Route::get('/organizational-chart', 'WebController#organizationalchartpage');
Route::get('/directory', 'WebController#directorypage');
Route::get('/barangays', 'WebController#barangayspage');
Route::get('/barangays-detail', 'WebController#barangays_detail_page');
Route::get('/history', 'WebController#historypage');
// Stories & News dropdown pages route
Route::get('/stories', 'WebController#storiespage');
Route::get('/stories-detail', 'WebController#storiesdetailspage');
Route::get('/news', 'WebController#newspage');
Route::get('/news-detail', 'WebController#newsdetailspage');
Route::get('/gallery', 'WebController#gallerypage');
Route::get('/videos', 'WebController#videospage');
Route::get('/bids-and-awards', 'WebController#bidsandawardspage');
// Transparency route
Route::get('/transparency', 'WebController#transparencypage');
// Covid-19 update route
Route::get('/covid-updates', 'WebController#covidupdatespage');
});
Route::group(['middleware' => ['auth']], function ()
{
Route::get('authors/table', 'AllSystemController#indexauthorsdata')->name('authors.table');
Route::get('/authors/add', 'AllSystemController#createviewauthors')->name('create.authors');
Route::post('/authors/authorsdata/submit', 'AllSystemController#submitauthors')->name('submit.authors.data');
Route::get('/authors/view/{id}', 'AllSystemController#showviewauthors')->name('view.authors.data');
Route::get('/authors/edit/{id}', 'AllSystemController#editviewauthors')->name('edit.authors');
Route::patch('/authors/{id}', 'AllSystemController#updateauthordata')->name('update.authors.data');
Route::get('stories/table', 'AllSystemController#indexstoriesdata')->name('stories.table');
Route::get('/stories/add', 'AllSystemController#createviewstories')->name('create.stories');
Route::post('/stories/storiesdata/submit', 'AllSystemController#submitstories')->name('submit.stories.data');
Route::get('/stories/view/{id}', 'AllSystemController#showviewstories')->name('view.story.data');
Route::get('/stories/edit/{id}', 'AllSystemController#editviewstories')->name('edit.stories');
Route::patch('/stories/{id}', 'AllSystemController#updatestorydata')->name('update.story.data');
Route::get('activities/table', 'AllSystemController#indexactivitiesdata')->name('activities.table');
Route::get('/activities/add', 'AllSystemController#createviewactivities')->name('create.activities');
Route::post('/activities/activitiesdata/submit', 'AllSystemController#submitactivities')->name('submit.activities.data');
Route::get('/activities/view/{id}', 'AllSystemController#showviewactivities')->name('view.activity.data');
Route::get('/activities/edit/{id}', 'AllSystemController#editviewactivities')->name('edit.activities');
Route::patch('/activities/{id}', 'AllSystemController#updateactivitydata')->name('update.activity.data');
Route::get('blogs/table', 'AllSystemController#indexblogsdata')->name('blogs.table');
Route::get('/blogs/add', 'AllSystemController#createviewblogs')->name('create.blogs');
Route::post('/blogs/blogsdata/submit', 'AllSystemController#submitblogs')->name('submit.blogs.data');
Route::get('/blogs/view/{id}', 'AllSystemController#showviewblogs')->name('view.blog.data');
Route::get('/blogs/edit/{id}', 'AllSystemController#editviewblogs')->name('edit.blogs');
Route::patch('/blogs/{id}', 'AllSystemController#updateblogdata')->name('update.blog.data');
Route::get('news/table', 'AllSystemController#indexnewsdata')->name('news.table');
Route::get('/news/add', 'AllSystemController#createviewnews')->name('create.news');
Route::post('/news/newsdata/submit', 'AllSystemController#submitnews')->name('submit.news.data');
Route::get('/news/view/{id}', 'AllSystemController#showviewnews')->name('view.new.data');
Route::get('/news/edit/{id}', 'AllSystemController#editviewnews')->name('edit.news');
Route::patch('/news/{id}', 'AllSystemController#updatenewdata')->name('update.new.data');
Route::post('/storiesgallery/submit', 'AllSystemController#submitstoriesgallery')->name('add.stories.galleries');
Route::post('/activitiesgallery/submit', 'AllSystemController#submitactivitiesgallery')->name('add.activities.galleries');
Route::post('/blogsgallery/submit', 'AllSystemController#submitblogsgallery')->name('add.blogs.galleries');
Route::post('/newsgallery/submit', 'AllSystemController#submitnewsgallery')->name('add.news.galleries');
Route::get('contact_us', 'AllSystemController#indexcontactus')->name('sending.mail');
Route::post('contact_us/submit', 'AllSystemController#submitcontactus')->name('sending.mail');
});
and this is my Controller WebController
public function __contruct()
{
$this->middleware('guest');
}
// TOP BAR PAGES
public function officemayor_about_page()
{
return view('web-routes.about.office-mayor');
}
public function organizationalchartpage()
{
return view('web-routes.organizational.organizational-chart');
}
public function directorypage()
{
return view('web-routes.about.directory');
}
public function barangayspage()
{
return view('web-routes.about.barangays');
}
public function barangays_detail_page()
{
return view('web-routes.about.barangays-detail');
}
public function historypage()
{
return view('web-routes.about.history');
}
public function storiespage()
{
return view('web-routes.stories.stories');
}
public function storiesdetailspage()
{
return view('web-routes.stories.stories-detail');
}
public function newspage()
{
return view('web-routes.news.news');
}
public function newsdetailspage()
{
return view('web-routes.news.news-detail');
}
public function gallerypage()
{
return view('web-routes.gallery.gallery');
}
public function videospage()
{
return view('web-routes.video.videos');
}
public function bidsandawardspage()
{
return view('web-routes.bids_awards.bids-and-awards');
}
public function officemayorpage()
{
return view('web-routes.office-mayor.office-mayor');
}
public function covidupdatespage()
{
return view('web-routes.covid-updates.covid-updates');
}
public function transparencypage()
{
return view('web-routes.transparency.transparency');
}
if i want to go in the page example /office-mayor its return like this
error:
This page isn’t working right now localhost can't currently handle this request.
HTTP ERROR 500
I already use config:cache, route:cache and dump-autoload but doesn`t work it
How can i fix this...
I think this is happening because you have misspelled constructor it should be like public function __construct() and you don't have to define middleware in your constructor because you have already used it in routes.
I want to redirect to index page having username or other things. Like a branch coordinator when do login , then he redirect to index/{branch_name}.
Please help me to do that.
My Route :
Route::get('/branch' , 'HomeController#index');
Controller :
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth:branch');
}
public function index()
{
return view('branch.index');
}
}
In web.php change to :
Route::get('/branch/{branch_name}' , 'HomeController#index');
In controller
public function index($branch_name)
{
//$branch_name will be containg the variable
return view('branch.index');
}
To redirect to above page
return redirect()->route('branch/'.$branch_name);
I have an application which needs to accomplish this in RouteServiceProvider.php:
//in \App\Providers\RouteServiceProvider.php
public function boot(Router $router)
{
//
parent::boot($router);
//somehow can get the current action
$action = $router->getCurrentAction();
if($action == 'edit'){
$router->model('articles','App\Article');
} else{
$router->bind('articles', function($id){
return Article::published()->findOrFail($id);
});
}
}
Here is my route:
Route::resource('articles', 'ArticlesController');
Here is my Controller:
public function show(Article $article){
return view('articles.show',compact('article'));
}
public function edit(Article $article){
return view('articles.edit',compact(['article','tags']));
}
The reason I want to do it because I want show action only shows published articles while edit action can change both published and unpublished articles.
if there is any better solution, please teach me. Thank you !
Why don't you make a middleware, that will work only for 'published' routes, where you will check if your Article is published?
Based in your route, I assume that you have the following paths:
example.com/edit
example.com/3 - article ID
So, you could use a pattern to bind the article:
public function boot(Router $router)
{
parent::boot($router);
// bind
$router->bind('article', function($id){
return Article::published()->findOrFail($id);
});
// id pattern
$router->pattern('id', '[0-9]+');
}
Now, just declare the respective routes:
Route::get('articles', 'ArticlesController#showAll');
Route::get('{id}', 'ArticlesController#edit');
Finally, your controller could looks like this:
class ArticlesController{
public funcion showAll(){
// show a list of articles
}
public function edit(Article $article){
// selected article
}
}
Try to get the best of Laravel using class injection and keeping cleaning code.