Laravel - Passing Parameters/ undefined variable - laravel

I'm struggling with passing parameters through in Laravel, I can access it via URL but I don't want that. I'm getting Undefined variable: user in the master.blade error.
Any help is appreciated
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user/userdetails',['user' => $user]);
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>

Other guys are telling you about userdetails view, but your code is totally fine. What you need to do is pass $user variable to the master view too:
return view('master', ['user' => $user]);

Try to pass like this
return view('user/userdetails')->with('user' => $user);

Controller::
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails')-with('user',$user);
}
Then Used In blade File..
#foreach ($user as $data){
<h1> {{ $data['_id'] }} </h1>
<h1> {{ $data['name'] }} </h1>
#endforeach

I assume that you have extended master.blade in user/userdetails.blade. You need use array as second parameter to url function.
<li>Your Details</li>
It is best practice to use named route in laravel.
Change your route like this :
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails')->named('user_details');
In your blade you can use route function to get the path.
<li>Your Details</li>

You can update your code like:
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails',compact('user'));
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>

Related

Method Illuminate\Database\Eloquent\Collection::links does not exist

I created a model relationship between User and Message. I want to implement a list of messages for the authenticated user but I get the following error.
Method Illuminate\Database\Eloquent\Collection::links does not exist
Controller
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $user->message);
}
Message provider
class message extends Model
{
public function user() {
return $this->belongsTo('App\User');
}
}
User provider
public function message ()
{
return $this->hasMany('App\Message');
}
index.blade.php
#extends('layouts.app')
#section('content')
<h1>messages</h1>
#if(count($messages)>0)
#foreach ($messages as $message)
<div class="well">
<h3>{{$message->user}}</h3>
<small>Written on {{$message->created_at}} </small>
</div>
#endforeach
{{$messages->links()}}
#else
<p> no post found </p>
#endif
#endsection
Error
"Method Illuminate\Database\Eloquent\Collection::links does not exist.(View: C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)"
Check your view blade, that method (links()) only could be used when your data model is implementing paginate() method.
If you dont use paginate(), remove this part:
{{$messages->links() }}
If you are trying to paginate your data when it gets to the view then you need to add the paginate in your controller before passing the data to the view. Example
return $users = Users::select('id','name')->paginate(10);
with that paginate method in your controller, you can call the links method to paginate your object in view as shown below
{{$users->links()}}
hope it helps you
There are 2 ways to resolve this issue:
Either use paginate function while searching data from database:
$users = DB::table('users')->where('id',$user_id)->paginate(1);
Remove links() function from index.blade.php
{{ $messages->links() }}
Remove {{ $messages->links() }} to in your index.blade.php because {{ $messages->links() }} is supported only when you use paginate
You can do something like this in your controller file.
public function index()
{
$messages = Message::all()->paginate(5);
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $messages, $user->message);
}

Laravel 5.6 Function () does not exist in route/web.php

This is my code using to send an email
Route::post('/mail/send', [
'EmailController#send',
]);
in EmailController this is the send action
public function send(Request $request)
{
$data = $request->all();
$data['email'] = Input::get('email');
$data['name'] = Input::get('name');
$obj = new \stdClass();
$obj->attr = 'Hello';
Mail::to("dev#mail.com")->send(new WelcomeEmail($obj));
}
getting a error as Function () does not exist
In your route/web.php file
Change it to
Route::post('/mail/send', 'EmailController#send');
Refer to the documentation to see the possible options to define routes:
https://laravel.com/docs/5.6/routing
Route's action method can be defined using a array, but not simply wrap controller#action in an array, you should assign it to array's key 'uses'.
In your example, it should be like this:
Route::post('/mail/send', [
'uses' => 'EmailController#send',
//'middleware' => .... assign a middleware to this route, if needed
]);
the array form usually is used when we want to specify more specification about the route like use a specific middleware and pass middleware parameters.
if you just want to define route's processing method you can simply use controller#action as Route::post's second parameter:
Route::post('/mail/send','EmailController#send');
In your route ...
Route::post('/mail/send','EmailController#send')->name('send_email');
Inside of your HTML form add below code...
<form action="{{route('send_email')}}" method="post">
...
{{csrf_field()}}

Laravel parameters without explicitly specifying

I want to generate the page url without $name, something like this
mysite.app/statestring
BUT with the route get('/action/{name}', ...) i can get only this
mysite.app/statestring/somename
If i changing route to Route::get('/action', ...) it doesnt work, the error is "Missing argument 1"
My web.php
Route::get('/action/{name}', [
'uses' => 'DoActionController#getAction',
'as' => 'returnAction',
]);
My Controller action
public function ($name)
{
return view('returnaction', ['name'=>$name]);
}
My home page
<body>
#foreach ($yourActions as $yourAction)
<li>
{{ $yourAction->name }}
</li>
#endforeach
You'll need to make the $name argument optional.
First, declare it as optional in the controller:
public function ($name = null)
That will get rid of the error your getting about missing argument. Next, make the name parameter optional in your route with:
Route::get('/action/{name?}', ...);

laravel 5.2 pagination pretty url

I am Using Laravel 5.2
Is There a Way To Get a Pagination Pretty URL in Laravel 5.2?
http://localhost:8000/backend/admin_user?page=10&page=1
And What I Would Like To Get,How generate Link Pretty Url:
http://localhost:8000/backend/admin_user/10/1
So you can try something like that:
Route::get('test/{page}', function ($page) {
return User::paginate(2, ['*'], 'page', $page);
});
You can achieve this with three simple steps.
Register the route:
Note the question mark, this makes the size and page values optional;
Route::get('backend/admin_user/{size?}/{page?}', ['uses' => 'BackendController#adminUser']);
Implement this function in your controller:
Note the default values, $size = 10, $page = 1. This makes sure that you don't get an error if you navigate to the url without the pagination.
<?php namespace App\Http\Controllers;
use App\Models\AdminUser;
use Illuminate\Pagination\LengthAwarePaginator;
class BackendController
{
public function adminUser($size = 10, $page = 1)
{
$collection = AdminUser::all();
$users = new LengthAwarePaginator($collection, $collection->count(), $size);
$users->resolveCurrentPage($page);
return view(backend.admin_user);
}
}
Use in your view like this:
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
</div>
{{ $users->links() }}

Redirect to external url with GET Parameter in Laravel 5

Is there possibility to redirect user to an external URL and pass paramter to it i Laravel 5?
You should use away() method:
return redirect()->away('http://sample.com/url?param=param')
You can pass values to views:
Route::get('/user/{id}', function () {
$user = User::find($id);
return view('user', ['user' => $user]);
}
And then access user in the view:
{{ $user }}

Resources