I want to do like twitter/instagram profile url address and I want to get data from database. like: example.com/{username}. But I can not do that because I'm beginner in Laravel. How can I do this? And I'm using Laravel 8 and are my last two routes correct?
My web.php is:
Route::get('/test', [HomeController::class, 'test'])->name('test');
Route::middleware(['auth:sanctum', 'verified'])->get('/profile', function () {
return view('design2.profile');
})->name('profile');
Route::middleware(['auth:sanctum', 'verified'])->get('/settings', function () {
return view('design2.settings');
})->name('settings');
My HomeController is:
public function test(){
return view('design2.test');
}
public function settings(){
return view('design2.settings');
}
public function profile(){
return view('design2.profile');
}
This documentation link that is going to help you: https://laravel.com/docs/8.x/routing#route-parameters
But here is a simple example
Route::get('/{username}', function ($username) {
// Logic to show the data
});
There's a few ways to do this, but the simplest way would look something like this:
Route::get('profile/{username}', [HomeController::class, 'profile']);
And then use the $username parameter from your controller like so:
public function profile($username){
// The following line assumes you have a model called "User"
// and a property / DB column called "username" that you're wanting in the URL.
// firstOrFail will return a 404 error automatically if the username isn't found
$user = User::where('username', $username)->firstOrFail();
return view('design2.profile', compact('user'));
}
Then in your design2.profile blade file $user can be used like so:
<h1>{{ $user->fullName}}'s Profile</h1>
<ul>
<li>First Name: {{ $user->firstName }}</li>
<li>Last Name: {{ $user->lastName}}</li>
<li>Email: {{ $user->email }}</li>
</ul>
Related
The blade code:
<td>{{ $employee->first_name }} {{ $employee->last_name }}</td>
<td>{{ __('app-text.indexEdit') }}</td>
<td><form action="{{ route('employee.delete', ['lang' => app()->getLocale(), 'employee' => $employee->id]) }}" method="post">
The controller function:
public function edit(Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
The error:
TypeError
Argument 1 passed to App\Http\Controllers\EmployeesController::edit() must be an instance of App\Employee, string given
http://localhost:8000/fr/employee/edit/1
The routes:
Route::group(['prefix' => '{lang}'], function() {
Route::prefix('employee')->name('employee.')->group(function() {
Route::get('/edit/{employee}', 'EmployeesController#edit')->name('edit');
Route::patch('/edit/{employee}', 'EmployeesController#update')->name('update');
I'm trying to make the application a multi-language application so just after I added the lang variable the route won't pass the $employee->id variable. Should I add a variable that's passable to my controller for the lang variable?
any solution? Many thanks.
first you can make a route to change language
Route:: get('lang/{lang}', function ($locale) {
session(['locale' => $locale]);
return \Redirect::back();
})
step 2: middleware
public function handle($request, Closure $next)
{
App::setLocale(session('locale'));
return $next($request);
}
after you can make a group
Route::group(['middleware' => 'language'],function(){
//routes with u want change language
Route::get('/edit/{employee}', 'EmployeesController#edit')->name('edit');
Route::patch('/edit/{employee}', 'EmployeesController#update')->name('update');
});
and you forget to send the language in each route
Your parameters are wrong. As the stack trace says the controller method is expecting an instance of your Employee model but you are passing in a string
Change
public function edit(Employee $employee)
To
public function edit(Request $request, $employeeId) // you can remove $request if you dont intend to perform redirects
So in the end your code looks like
public function edit(Request $request, $employeeId)
{
$employee = Employee::find($employeeId);
$companies = Company::all(); // use all instead of get if you arent going to perform selections.
return view('employee.edit', compact('employee', 'companies'));
}
Note: you may need to handle cases where employee is not found based on the $employeeId supplied
I think you have to modify your routes like these
in web.php
Route::get('your-route/{lang}/{id}','YourController#edit');
In your controller
public function edit($lang,Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
if you are passing locale as well in the route then it should be as below:
web.php
Route::get('your-Own-route/{lang}/{employee}','YourController#edit');
Controller edit method
public function edit($lang,Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
Sorry, I'm new to developing on Laravel. I'm trying to show info contained in the database on my page. But it can't find the variable holding all the data. I can see the info in Tinker, but i can't seem to deplay is.
I posted some pictures so you can have a look. I'd love to hear your feedback.
Images: https://imgur.com/a/zLSqSDG
Code:
Route:
<?php
Route::get('/', function () {
return view('index');
});
Route::resource('complaints', 'ComplaintController');
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Complaint;
class ComplaintController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$complaints = Complaint::all();
return view('index', compact('complaints'));
}
Blade:
#extends('layout')
#section('title','Welcome')
#section('content')
{{-- #foreach ($complaints as $complaint)
<h1>{{ $complaint->title }}</h1>
<p>{{ $complaint->body }}</p>
#endforeach --}}
{{ $complaints }}
#endsection
You are not routing to the correct function in your controller. Try this
Route::resource('complaints', 'ComplaintController#index');
Try it with another syntax like below:
public function index() {
$complaints = Complaint::all();
return view('index')->with(compact('complaints'));
}
or
public function index() {
$complaints = Complaint::all();
return view('index')->with('complaints', $complaints);
}
As #amirhosseindz said
When you're visiting this url : http://127.0.0.1:8000/complaints it will work because you're hitting
Route::resource('complaints', 'ComplaintController');
But when you visit this url : http://127.0.0.1:8000
you're hitting this action:
Route::get('/', function () {
return view('index');
});
where $complaints doesn't exists
You should try this:
Your Controller
public function index() {
$complaints = Complaint::all();
return view('index',compact('complaints'));
}
Your View (index.blade.php)
#extends('layout')
#section('title','Welcome')
#section('content')
#if(isset($complaints))
#foreach ($complaints as $complaint)
<h1>{{ $complaint->title }}</h1>
<p>{{ $complaint->body }}</p>
#endforeach
#endif
#endsection
I am Newbie. I'm trying to return a view of member profile
At the moment, the user profile is accessible by its ID, like so
profile/7
I would like to access it through the name that I've created
profile/John
this is my route
Route::get('profile/{id}', 'ProfilController#tampilkanID');
this is my controller
public function tampilkanID($id)
{
$auth = Auth::user()->id;
$users=\App\users::all()->whereNotIn('id',$auth);
$tampilkan = Users::find($id);
return view('tampilkan', compact('tampilkan', 'users'));
}
and this how i call it in my blade
#foreach($users as $user)
<tr>
<td><a id="teamname" href="{{ url('profile',$user->id) }}" target="_blank">{{$user->name}}</a></td>
</tr>
#endforeach
thank you
try this:
Route:
Route::any('profile/{name}', 'ProfilController#index')->name('profile.index');
Controller:
public function index(Request $request, $name)
{
$user = User::where('name', $name)->first();
if(isset($user))
return view('tampilkan', ['user' => $user]);
return "user not found!";
}
Blade:
#foreach($users as $user)
<tr>
<td><a id="teamname" href="{{ route('profile.index',['name' => $user->name]) }}" target="_blank">{{$user->name}}</a></td>
</tr>
#endforeach
Suggestion:
if you're doing this, you should also set "name" column to "unique" in users table in order to get exactly one user each time and not confuse users to each other.
You can use laravel Route Model Binding.
What is Route Model Binging?
Route model binding in Laravel provides a mechanism to inject a model instance into your routes.
How can I use it?
Pass object rather then id like
Route::get('users/{user}', function ($user) {
return view('user.show', compact('user'));
});
In your User.php define getRouteKeyName then return whatever you want as a route
public function getRouteKeyName()
{
return 'name'; //this will return user name as route
}
so your route will be users/name
For more information have a look at laravel documentation https://laravel.com/docs/5.5/routing#route-model-binding
Just Customise you RouteServiceProvider as Like below :
public function boot()
{
parent::boot();
Route::bind('user', function ($value) {
return App\User::where('name', $value)->first() ?? abort(404);
});
}
or
customise your route key in model.
For eg :
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'name';
}
Route :
Route::get('users/{user}', function ($user) {
return view('user.show', compact('user'));
});
I'm looking for some help. I've searched on other topics, and saw what is the problem approximatively, but didn't succeed to fix it on my code.
Now the question is: I have NotFoundHttpException when i try to submit an update on my code.
Here is the Controller and my function update
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;
class testing extends Controller
{
public function index()
{
$user = T_collaborateurs_table::all();
return view ("read", compact("user"));
}
public function create()
{
return view("create");
}
public function store(Request $Request)
{
T_collaborateurs_table::create(Request::all());
return redirect("index");
}
public function show($id)
{
$user=T_collaborateurs_table::find($id);
return view("show", compact("user"));
}
public function edit($id)
{
$user=T_collaborateurs_table::find($id);
return view("update", compact("user"));
}
public function update(Request $Request, $id)
{
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
}
Now the routes
Route::get("create", "testing#create");
Route::post("store", "testing#store");
Route::get("index", "testing#index");
Route::get("show/{id}", "testing#show");
Route::get("edit/{id}", "testing#edit");
Route::patch("update/{id}", "testing#update");
And now the view update.blade.php
<body>
{{Form::model($user, ['method'=>'patch', 'action'=>['testing#update',$user->id]])}}
{{Form::label('Id_TCa', 'ID')}}
{{Form::text('Id_TCa')}}
{{Form::label('Collaborateur_TCa', 'collab')}}
{{Form::text('Collaborateur_TCa')}}
{{Form::label('Responsable_TCa', 'resp')}}
{{Form::text('Responsable_TCa')}}
{{Form::submit("update")}}
{{Form::close()}}
</body>
Here the route:list
I'm sorry if my words are not very understable...
Thank you all for your time.
{{Form::model($user, ['method'=>'PATCH', 'action'=> ['testing#update',$user->id]])}}
Or try to use 'route' instead of 'action',to use 'route' you just need a little edit in your update route.
Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing#update'));
in your view:
{{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}
And please follow the convention of class naming. Your class name should be 'TestingController' or 'Testing'.
You could try method spoofing by adding
{{ method_field('PATCH') }}
in your form and change the form method to POST
{{ Form::model($user, ['method'=>'POST', 'action'=>['testing#update', $user->id]]) }}
add the id as an hidden field
{{ Form::hidden('id', $user->id) }}
access the id in the controller as
public function update(Request $Request)
{
$id = Input::get('id');
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
also need to modify your route accordingly
Route::patch("update", "testing#update");
Try using on function update:
return redirect()->route('index');
I want to pass the selected item's $id to my controller for doing changing on it .
it's my index.blade.php (view)code
<table>
#foreach($posts as $p)
<tr>
<td>{{$p->title}}</td>
<td>{{substr($p->body,0,120).'[...]'}}</td>
<td>{{HTML::link('posts_show',' Preview',array($p->id))}}</td>
<td>{{HTML::link('posts_edit','Edit',array($p->id))}}</td>
<td>
{{Form::open(array('method'=>'DELETE','url'=>array('posts.delete',$p->id)))}}
{{Form::submit('Delete')}}
{{Form::close()}}
</td>
</tr>
#endforeach
</table>
but it doesnt pass $id to my controller's methods.
thanks for your time.
What you need to do is to set route parameter. Your route should be like that.
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show/{id}','postController#show');
Route::get('posts_edit/{id}','postController#edit');
Route::post('posts_delete/{id}','postController#destroy');
If you want to use named route {{ Form::open(array('url' => route('posts.edit', $p->id))) }}, you need to set name like that.
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
You can check routing in laravel official documentation.
Edit
For now, your form in view look like that.
{{ Form::open(array('url' => route('posts.edit', $post->id), 'method' => 'POST')) }}
In route,
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
In PostController,
public function edit($id)
{
// do something
}
I hope it might be useful.
Looks like your route has a name posts.destroy, if that is the case you should use route instead of url as a parameter
{{Form::open(array('method'=>'DELETE','route'=>array('posts.destroy',$p->id)))}}
it's my route:
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show','postController#show');
Route::get('posts_edit','postController#edit');
Route::post('posts_delete','postController#destroy');
it's my postController:
class postController extends BaseController{
public function show($id){
$post=Post::find($id);
$date=$post->persian_date;
return View::make('posts.show')->with('post',$post)->with('date',$date);
}
public function edit($id){
$post=Post::find($id);
if(is_null($post)){
return Redirect::route('posts.index');
}
return View::make('posts.edit')->with('post',$post);
}
public function update($id){
$input=array_except(Input::all(),'_method');
Post::find($id)->update($input);
return Redirect::route('posts.index');
}
public function destroy($id)
{
Post::find($id)->delete();
return Redirect::route('posts.index');
}