Laravel - Route [companies] not defined - laravel

I developed a web application with Laravel-5.8 as shown below:
<?php
namespace App\Http\Controllers\Organization;
use App\Http\Controllers\Controller;
use App\Models\Organization\Company;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
class CompaniesController extends Controller
{
public function index()
{
$companies = Company::paginate(25);
return view('organization.companies.index', compact('companies'));
}
}
The CompaniesController is in a folder called Organization while the companies view is in a folder called organization.
routes/web.php
Route::get('/companies','CompaniesController#index');
navbar is as shown below:
<li class="nav-item">
<a href="{{ route('companies') }}l" class="nav-link">
<i class="nav-icon far fa-image"></i>
<p>
Company Info.
</p>
</a>
</li>
When I clicked on the navigation sidebar, I suppose to see the companies index being displayed but I got this error:
Route [companies] not defined
How do I resolve it?
Thanks

You're referencing an undefined named route. Try:
Route::get('/companies','CompaniesController#index')->name('companies');
This should solve your problem. You are having the error because you're referencing a named route which is not defined yet.
See the docs for more information.

Maybe you forgot to name your route?
Route::get('/companies','CompaniesController#index')->name('companies');

You forgot to name your route:
Route::get('/companies','CompaniesController#index')->name('companies');

Related

Laravel Relationship not working with belongsTo

Hello Guys, I am just passing my query to notification blade, but its gave error. I dont know what i did wrong with bellow code. If you guys fix this issue i will be very glad. Thanks in advance
Notification seen model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class notificationseen extends Model
{
use HasFactory;
protected $table = 'notificationseens';
public function Notification()
{
return $this->belongsTo(Notification::class, 'notificationID');
}
}
Notification Model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
public function notificationseen()
{
return $this->belongsTo(notificationseen::class, 'notificationID');
}
}
View Blade
#foreach( $notification as $notify )
#if($notify->Notification->seen == 0)
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}" id="notifysee" data-id="{{ $notify->id }}">
<div class="alert unread custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#else
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}">
<div class="alert custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#endif
#endforeach
Table structure
$table->increments('id');
$table->integer('userid');
$table->integer('notificationID');
$table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
$table->timestamps();
Can you please help me out. I cant see any issue but its me error "Attempt to read property "seen" on null"
Ok, some things:
I would use belongsTo in the notificationseen class unless one notificationseen could have more than one Notifications to belong to ;)
Do a Notification have more than one notificationseen references? I do not think so, so in your Notification change the reference to hasOne.
In your blade, use #if($notify->notificationseen->seen == 0) or you call better "Notification::with('notificationseen')->get()" in your controller and then pass it to your view.
You can try to add an isset to avoid the error :
#if(isset($notify->Notification->seen) && $notify->Notification->seen == 0)
(...)
#else
(...)
#endif
EDIT : in your code, you defined 2 belongsTo methods, but according to the official Laravel documentation, you must define a hasOne method and the inverse of it, the belongsTo method.

Trying to get property 'image' of non-object in Laravel

