data not displayed when passed from controller to view - laravel

I am trying to display data sent from Controller to view in Laravel 5.8 but when I do in my controller:
return view('index', ['user'=> 'mark']
and I try to display it in the view using:
{{$user}}
nothing is displayed, why and how to solve this please?
when I do dd($user) in view I get 'mark' but it is not displaying using previous method
I tried what is in Laravel 5.8 documentations

you can try this:
//controller
$data['user'] = 'mark';
return view('index', $data);
//your view
{{ $user }}

In your controller, put:
$user = 'mark';
return view('index', compact('user'));
In your view, you can now access the user through Blade templating:
{{ $user }}

Related

Laravel missing parameters to update form in controller

So I have a little project, in it, there's the possibility to upload banners images to be shown on the main page. All the stuff related to the DB are already created and the option to create a banner is working, it creates the banner and then stores the image on the DB for use later. Now I'm trying to work on an edit function so I can change the description under the bannners. I have an Edit route in my controller which returns a view where I edit said banner then it calls the update function on the controller. But no matter what I put here, I'm always getting the Missing Required Parameters error once I try to Save the edit and open my controller through the Update function. Here's the code as it is now:
The route definition:
Route::resource('banner', 'BannerController');
The edit function on my controller:
public function edit($id)
{
return view('admin/edit-banners',['id'=>$id]);
}
The update function has not been implemented because I always start with a dd() function to check if everything is working fine:
public function update(Request $request, $id)
{
dd($request);
}
And here's the form line in my edit view that is trying to call the update route:
<form class="card-box" action="{{ route('banner.update',[$banner]) }}">
I also added this at the beginning of the view to store the data from the DB into a variable:
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
The $banner variable contains all the information on the banner being edited, and I can get the new description at the controller with the $request variable, so I honestly don't know what should I put here as parameters, any ideas?
The $banner variable is not a Model instance, it is a Collection.
Adjust your controller to pass this to the view instead of dong the query in the view:
public function edit($id)
{
$banner = Banner::findOrFail($id);
return view('admin.edit-banners', ['banner' => $banner]);
}
You could also use Route Model Binding here instead of doing the query yourself.
Remove that #php block from your view.
The form should be adjusted to use method POST and spoof the method PUT or PATCH (as the update route is a PUT or PATCH route) and you should adjust the call to route:
<form class="card-box" action="{{ route('banner.update', ['banner' => $banner]) }}" method="POST">
#method('PUT')
If you include $id in your function declaration then when you call the route helper it expects you to give it an id parameter. Try with
<form class="card-box" action="{{ route('banner.update',['id' => $id]) }}">
You should be able to retrieve the form data just fine form the $request variable. More info here.
The code below should be the error source. $banner variable then is an array but the update function accept object or id.
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
You should try to replay this code by this one...
#php
use\App\Banner;
$banner = Banner::find($id);
//you should put a dd here to view the contain of $banner if you like
#endphp
Hop it help...

Laravel 404 Not Found - But Route Exists

Hello im trying to get information about user on my view.
Here is my UserController
public function getUser(Request $request, $id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user' -> $user]);
}
here is my web.php
Route::get('admin/user/{id}', "UsersController#getUser");
and my user view
#extends('admin.layouts.app')
#section('contents')
<h1>User {{ $user }} </h1>
#endsection
I am trying to display user information in this view, like name etc, but im recives 404
Not Found page. What im doing wrong. Im using Laravel 6
404 error may refer to a User not being found, since you have a findOrFail() query. It may have nothing to do with your routes.
Just double check with:
php artisan route:list
just to make sure the route is being registered correctly.
I think you firstly use any prefix for this route.For this it will give you an error.To check route list.
php artisan route:list
it will give you all route.
And Here you don't need (Request $request) because here you just need the id.it not the problem..i give you just this suggestion
public function getUser($id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user'=> $user]);
}
why you use '-> ' you should use '=>'

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 - Passing Parameters/ undefined variable

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>

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