Check if resource exists before rendering full page component on Livewire - laravel

How can I check if resource exists prior to rendering full page component with Livewire? This is pretty simple with the Laravel Controller.
My route:
Route::get('/profiles/{id}', \App\Http\Livewire\Profiles\Manage::class)->name('profiles.manage');
I was the Profiles\Manage class to check whether profile exists or not before rendering the full page component.
I am mounting the profile data using the mount function within the component and trying to check if the profile exists or not (and redirect accordingly). However, the redirect doesn't work.
public function mount($id)
{
$this->profile = Profile::where(['user_id' => Auth::user()->id, 'id' => $id])->first();
if(!$this->profile) {
return redirect()->to('/404');
}
}
I've also tried doing this within the render() method where the component view is returned but that method requires a livewire component to be rendered.

As Qirel said, I think that $this->profile does exist with empty value, so maybe you could try with findOrFail in your mount method:
$this->profile = Profile::findOrFail($id);

Try this:
if(!$this->profile) {
abort(404);
}
Or you can use this:
abort_if(!$this->profile, 404);

Related

Error showing when Delete image in laravel

I want to delete images that I store in server,
I store images like this
$image1 = $postData['img']['0']->store('public');
$Add->Img1 = str_replace('public/', '', $image1 );
images save in public/storage folder
I display images like this
<img src="{{ asset('/storage/'.$add->Img1)}}">
so I need to delete this image using a tag like this
Remove
this is my route
Route::get('/deleteImg/{id}', 'AlladdsController#DeleteImg')->name('deleteImg');
this is my controller for delete images
public function DeleteImg (Request $request, alladds $alladds)
{
$img= request('id');
if(Storage::delete('/public'.'/'.$img)) {
return 'file is deleted';
}
else {
return 'file is not deleted';
}
return redirect()->back();
}
but this code is not working, what I want to do correct this code
Your route is injecting the id into the method DeleteImg(), but you have a different field catching the injected id.
This routing:
Route::get('/deleteImg/{id}', 'AlladdsController#DeleteImg')->name('deleteImg');
pushes id into the method as the argument after $request.
I don't know what alladds is, and it doesn't seem to be used, so I suggest following Laravel convention and re-write the method input like so:
// Note lower case to match route method and std.
public function deleteImg (Request $request, $img){ ... }
This will inject whatever you are sending into the route right into the method. This will fix the mismatch error (if that was even an error -- not sure as you didn't say what the exact issue was).
Also - note you are calling the asset from storage directory and then trying to delete the image from public- these are two different places, and may well be the cause of the error - perhaps one of these locations is incorrect and thus you are trying to delete (or call) from an area where it doesn't exist.

How to render a cms page with default theme AND variables from controllers in OctoberCMS?

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.
If I have the following route:
Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController#bransje'
]);
And in my controller CPController ive tried several things, like I used to with Laravel:
public function bransje() {
$stuff = Stuff::with('info');
return View::make('cms::bransje')->with('stuff',$stuff);
}
But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:
public function onRun()
{
$this->eventen = $this->page['stuff'] = $this->stuff();
}
protected function stuff()
{
return ...
}
Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried
return View::make('my-theme-name::page');
and a lot of variants but no luck.
I know I can also do a:
==
public function onRun()
{
}
in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.
You can bypass frontend routing by using routes.php file in your plugin.
Full example in this video turotial.
If this answer can still be useful (Worked for October v434).
I have almost the same scenerio.
What I want to achieve is a type of routing like facebook page and profile.
facebook.com/myprofile is the same url structure as facebook.com/mypage
First I create a page in the CMS for each scenario (say catchpage.htm)
Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.
if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {
// Last fail over for looking up slug from the database
Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
//Pretend this are our routes and we can check them against the database
$routes = ["bola", "sade", "bisi", "ade", "tayo"];
if(in_array($slug, $routes)) {
$cmsController = new Cms\Classes\Controller;
return $cmsController->render("/catchpage", ['slug' => $slug]);
}
// Some fallback to 404
return Response::make(View::make('cms::404'), 404);
});
}
The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.
Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.
You may put all these in a controller though.
If anyone has a better workaround, please let us know.