I am working on a laravel project. In this project, when i try to view and edit the profile page. I am getting this error.
ErrorException
Trying to get property 'image' of non-object (View: C:\xampp\htdocs\HomeServices\resources\views\livewire\sprovider\sprovider-profile-component.blade.php)
http://127.0.0.1:8000/sprovider/profile
SproviderProfileComponent.php :-
<?php
namespace App\Http\Livewire\Sprovider;
use App\Models\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class SproviderProfileComponent extends Component
{
public function render()
{
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->first();
return view('livewire.sprovider.sprovider-profile-component',['sprovider'=>$sprovider])->layout('layouts.base');
}
}
Models/ServiceProvider.php :-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceProvider extends Model
{
use HasFactory;
protected $fillable = ['user_id'];
public function category()
{
return $this->belongsTo(ServiceCategory::class,'service_category_id');
}
}
sprovider-profile-component.blade.php
<div class="panel-body">
<div class="row">
<div class="col-md-4">
#if($sprovider->image)
<img src="{{asset('images/sproviders')}}/{{$sprovider->image}}" width="100%" />
#else
<img src="{{asset('images/sproviders/default.jpg')}}" width="100%" />
#endif
</div>
<div class="col-md-8">
<h3>Name: {{Auth::user()->name}}</h3>
<p>{{$sprovider->about}}</p>
<p><b>Email: </b>{{Auth::user()->email}}</p>
<p><b>Phone: </b>{{Auth::user()->phone}}</p>
<p><b>City: </b>{{$sprovider->city}}</p>
<p><b>Service Category: </b>
#if($sprovider->service_category_id)
{{$sprovider->category->name}}
#endif
</p>
<p><b>Service Locations: {{$sprovider->service_locations}}</b></p>
Edit Profile
</div>
</div>
</div>
The only reason this could happen is because $sprovider is null.
When it happens, $sprovider->image will translate to (null)->image, which is indeed Trying to get property 'image' of non-object.
You could do this to prevent $sprovider from being null:
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->firstOrFail();
By using firstOrFail instead of first, you ensure $sprovider will never be null (like the name suggests, if it doesn't find any provider, it will fail).
You will have another error saying that no provider could be found, this is another issue, probably because you don't have any provider for this user or something like that.
Two possibilities
$sprovider is null and
image key is not available in $sprovider ($sprovider->image) may be you mis-spelled image in database table so for that just use dd($sprovider); or print_r($sprovider); and check image key is available or not.

Livewire pagination links are missing the route

I created a Livewire component that uses the WithPagination trait, on the render function I paginate the results and on the component's blade I print the paginator links, the paginator works however when I click any paginator link the page URL changes to the base_url/?page=x
The component class:
<?php
namespace App\Http\Livewire;
use App\Models\Order;
use Livewire\Component;
use Livewire\WithPagination;
class Table extends Component
{
use WithPagination;
public function render()
{
$orders = Order::latest()->paginate(10);
return view('livewire.table', compact(['orders']));
}
}
The component blade:
<div>
#foreach($orders as $order)
{{ $order->name }}
#endforeach
{{ $orders->links() }}
</div>
I tried the appends (like in regular Laravel paginator) and the withQueryString but when I click any pagination link the URL changes to the base URL only with the ?page=page_number, and for many reasons, I need to keep the original route.
Am I missing something?
Versions:
Laravel 8
Livewire 2

NotFoundHttpException Laravel

I am very new in learning Laravel. I want to fetch data from a database and show it. I can do it. But I want to use the title (fetched from the database) as a link. but then I get a NotFoundHttpException.
Routes
Route::get('articles', 'ArticleController#index');
Route::get('articles/{id}', 'ArticleController#show');
Controller
class ArticleController extends Controller
{
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id){
$article = Article::find($id);
return view('articles.show', compact('article'));
}
}
View
#extends('new_welcome')
#section('content')
<h1>Articles</h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop
Can someone help me in this case?
Your problem is because of You've "eat" one curly brace (blade engine skips it):
was:
href="{url ('/articles',$article->id)}"
have to be:
href="{{url ('/articles',$article->id)}}"
as You said:
if I click on any single article title then it can not show me the
specific article. But, if I give the URL "homestead.app/articles/2";
so You can see that when You click on link Your browser's address bar becomes:
homestead.app/{url ('/articles',$article->id)}
Because You're beginner so I'll give You advice to not to set direct url in views using url() helper.
Named routes are better if You want to have app that will work properly if in future You decide to change url from: articles to artcls. In this named routes will save You from bulk changing urls in view files.
set name to Your route using 'as' directive that makes Your routing flexible for changes (when You need to change URL so You change only path and keep views unchanged):
Route::get('articles/{id}', ['as' => 'article', 'uses' => 'ArticleController#show']);
Route::get('articles', ['as' => 'articles', 'uses' => 'ArticleController#index']);
change Your view file (find route helper in href):
#extends('new_welcome')
#section('content')
<h1> Articles </h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.

Resources