Get textarea Info Value LARAVEL - laravel

How get value / info in LARAVEL?
admin.blade.php: https://pastebin.com/XHMM3PN9
GeneralController.php
$socket = $_POST['compr'];
ROUTE:
Route::post('sname', 'GeneralController#Sname')->middleware(['auth','admin:6']);

Your form should be :
<form name="fname" action="{{ url('sname') }}" method="post">
<legend>Server Name</legend>
<textarea name="compr" rows="1" placeholder="Server Name" class="form-control">{{ old('compr') }}</textarea>
<input class="btn btn-success btn-sm btn-block" type="submit" value="Set Server Name" />
</form>
Controller Method:
public function Sname(Request $request) {
$socket = $request->compr;
}
Good luck.

As I understand you would like to get input value from the request in your controller.
You can do this as follows:
use Illuminate\Http\Request;
class GeneralController extends Controller
{
public function Sname(Request $request)
{
$compr = $request->input('compr');
// do something with your input
}
}

Related

The GET method is not supported for this route. Supported methods: POST in laravel

i'm trying to add a post after the authentification and getting the name of the doctor who posted the post and add it into the blog.
When i click on submit to add the post i get this error :
The GET method is not supported for this route. Supported methods: POST.
but when i check the database i found the post.
This is the controller postcontroller :
public function add()
{
$blog= new blog;
$blog-> title = request('title');
$blog ->body = request('description');
$blog->author_id=request('articlemed');
$blog ->image = request('photo');
$blog-> save();
return view ('index') ;
}
and this is the form :
<form action="{{ url('/add') }}" method="POST" enctype="multipart/form-data">
#csrf
<label for="">Titre de l'article</label>
<input type="text" class="form-control" name="title" id="" placeholder="Titre de l'article">
<label for=""> Description de l'article</label> </br>
<textarea name="description" id="" cols="63" rows="20"></textarea>
<label for="">Image pour l'article</label>
<input type="file" name="photo" class="form-control" id="" placeholder="Titre de l'article">
<input type="hidden" class="form-control" name="articlemed" value="{{$med->ID}}" />
<button type="submit" class="btn btn-primary"> Ajouter l'article </button>
</form>
and this is the web.php :
Route::post('/add', 'postcontroller#add');
also this is the controller of the authentification :
public function authentification (Request $request)
{
$this->validate($request, [
'email' => 'required',
'mdp'=> 'required'
]);
$logmedecin=doc::orderBy('created_at','desc')->get() ;
$rdv=rendezvous::get();
foreach ($logmedecin as $log )
{
if (( $log->Login== $request->input('email') ) && ( $log->Password== $request->input('mdp') ))
{
return view ('/bienvenu',['med'=>$log] ,['pat'=>$rdv] );
}
}
return back()->withErrors([
'message'=> 'Emails or password not correct!'
]) ;
}
and the web.php of the authentification :
Route::post('/bienvenu','doctor#authentification')->name('aziz');
So the problem was that i didn't use the relationship between post and user (one to many)
so i just add this one to my Post model :
public function user()
{
return $this->belongsTo('App\doc');
}
And i add this to the user model :
public function article()
{
return $this->hasMany('App\blog');
}

Eloquent update doesn't work in Laravel 6

I'm trying to update a field after submitting in the following form:
<form action="{{ route("comments.update") }}" method="post">
#csrf
<input type="hidden" name="commentIDToEdit" id="commentID">
<div class="md-form mb-5">
<i class="fas fa-comment"></i>
<label for="toEditComment"></label>
<textarea name="toEditCommentary" id="toEditComment" cols="3" rows="5" style="resize: none"
class="form-control"></textarea>
</div>
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-default">Modificar</button>
</div>
</form>
I have the CommentsController, where I process the data from the form. Here is the code:
public function updateComment()
{
request()->validate([
"toEditCommentary" => "min:10|max:500"
]);
if (Session::has("username") && getWarningCount(User::whereUsername(session("username"))->value("email")) > 0) {
Caveat::where("commentID", request("commentIDtoEdit"))
->update(["updatedComment" => request("toEditCommentary")]);
} else {
die("No se cumple la condiciĆ³n");
}
if (Comment::where("commentID", request("commentIDToEdit"))->exists()) {
Comment::where("commentID", request("commentIDToEdit"))
->update(["commentary" => request("toEditCommentary")]);
}
return back();
}
Curiosly, the comment is updated in his table, but not the warning. I was thinking in the fillable property in the model, but I don't have it, instead this, I have the following code:
protected $guarded = [];
const UPDATED_AT = null;
const CREATED_AT = null;
Your hidden input is named commentIDToEdit, but in the Controller you fetch the Caveat using request("commentIDtoEdit") (different case).
What you wrote:
Caveat::where("commentID", request("commentIDtoEdit"))
What you should have done: (note the different casing)
Caveat::where("commentID", request("commentIDToEdit"))
This is because in the view, the input name is commentIDToEdit, not commentIDtoEdit.

How to send variable from blade to controller without changing the url