How to get controller action by passing URL in laravel

I searched more time to find how to get the controller method name by passing the URL but not found my expected answer. I want to make a method where I will pass a URL and it will give the corresponding controller action like as below but I can't figure out.
I found a helper which just return the current URL's action which is Route::currentRouteAction()
If a route in my application like as Route::get('/abc', 'YourController#method') which will generate the url http://example.com/abc
then how can I get the YourController#method by passing http://example.com/abc
function getAction($url){
//what will be logic?
// return like App\Controllers\MyController#method
}
I have to make a custom permission system where I need it for show and hide the menu by checking the URL of each menu.
Within your controller you can do the following:
<?php
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
public function index(Request $request, Router $route)
{
$action = $router->getRoutes()->match($request)->getActionName();
// action should be what you're looking for.
}
You can try this if you want to:
Route::get('/the/url', 'YourController#method');
Every time anything calls the URL in the route, your method will be called.
You don't need to navigate to that url to call your method, it could be called by a form action, or a buttons action and just execute your method.
Edit:
url is your url as parameter (plain route)
import this:
use Illuminate\Routing\Route;
this is your function:
public function method(Route $route, $url)
{
$routes = \Route::getRoutes()->getRoutes();
foreach($routes as $r){
if($r->getUri() == $url){
$youraction= $r->getActionName();
dd($youraction);
}
else{
dd('does not exist');
}
}
}
Tested.

How to share object in blade template across Zizaco/Confide routes?

I'm trying to share an object across a Laravel application. I need this because I want to create a blade template which will be included everywhere and will also perform some logic/data manipulation (a dynamic menu sort of speak).
To be able to accomplish this I've created a constructor in the Base controller and used View::share facade.
While this works across all routes in the application, it's not working for Zizaco/Confide generated routes, where I get Undefined variable error for $books.
This is the constructor in the base controller:
public function __construct()
{
$books = Book::all();
View::share('books', $books);
return View::make('adminMenu')->with('books', $books);
}
What you need are View Composers!!
You can hook a view composer to a certain view name or pattern (using * wildcard). Every time before that view gets rendered the view composer will run.
You can put this anywhere. Most elegant would be a custom app/composers.php which is then required at the bottom of app/start/global.php
View::composer('adminMenu', function($view){
$books = Book::all();
$view->with('books', $books);
}

Laravel Trying to get property of non-object but not sure why

I want to grab some data from a database and display on a layout page, I've basically started building a small CMS to get into Laravel and all has gone fine so far but now i'm at a wall, and can't find a solution.
I have a layout blade file like so: http://paste.laravel.com/1fB1 nothing majot but you will see i have used $page->meta_title etc in there and in my controller i have:
public function home()
{
$pages = Pages::all();
return View::make('frontend/home')->with('pages',$pages);
}
Which I have a pages model doing nothing else really like so:
class Pages extends Eloquent {
protected $table = 'pages';
}
So why is it trying to get property of non-object and I don't really want to use a foreach because this is going to be the frontend of my 'test' website so a foreach wouldn't suite.
You'll need to access these items as a multi-dimensional array if you don't want to loop through them.
$pages[0]['field_name_here']
or
$pages[1]['field_name_here']
Its a bit of a tough one to answer without knowing how you want your CMS to work.
For example, you could have a route as {pagename} in your routes.php file, then have a page controller where you would get the requested route from the variable passed in. This would then load the page you wanted using the variable
public function page( $pagename ) {
$page = Page::where('page_title', '=', $pagename)->first();
View::make('frontend/page', array( 'page' => $page ));
}
Using a route like that, and the controller, in your view you could use {{ $page->content }} to get the content of the requested page from the database and display it.
Hope this helps.
Edit: Example Route:
Route::get('{pagename}', 'PageController#page');

Resources