I send the parameter by url in view but I get an error of the undefined variable but I don't understand why if I am not sent that variable.
enter image description here
web.php
Route::get('acceso/{url}', 'AccesoController#index')->name('acceso');
Route::put('acceso/user-id/{url}', 'AccesoController#update');
acceso.blade.php
<form method="POST" action="{{ url('acceso/user-id', ['url' => $url]) }}">
#csrf
#method('PUT')
<h1 class="h3 mb-3 fw-normal">Bienvenido a Suprapp, {{ old('title', $acceso->name) }}</h1>
<button type="submit" class="w-100 btn btn-lg btn-primary">Acceso a la APP</button>
<p class="mt-5 mb-3 text-muted">© 2017-2020</p>
</form>
AccesoController.php
public function index($url)
{
$users = User::where('url', $url)->get();
foreach ($users as $user) {
return view('acceso', ['url' => $user->id]);
}
}
public function update(Request $request, $url)
{
//$data = $request->all();
$user = User::find($url);
$user->ip_acceso = $request->ip();
$user->url = $request->url();
$user->save();
return redirect()->route('login', ['url' => $user->id]);
}
thanks for your help
The following line is causing your problem:
<h1 class="h3 mb-3 fw-normal">Bienvenido a Suprapp, {{ old('title', $acceso->name) }}</h1>
You're using a variable $accesso but do not pass it through when you return your view.
public function index($url)
{
$users = User::where('url', $url)->get();
foreach ($users as $user) {
return view('acceso', ['url' => $user->id]); // no accesso passed
}
}
Related
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');
I'm new to laravel,i want to use the same form for add and edit.I created an form and form insertion is ok but i need to use the same form edit based on the id selected.When click the edit icon i want to direct the same page displaying the contents to edit.So give me idea for implementing this.
<form method="POST" action="/categoryinsert">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="card-body">
<div class="form-group">
<div class="col-md-4">
<label for="exampleInputEmail1">Category</label>
<input type="text" class="form-control" name="category" id="category" placeholder="Enter Category">
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
// To create a new user in controller
public function create()
{
// user/createOrUpdate.blade.php view
return View::make('user.createOrUpdate');
}
// To update an existing user
public function edit($id)
{
$user = User::find($id);
// user/createOrUpdate.blade.php view
return View::make('user.createOrUpdate')->with('user', $user);
}
Add/edit in view with the help of user model
#if(isset($user))
{{ Form::model($user, ['route' => ['updateroute', $user->id], 'method' => 'patch']) }}
#else
{{ Form::open(['route' => 'createroute']) }}
#endif
{{ Form::text('fieldname1', Input::old('fieldname1')) }}
{{ Form::text('fieldname2', Input::old('fieldname2')) }}
{{ Form::submit('Save', ['name' => 'submit']) }}
{{ Form::close() }}
// To create a new user in controller
public function create()
{
// user/createOrUpdate.blade.php view
return view('user.createOrUpdate')->with([
'view_type' => 'create',
]);
}
// To update an existing user
public function edit($id)
{
$user = User::find($id);
// user/createOrUpdate.blade.php view
return view('user.createOrUpdate')->with([
'view_type' => 'edit',
'user' => $user
]);
}
<form action="{{ ( $view_type == 'edit' ? route('example', $id) : route('control.mentors.store')) }}" role="form" method="post" name="frmDetail">
I have an issue with assign user to any Shop. i created Shop A and Shop B and want to assign user to each shop. Its work fine, when im assign any user to Shop A. however, when i try assign user to Shop B , user alway got in to Shop A not Shop B.
// My User Model
public function shop()
{
return $this->belongsTo(\App\Shop::class, 'user_id');
}
// My Shop Model
public function user()
{
return $this->hasMany(\App\User::class, 'user_id');
}
// My UserController
public function index()
{
$users = User::all();
$shops = Shop::all();
// return view('user', compact('users', 'shops'));
return UserResource::collection($users);
}
public function create(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
$user = new user();
$user->user_id = auth()->user()->id;
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return new UserResource($user);
}
// My User.blade.php Code
#extends('layouts.app')
#section('content')
<div class="container" style="width: 50%">
<h2>Create User</h2>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="user" method="POST">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" >
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="form-group">
<label for="shop">Shop</label>
<select name="shops" class="form-control">
#foreach($shops as $shop)
<option value="{{ $shop->id }}">
{{ $shop->name }}
</option>
#endforeach
</select>
</div>
<button class="btn btn-primary">Submit</button>
</form>
</div>
#endsection
Am i doing something wrong with Relationship?
You have two distinct relation from the Shop model to the User model.
// Shop Model
public function users()
{
return $this->hasMany(\App\User::class);
}
public function owner()
{
return $this->belongTo(\App\User::class);
}
If in your controller you want to assign the shop to the user created
public function create(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
$user = new user();
$user->user_id = auth()->user()->id;
$user->shop_id = $request->shops;
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return new UserResource($user);
}
For the User Model, you need to define the two different relation with Shop Model
public function shop()
{
return $this->belongsTo(\App\Shop::class); //remove the foreign key or change it to 'shop_id'
}
public function ownedShops()
{
return $this->hasMany(\App\Shop::class);
}
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);
I am trying to create a search where user can search for book title. There is a field in 'books' table called 'title'.
however if I do dd($books); I get:
Collection {#667 ▼
#items: []
}
If I do dd($searchTerms); output seems to be correct
Here's my controller:
function search()
{
$books = Book::all();
return view('layouts/search',['books' => $books]);
}
function details() {
$searchTerms = explode(' ', \Request::input('book'));
$books = Book::where(function($q) use($searchTerms) {
foreach ($searchTerms as $book) {
$q->orWhere('title', '%'.$book.'%');
}
})->get();
dd($books);
return view('layouts/details', compact('books'));
}
Search blade:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Search for books</h1>
<form action="{{url('details')}}" method="POST">
{{ csrf_field() }}
<div>
<input type='text' name='book' placeholder='Enter any character' />
</div>
<input type="submit" name="submitBtn" value="Search">
</form>
#endsection
Details blade:
#extends('layouts.master')
#section('title')
#section('content')
<h1>User Details</h1>
<form>
<p>
<ul>
#foreach ($books as $book)
<a href= "{{url('reader/'.$book->title)}}">
{{$book->title}}</a>
</p>
</form>
#endforeach
</ul>
#endsection
You forgot to tell the eloquent builder that your OR's should have LIKE as the operator:
$q->orWhere('title', 'like', '%'.$book.'%');
Please change the function of details to this
function details(Request $request) {
$searchTerms = $request->book;
$books = Book::where(function($q) use($searchTerms) {
foreach ($searchTerms as $book) {
$q->orWhere('title', '%'.$book.'%');
}
})->get();
dd($books);
return view('layouts/details', compact('books'));
}