The #edit Route is not working... showing 404 not found - laravel

the #edit route is not creating. everything is ok but showing 404 not found
the problem is on the last route. I ran the php artisan route:list code but not showing any route named /profile/{user}/edit
the web.php code ->>>>>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfilesController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/{post}','App\Http\Controllers\PostsController#show');
Route::get('/p/create','App\Http\Controllers\PostsController#create');
Route::post('/p','App\Http\Controllers\PostsController#store');
Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('profile.show');
Route::get('/profile/{user}/edit','ProfilesController#edit')->name('profile.edit');
the profiles controller code ->>>>>
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function edit(User $user)
{
return view('profiles.edit', compact('user'));
}
public function index(User $user)
{
return view('profiles.index', compact('user'));
}
}
the index.blade.php code --->>>>>>>>>>>>>>>
#extends('layouts.app')
#section('content')
<div class="container">
<div><h1>here is a big design</h1></div>
<br>
<div class="d-flex justify-content-between align-items-baseline">
<h1>{{ $user->username}}</h1>
Add New Post
</div>
Edit Profile
<div class="pr-5"><strong>{{$user->posts->count()}}</strong> posts</div>
<div class="pr-5"><strong></strong> followers</div>
<div class="pr-5"><strong></strong> following</div>
<br>
<div><h1>{{ $user->profile->title}}</h1></div>
<br>
<div><h1>{{ $user->profile->description}}</h1></div>
<br>
<div><h1>{{ $user->profile->url ?? 'N/A'}}</h1></div>
<h1>Posts</h1>
<hr>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4 pb-4">
<a href="/p/{{$post->id}}">
<img src="/storage/{{$post->image}}" class="w-100">
</a>
</div>
#endforeach
</div>
</div>
#endsection

Change your route to
Route::get('/profile/{user}/edit',[ProfilesController::class, 'edit'])->name('profile.edit');
Your way 'ProfilesController#edit' doesn't take use App\Http\Controllers\ProfilesController; into account.
Offtopic suggestion: since you already named your routes I suggest you use the named routes in your blade files instead of hardcoding them:
Edit Profile
instead of
Edit Profile
This way, should you decide to change a URL some time later, you only need to change it in your web.php file once, not all of your hardcoded occurences

Related

property does not refresh in the internal components of Livewire

