Undefined variable: posts when passing parameter from controller to view - laravel

I'm trying to create a search function in Laravel and its returning me with "undefined variable: posts" when I do foreach on my view.
My code:
Post Model
class Post extends Model {
protected $fillable = [
'creator',
'post_url',
'books',
'likes',
'created_at'
];
public function user() { return $this->belongsTo(User::class); }
}
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search($keyword)
{
$result = Post::where('books', 'LIKE', "'%' . $keyword . '%'")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search/{keyword}', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach
What am I doing wrong here?

This might help you out.
Homeview.blade.php
<form action="/search" method="POST">
#csrf // include your csrf token
<input type="text" class="search-text form-control form-control-lg" id="q" name="q" placeholder="Search" required>
</form>
Searchview.blade.php
<!-- or did you return a collection? -->
#if( $posts->count() > 1 )
<!-- then loop through the posts -->
#foreach( $posts as $post )
<div class="post"> {{ $post->id }} </div>
#endforeach
#else
#if( !empty($posts) )
<div class="post"> {{ $post->id }} </div>
#endif
#endif
Routes/web.php
Route::post('/search', 'PostsController#show')->name('posts.show');
PostsController
use App\Post;
public function show( Request $request )
{
$result = Post::where("books", "LIKE", "%{$request->input('q')}%")->get();
// Uncomment the following line to see if you are returning any data
// dd($result);
// Did you return any results?
return view('searchview', ['posts' => $result]);
}

The reason it wasn't working,
Route::get('/search/{keyword}', 'SearchController#search');
In your route file you were looking for a {keyword} that was never passed by the form. Your form action is action="{{ url('/search') }}". A get variable will not be picked up by a route and if it was you called the input 'q' anyway.
So then in your controller you were looking for the keyword being passed that is never passed in.
public function search($keyword)
Instead the correct thing to do is pass in the Request object like so
public function search(Request $request)
Then use $request->input('q') to retrieve the passed value through your form.
In your example $keyword would always have been blank.
Corrected code
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search(Request $request)
{
$result = Post::where('books', 'LIKE', "%{$request->input('q')}%")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach

try:
return view('/search')->with('posts', $result);
Or even better with dinamic vars.
return view('/search')->withPosts($result);

Related

How to update an array field in laravel?

EDIT FORM
<div class="form-group mb-3">
<label>Country:</label>
<div class="col-sm-4">
<select id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
#foreach($countries as $country)
<option value="{{$country->id }}" {{in_array($country->id, explode(',',$user->country)) ? 'selected' : '' }}> {{$country->name}}</option>
#endforeach
</select>
</div>
</div>
CONTROLLER
public function updateuser(Request $request, $id)
{
// dd($request->all());
$request->validate([
'name'=>'required',
'email'=>'required|email|unique:users',
'country'=>'required',
'state'=>'required',
'city'=>'required',
'role_id'=>'required'
]);
$name = $request->name;
$email=$request->email;
$country=implode(',',$request->country);
$state=implode(',',$request->state);
$city=implode(',',$request->city);
$role_id=$request->role_id;
User::whereId($id)->update($request->all());
return redirect()->route('viewuser');
}
ROUTE
Route::put('/updateuser/{id}',[UserController::class,'updateuser'])->name('updateuser');
If I update a record. It just reloads the page and there is no error. Please correct me if I am doing wrong. Thanks in advance.
Try in the CONTROLLER:
public function updateuser(Request $request, $id)
{
$request->validate([
'name'=>'required',
'email'=>'required|email|unique:users',
'country'=>'required',
'state'=>'required',
'city'=>'required',
'role_id'=>'required'
]);
$user = User::findOrFail($id);
$user->name = $request->name;
$user->email=$request->email;
$user->country=implode(',',$request->country);
$user->state=implode(',',$request->state);
$user->city=implode(',',$request->city);
$user->role_id=$request->role_id;
$user->update();
return redirect()->route('viewuser');
}
Try in the form:
<form action="{{ route('updateuser',$data->id) }}" method="POST" enctype="multipart/form-data">
#method('PUT')
#csrf
ALL YOUR INPUTS HERE
<div class="card-footer">
<button type="submit" class="btn btn-primary mt-1 pr-4 pl-4">Update</button>
</div>
</form>
Route
Route::match(['put', 'patch'], 'updateuser/{id}', [UserController::class,'updateuser'])->name('updateuser');

After reflecting, I would like to display changes in form page

I would like to make a form.
After submitting the data,Redirect to the same page and reflect a change.
I thought return redirect should be a good way.
but it seems need to fetching the DB.
because 'ErrorException
Trying to get property 'id' of non-object (View:'error happens.
writing $user = \DB::table('users')->where('id', $request->id)... twice
is redundancy and cheesy.
Is there any good way to implement this.
class CertainController extends Controller
{
public function index(Request $request)
{
$user = \DB::table('users')->where('id', $request->id)->first();
$data = ['user' => $user];
return view('user.detail',$data);
}
public function update(Request $request)
{
\DB::table('users')
->where('id', $request->id)
->update([
$request->name => $request->value
]);
return redirect(route('user.detail', [
'user_id' => $request->id,
]));
}
}
web.php
Route::get('/user_detail', 'CertainController#index')->name('user.detail');
Route::get('/user_detail/update', 'CertainController#update')->name('user.detail.update');
blade
<form method ="GET" action={{ route('user.detail.update')}}>
<div class="form-group row">
<label>name</label>
<div class="col-md-6">
<input type = "hidden" name ="id" value="{{ $user->id }}"/>
<input type = "hidden" name = "column" value="name">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ $user->name }}" required autocomplete="name">
#error('')
{{ $message }}
#enderror
<button type = "submit" class ="button">submit</button>
</div>
</div>
</form>
You are looking for id in the index method,
$request->id
^^
But you are sending user_id from update method.
'user_id' => $request->id,
^^^^^
In a simple way, you can just do
return back();
here, back() is a helper function, which redirect back to where it came from.

search by multiple values using checkbox in laravel

I have job_sector table in which sector_id and job_id fields are there. I just want to search job_id by the sectors which I have selected through checkbox. One may select multiple sectors.
My model :
public function scopeSelectedOptions($query, $input = [])
{
if(!empty($input)) {
if(array_key_exists('sector_id', $input)) {
$query->whereHas('sector_id', function($q) use ($input) {
return $q->whereIn('sector_id', $input['sector_id']);
});
}
}
return $query;
}
Controller :
public function jobsFilter(Request $request)
{
$jobs = JobSector::SelectedOptions(request()->all())->get();
return view('front.pages.job.jobfilter')->with(['title'=>'Job Filter', 'jobs' => $jobs]);
}
Form from where I am selecting multiple sectors :
<form action="{{ route('job.jobfilter') }}" method="GET" class="mb-4">
{{csrf_field()}}
#foreach(get_sectors() as $k=>$s)
<input type="checkbox" name="input[]" value="{{ $k }}">{{ $k }}<br>
#endforeach
<input type="submit" value="Search" />
</form>
Query showing the output :
#foreach($jobs as $c)
{{ $c->job_id }} <br>
#endforeach
It shows me all the job_id in the table.
Please help me out,
You are giving the wrong array to your scope
it would look like this :
['input' => ['12' => true]]
try this
<form action="{{ route('job.jobfilter') }}" method="GET" class="mb-4">
{{csrf_field()}}
#foreach(get_sectors() as $k=>$s)
<input type="checkbox" name="sector_id[{{ $k }}]">{{ $k }}<br>
#endforeach
<input type="submit" value="Search" />
</form>
public function jobsFilter(Request $request)
{
$jobs = JobSector::whereIn('sector_id', array_keys(request()->sector_id))->get();
return view('front.pages.job.jobfilter')->with(['title'=>'Job Filter', 'jobs' => $jobs]);
}
(I just ignore your scope to be more readable)

"No message" error laravel - trying to update user account information

I'm receiving the error "MethodNotAllowedHttpException
No message" on submit of my user's form, which is meant to update the user's table. I have two post forms on the same page and two post routes, would that have something to do with it?
I will include all the routes and another form that might be conflicting with it.
web.php
Route::get('profile','userController#profile');
Route::post('profile', 'userController#update_avatar');
Route::post('profile-update', 'userController#update_account'); //this ones not working
userController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Image;
class UserController extends Controller
{
//
public function profile()
{
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
public function update_account(Request $request, $id) //the function with the error
{
User::update([
'id' => Auth::user()->id,
'name' => $request->name,
'email' => $request->email
]);
return redirect('/profile');
}
}
profile.blade.php
<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="/profile" method="post">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;" value="Update Image">
</form>
<form method="post" action="/profile-update"> <!-- The form with the error -->
{{ method_field('put') }}
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT" />
<label>Username</label>
<input type="text" name="name" class="form-control" value="{{ $user->name }}">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{ $user->email }}">
<input type="submit" id="update-account" class="btn btn-success" value="Update">
</form>
try this method:
public function update_account(Request $request, $id)
{
$user = User::find($id)
$user->name = $request->name;
$user->email = $request->email;
$user->update();
return redirect('/profile');
}
You don't have any route which can handle the PUT request for "profile-update". In your form you have defined the following function.
{{ method_field('put') }}
This helper function generate an hidden input field which will be used by Laravel to process the current request only as PUT.
To make this work, you either have to make your make your request POST by removing the above helper function or change your route method to PUT.
Route::put('profile-update', 'userController#update_account');
For those that might need the same answer, to fix this I had to play about with it for quite some time and used bits from the suggested answers to solve the issue completely.
I changed the route method to put in web.php.
Replaced my update_account function with #TonzFale answer but replaced $user = User::find($id)with $user = User::find(Auth::user()->id);.

Laravel - If has errors on validation, insert class in the specifics inputs

I'm totally noob in PHP, but my question is, after the validation which has errors for specific inputs, how can I insert a class in the specific input?
Example, if i have this error in the validation: "The email field is required."
How can i insert a specific class in the email input?
Login routes:
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'Admin\AdminController#index');
Route::get('login', 'Admin\AuthController#getLogin');
Route::post('login', 'Admin\AuthController#postLogin');
Route::get('logout', 'Admin\AuthController#getLogout');
});
AdminController:
class AdminController extends AdminBaseController
{
public function index()
{
if(Auth::user()){
return view('admin/pages/admin/index');
}
return view('admin/pages/login/index');
}
}
AuthController:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
private $redirectTo = '/admin';
public $loginPath = '/admin';
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
public function getLogin()
{
if(Auth::user()){
return redirect('/admin');
}
return view('admin/pages/login/index');
}
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6',
]);
}
}
My blade form:
<form class="s-form" role="form" method="POST" action="/admin/login">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="s-form-item text">
<input type="text" name="email" value="{{ old('email') }}" placeholder="Email">
</div>
<div class="s-form-item text">
<input type="password" name="password" value="{{ old('password') }}" placeholder="Senha">
</div>
<div class="s-form-item">
#if ($errors->has())
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
{{ $error }}<br>
#endforeach
</div>
#endif
</div>
<div class="s-form-item s-btn-group s-btns-right">
<input class="s-btn" type="submit" value="Entrar">
</div>
</form>
You can pass an argument to the has method to specify the specific key.
For example, for your email input...
<input class="#if($errors->has('email')) some-class #endif" ... >
I left out the rest of the input field for brevity. It basically checks if an error for the email input exists. If so, 'some-class' is outputted. Otherwise, it skips over it.
Edit: To answer the question on how you can customize where to output your error messages, you can use the get or first methods in conjunction with the has method. For example...
#if ($errors->has('email'))
#foreach ($errors->get('email') as $error)
<p>{{ $error }}</p>
#endforeach
#endif
The has method has already been explained. The get method retrieves the validation errors. Because there can be more than one validation error, you must loop through it and output it.
In the next example, I use first. This method just outputs the first error message so there is no need to loop through it.
#if ($errors->has('email'))
<p>{{ $errors->first('email') }}</p>
#endif

Resources