In blade I have a list of books. I want to choose a specific book to show its information. And to do so I want to send with href the id of the book to my controller passing through route.
For example i have
<div class="body text-center">
<h6><b>{{($book->getName())}}</b></h6>
</div>
In href I want to add $bookId = $book->id and the route name so I can call the route with the specific name which calls a method in a controller which can use the variable $bookId
Route::get('/infromation','Books\BookController#index')->name('info');
Here's two propositions:
The first one is to use spatie/laravel-sluggable to have the book name in the URL
The second one is to access the book without changing the URL with a POST request
Using spatie/laravel-sluggable
The slug will be generated automatically from name when the book is created.
your-migration.php
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('slug')->unique()->index();
$table->string('name');
// ...
$table->timestamps();
});
web.php
// Change the URIs as you want. `{book}` is mandatory to retrieve the book though.
Route::get('/books','Books\BookController#index')->name('book.index');
Route::get('/books/{book}','Books\BookController#show')->name('book.show');
Book.php
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class Book extends Model
{
use HasSlug;
protected $guarded = [];
public function getSlugOptions()
{
// Adapt with what you want
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug')
->doNotGenerateSlugsOnUpdate();
}
public function getRouteKeyName()
{
return 'slug';
}
}
BookController.php
class BookController extends Controller
{
public function index()
{
return view('book.index');
}
public function show(Book $book)
{
// $book is retrieving using Model Binding: https://laravel.com/docs/5.8/routing#route-model-binding
return view('book.show', compact('book'));
}
}
index.blade.php
<div class="body text-center">
<a href="{{ route('book.show', $book) }}">
<h6><b>{{ $book->getName() }}</b></h6>
</a>
</div>
Using POST request (URI does not change) and without SLUG
I wouldn't recommend using this for the user experience.
The user cannot bookmark the book or share the link with someone else
When refreshing the page, it will prompt to the user if he want to re-submit the form request
web.php
Route::get('/books','Books\BookController#index')->name('book.index');
Route::post('/books','Books\BookController#show')->name('book.show');
BookController.php
class BookController extends Controller
{
public function index()
{
return view('book.index');
}
public function show()
{
$book = Book::findOrFail(request('book_id'));
return view('book.show', compact('book'));
}
}
index.blade.php
<div class="body text-center">
<form action="{{ route('book.show') }}" method="POST">
#csrf
<input type="hidden" value="{{ $book->id }}" name="book_id">
<h6>
<button type="submit">
<b>{{ $book->getName() }}</b>
</button>
</h6>
</form>
</div>
You can remove the default button style to make it looks like a link
https://stackoverflow.com/a/45890842/8068675
You can try like this
<form action="/BookName/information/<?php echo $book->id; ?>" method="post">
<div class="body text-center">
<input type="hidden" name="book_id" value="{{ $book->id }}">
<a href="/information/<?php echo $book->id; ?>">
<button type="submit" name="book_information" class="btn btn-primary">
<h6>
<b>{{($book->getName())}}</b>
</h6>
</button>
</div>
</form>
// make route like this
Route::post('/BookName/information/{id}','Books\BookController#index');
// Access the that id in controller
public function index(Request $request)
{
echo $request->book_id;
}

Laravel search feature without using collective for forms

I'm trying to use a basic html form without using Laravel collective.
I have this code here.
<form class="form-inline my-2 my-lg-0" action="{{route('patients.index')}}" method="get">
<input class="form-control mr-sm-2" type="text" placeholder="Search" name="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
And then in the PatientsController index function
public function index()
{
$search = \Request::get('search');
$patients = Patient::where('lname','like', '%'.$search.'%');
return view('/searchResults')->with('patients', $patients);
}
When i return the view at the end of the function, it just loads a blank page. when i do $patients = Patient::all(), it yields my full database so i know that at least part of the query is right. what am i doing wrong?
Try searchResults without /
public function index()
{
$search = \Request::get('search');
$patients = Patient::where('lname','like', '%'.$search.'%')->get();
return view('searchResults', ['patients'=>$patients]);
}
You blade template:
resources/views/searchResults.blade.php

Redirection from a vue component to a laravel route

I have a Vue component named searchbox and I want the users to get redirected to display the results once they type the name and click the search button. I am using axios to make the http request. Here's my template:
<form #submit.prevent="searchResult">
<div class="field has-addons searchbox">
<div class="control">
<input class="input" type="text" id="search" name="q" placeholder="Search a video..." #keyup.enter="searchResult" v-model="searchData">
</div>
<div class="control">
<button class="button is-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
Here's my script in the Vue file:
<script>
export default {
data() {
return {
searchData: null,
};
},
methods: {
searchResult() {
axios.get('/search?q=' + this.searchData);
}
}
}
</script>
Here's my search controller:
class SearchController extends Controller {
public function index(Request $request) {
return view('search.index');
}
}
However, I can not see the redirection. How do I redirect from vue component to another route in laravel??
Is vue-router necessary or we can follow any other method??
you can replace axios.get('/search?q=' + this.searchData); with window.location.href = '/search?q=' + this.searchData;

Resources