Take a look at the following examples:
showPost.blade.php:
<div>
<livewire:content-box :content="$post"/>
<button wire:click="nextPost" >Next Post >></button>
</div>
and
content-box.blade.php :
<div>
<h1>{{ $content->title }}</h1>
<p>{{ $content->content }}</p>
</div>
So far, it is completely clear what is going to happen ...: First, the information of the content to be viewed is received through showPost and passed to the contentBox, and everything is OK ..
Well now I want to get the information of the next content via the account through the button I put and calling the nextPost method:
class ShowPost extends Component
{
public Post $post;
public function render()
{
return view('livewire.show-post');
}
public function nextPost()
{
$id = $this->post->id;
$nextPost = Post::where('id', '>', $id)->first();
$this->post = $nextPost;
}
...
But nothing happens and the contentBox component has no reaction .... Has anyone had this problem ???!
I'm not sure livewire works well with nested components. could use the pagination instead. The livewire docs suggest you should not use them for little snippets or use blade components for that kind of nesting.
You can achieve what you're doing at the moment with some simple pagination.
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;
class SomeContent extends Component
{
use WithPagination;
public function render()
{
// Using simplePaginate(1) instead of paginate(1).
// simplePaginate only shows "<- Previous" and "Next ->" links
// paginate shows those 2 buttons but also page numbers which you don't seem to want.
return view('livewire.some-content', [
'users' => User::simplePaginate(1),
]);
}
}
<div>
{{-- This might look wrong, but essentially it's looping through an array of length 1 because we're paginating --}}
#foreach ($users as $user)
<h1>{{ $user->name }}</h1>
<h2>{{ $user->email }}</h2>
#endforeach
{!! $users->links() !!}
</div>
EDIT
I can confirm blade components work.
Here, nextUser is the same implementation you gave.
public function nextUser()
{
$id = $this->user->id;
$nextUser = User::where('id', '>', $id)->first();
$this->user = $nextUser;
}
<div class="container">
<div class="content">
{{-- These two have the exact same template --}}
<livewire:child :user="$user" />{{-- Doesn't update when clicking Next --}}
<x-blade-child :user="$user" />{{-- Updates when clicking Next --}}
</div>
<div>
<button wire:click="nextUser">Next</button>
</div>
</div>
When clicking nextUser, the blade component updates but the livewire one doesn't.
Livewire doesn't like nested components. In your case, we can use basic blade component:
<div>
<x-content-box :content="$post"/>
<button wire:click="nextPost" >Next Post >></button>
</div>
And then:
Move content-box.blade.php to resources/views/components/
Remove component_name.php file in app/Http/Livewire
Most of the time, we can change 2 nested livewire components to livewire(parent) + basic blade component(child),

Undefined variable Laravel (strange case)

I am m having this annoying problem. Can anyone give a hand to sort out it? I read all posts and I cannot find the solution.
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
use App\Post;
use App\RegisteredCourse;
class DashboardsController extends Controller
{
public function indexA()
{
return view('dashboards.admin-dashboard');
}
public function indexS()
{
$id = Auth::user()->id;
$courses = DB::table('registered__courses')->select('user_id')->where('user_id', '=', $id)->count();
$posts = Post::all();
return view('dashboards.student-dashboard', compact('id', 'courses', 'posts'));
}
public function indexT()
{
return view('dashboards.teacher-dashboard');
}
}
This is a fragment of the blade view. The name of the view is dashboards.student-dashboard
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Posts</h3>
</div>
<div class="card-body">
<div class="tab-content">
<div class="active tab-pane" id="activity">
<!-- Post -->
<!--forEACH-->
#foreach ($posts as $post)
<div class="post">
<div class="user-block">
<span class="username">
{{$post->title}} | {{$post->author}}
</span>
<span class="description">{{optional($post->created_at)->format('d-m-Y')}}</span>
</div>
<!-- /.user-block -->
<p>
{{$post->content}}
</p>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
And these are the routes
Route::group(['prefix' => 'dashboard',], function () {
Route::get('/admin', 'DashboardsController#indexA')->name('dashboards.indexA');
Route::get('/teacher', 'DashboardsController#indexT')->name('dashboards.indexT');
Route::get('/student', 'DashboardsController#indexS')->name('dashboards.indexS');
});
I tried to pass al the variable to the view and I always have the same problem. ("Undefined variable").It seems like blocked the possibility to pass variable to the blade view.
What I did before:
1-dd($posts) It does not working, it appears the exception "Undefined variable: posts".
2- I remove the whole content of the blade file and I tried with a simple varible and it stills appearing the exception "Undefined variable".
3- I ran php artisan view:clear and restarted the server.
Any sugestions?
Thank you very much.
Instead of
return view('dashboards.student-dashboard', compact('id', 'courses', 'posts'));
try
return view('dashboards.student-dashboard', ['id'=>$id, 'courses'=>$courses, 'posts'=>$posts]);

Laravel NotFoundHttpException although route exits

I added a new route as:
Route::post('friendSend/{uid}','FriendController#sendFriendRequest')->name('friends.add');
and called it as a hyperlink to submit form:
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form).submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
However when I click on the said link, I get redirected to /friendSend with the said error.
the route is visible in:
php artisan route:list
which makes sense since I called it via it's name 'friends.add'. It doesn't even go the controller.
I've already tried the following:
Laravel NotFoundHttpException although route exists
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Friend;
use Auth;
class FriendController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return redirect()->route('home');
}
public function sendFriendRequest($id)
{
echo "hello world";
}
}
Update:
Manually entering the url as /friendSend/2 (or any number for that matter) works.
You are missing a ' in the onclick javascript.
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form').submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
If this doesn't work, what is the href on the generated page? Do you have multiple of these forms on a single page?
I thing you are doing wrong on set the url in form submit.You are adding route like
href="{{route('friends.add',$user->uid)}}"
Just modified it as
href="{{route('friends.add',['uid' => $user->uid])}}"
You can refer Laravel Named Routes

Undefined variable: names

I am facing difficulties pushing data from the Controller to the View. Below is my code script.
I created my controller ListController using artisan
php artisan make:controller ListController
ListController - show method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ListController extends Controller
{
public function listNames()
{
$names = array(
'Daenerys Targaryen',
'Jon Snow',
'Arya Stark',
'Melisandre',
'Khal Drogo'
);
return view('welcome', ['names' => $names]);
}
}
Created a view welcome.blade.php (which is default)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">API DOCUMENTATION</div>
<div class="panel-body">
#foreach ($names as $n)
<p>{{$n}}</p>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Then, added the appropriate route for it (routes.php)
<?php
Route::get('/', 'ListController#listNames');
When opening localhost:8000 after running php artisan serve, it throws an Exception
ErrorException in 56f6d37e6c39b528e3f5a170141b734befe0f7a2.php line 14:
Undefined variable: names (View: /Applications/MAMP/.../views/welcome.blade.php)
So far I have tried the following:
In the Controller:
return view('welcome', compact('names')); --> DOESN'T WORK
return view('welcome', $names); --> DOESN'T WORK
return view('welcome')->with($names); --> DOESN'T WORK
return view('welcome')->with('names', $names); --> DOESN'T WORK
Hard coding the array in the view and assigning it a variable works
<?php $names = array('John Snow', 'Arya Stark');?>
<?php foreach ($names as $n):?>
<tr>
<td><?php echo $n;?></td>
</tr>
<?php endforeach;?>
I can't seem to detect the problem. Any help is appreciated.
You should pass variables using an array:
return view('welcome', ['names' => $names]);
Or just:
return view('welcome', compact('names'));
To check it do this in the view:
{{ dd($names) }}
First, pass your variables as Alexey said,
return view('welcome', ['names' => $names]);
And then, on the view, do
#foreach ($names as $n)
<p>This is name {{ $n }}</p>
#endforeach
Documentation here: https://laravel.com/docs/master/blade#displaying-data
Managed to resolve the issue.
After spending so much time looking for the bug, I tried to check if laravel version could have something to do with this. The routes.php doesn't work in Laravel v5.3+ and I was using Laravel v5.4.x.
The routes are available in a directory called routes and so I pasted the code from routes.php to routes/web.php and it worked.
<?php
Route::get('/', 'ListController#listNames');

Laravel:Passing variable from controller to view

chatcontroller.php function returns variable to view:
public function getChat()
{
$message = chat_messages::all();
return View::make('home',compact($message));
}
this is my route:
Route::get('/getChat', array('as' => 'getChat','uses' => 'ChatController#getChat'));
this is my home.blade.php:
#extends('layouts.main')
#section('content')
<div class="container">
<h2>Welcome to Home Page!</h2>
<p> <a href="{{ URL::to('/logout') }}" > Logout </a></p>
<h1>Hello <span id="username">{{ Auth::user()->username }} </span>!</h1>
<div id="chat_window">
</div>
<input type="text" name="chat" class="typer" id="text" autofocus="true" onblur="notTyping()">
</div>
<ul>
#foreach($message as $msg)
<li>
{{ $msg['sender_username']," says: ",$msg['message'],"<br/>" }}
</li>
#endforeach
</ul>
<script src="{{ asset('js/jquery-2.1.3.min.js') }}"></script>
<script src="{{ asset('js/chat.js') }}"></script>
#stop
I am trying to send result returned by select query to view from controller.
when I do this from homecontroller.php then it works fine.
if I try to pass from controller which I have defined it gives error message as:Undefined variable.
I have used the extends \BaseController do i need to do anything else to access my controller variable from view.
please suggest some tutorial if possible for same.
Verify the route to be sure it uses the new controller:
Route::get('user/profile', array('uses' => 'MyDefinedController#showProfile'));
First of all check your routes, as Matei Mihai says.
There are two different ways to pass data into your view;
$items = Item::all();
// Option 1
return View::make('item.index', compact('items'));
// Option 2
return View::make('item.index')->with('items', $items); // same code as below
// Option 3
View::share('item.index', compact('items'));
return View::make('item.index);
You can also do this:
$this->data['items'] = Item::all();
return View::make('item.index', $this->data);

Resources