I`m new to the Laravel 5.4.i wanted to developed search when i enter id number need to fetch service from the database called new.but according to this code it is not functioning.it just show all the services without the exact value related to its id.All i want is if i enter id 1 ..i need to fetch out service related to id=1 and display it in search.blade.phpplease help me!
Here Is my Search.blade.php
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
<!-- Styles -->
</head>
<body>
<form action="search" method="post">
<label>Search by Id!</label><br>
<input type="text" name="Search" /><br>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
<table class="table table-bordered table-hover" >
<thead>
<th>Name</th>
</thead>
<tbody>
#foreach($customers as $customer)
<td>{{ $customer->service }}</td>
#endforeach
</tbody>
</table>
</body>
</html>
Here is my Controller UserController
public function search_code(Request $request){
$query = $request->search;
$customers = DB::table('news')->where('id', 'LIKE',"%$query%")->get();
return view('search' , compact('search', 'customers'));
}
Here Is My Route
Route::post('search', 'UserController#search_code');
Why are you using LIKE for an integer column? Just use a standard where clause on your query.
$customers = DB::table('news')->where('id', $request->search)->get();
I would also check the name of your search input, right now it is Search but you may want to lowercase it to just search.
Try the following things:
<form action="search" method="post" action="{{ url(/search) }}">
// pass the route in action attribute of form tag
Route::post('/search', 'UserController#search);
UserController:
public function search()
{
$Search = Input::get('Search');
// search logic here
}
Change from
return view('search' , compact('search', 'customers'));
To
return view('search')->with('customers',$customers);
Related
I want to fetch some data from an API and then pass it to a blade view.
but when i try to pass the data to the blade view i get an error:
Undefined variable. $result is undefined
The function I'm using in my Controller is:
public function getAll(Request $request){
$userInput = $request->input();
$response = Http::withHeaders(
[
"x-rapidapi-host"=> "xxxxxxxx",
"x-rapidapi-key"=> "xxxxxxxxx",
]
)->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput);
return view('list', ['result' => $response]);
}
and my blade file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>movies</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="m-2">
<form action="/getList" method="get">
<input class="form-control w-50 m-2" type="text" name="q" id="showName">
<input class="btn btn-primary m-2" type="submit" value="Search...">
</form>
</div>
{{$result['l']}}
</body>
</html>
I have tried to use foreach to loop through the array in the blade view but i had the same error so i changed the code to the code above but still having the same error!
and in my controller i have tried to use toArray(); with $response after reading the Laravel documentation for a while but still got the same error.
how can i pass an array of objects from the controller to the blade view?
I'm trying to display set of values(hashtags) which I get from DB to View through controller.
But I'm stuck at here in this controller. Not sure what function to use, view or With after getting DB values. What will be best option to use? and how to use it in view
here is my sample code of Controller class
<?php
namespace App\Http\Controllers;
use App\Models\Hashtag;
use Illuminate\Http\Request;
use App\Http\Requests\SearchHashtagRequest;
class BannedHashController extends Controller
{
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
//Stuck in here
}else{
}
return view('bannedhashtags.index');
}
}
Index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instagram Hashtags</title>
</head>
<body>
<h1>Check your Instagram hashtags</h1>
<form method="get" type="get" action="{{url('bannedhashtags')}}">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="query_hashtag">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
</form>
</body>
</html>
you can do it like below:
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
return view('bannedhashtags.index',['hashtags'=>$hastags]);
}
return view('bannedhashtags.index');
}
and in this case you need to check for $hastags variable in your html like below
#if(isset($hashtags))
your html code here if the hashtags variable is set
#endif
if what you are looking for is to filter the hashtags list it will be better to show the hashtags list (maybe using pagination if the list is too big) and then filter it by using ajax request.
So, I am trying to redirect the page from login.blade.php into halamanUtama.blade.php with a button in a form, but somehow it always shows page expired. I don't know why. Here is my source code:
login.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login</title>
<!--CSS-->
<link rel="stylesheet" href="{{ asset('css/styles.css') }}" type="text/css">
</head>
<body>
<div class="login-page-frame">
<div class="header-login-page-frame">
<h3>Login</h3>
</div>
<div class="inner-login-form-frame">
<form method="post" action="./home" class="login-form">
<input type="text" placeholder="username" name="login-username">
<br>
<input type="password" placeholder="pass" name="login-password">
<br>
<button type="submit" class="btn-login">Log In</button>
</form>
</div>
</div>
</body>
</html>
web.php
Route::get('/', function () {
return view('login');
});
Route::post('/home', 'loginController#index');
loginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class loginController extends Controller
{
//
public function index(){
return view("halamanUtama");
}
}
halamanUtama.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Halaman Utama</title>
</head>
<body>
Selamat datang
</body>
</html>
Please help.
check this steps:
your login route is better to be get
check your directory in resource/views
your submit form
...
have a look on tutorials like scotch.io, etc...
I'd like to use dynamic form.
and input name have to be array. then have to add array number by automatic for post.
The following is my code.
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title></title>
<!-- Scripts -->
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css"integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz"crossorigin="anonymous">
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form action="" method="post">
<div id="sample">
<button v-on:click="addNewItemForm">New Item</button>
<div v-for="(item, index) in items">
<span style="coursor:pointer" v-on:click="deleteItemForm(index)">close</span>
<div>
<div>number({{index+1}})</div>
<div>
<label>Title:</label>
<div>
<input type="text" name="title[{{index}}]" required placeholder="here is title" v-model="item.title">
<p class="alert-warning">{{item.title}}</p>
</div>
</div>
<div>
<label>Detail:</label>
<div>
<input type="text" name="detail[{{index}}]" required placeholder="here is detail" v-model="item.detail">
<p>{{item.detail}}</p>
</div>
</div>
</div>
</div>
<div class="text-center mt-3">
<button type="submit" class="btn btn-success px-3">Register</button>
</div>
</div>
</form>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</body>
</html>
<script>
var app = new Vue ({
name:'Sample-app',
el:'#sample',
data:{
items:[
{
title:'',
detail:''
}
]
},
methods:{
addNewItemForm(){
this.items.push({
title:'',
detail:''
})
},
deleteItemForm(index){
this.items.splice(index,1)
}
}
})
</script>
here is link in code pen.
You can check this form.
[https://codepen.io/takudemo/pen/NWPBYMe?fbclid=IwAR3bpfc3IuI6cz7Y02ZgGGetDkDIHb5aC4op4earENzKYIkftPE2ZzzI51Q]
and I tried like this.
<div>
<input type="text" name="title[{{index}}]" required placeholder="here is title" v-model="item.title">
<p class="alert-warning">{{item.title}}</p>
</div>
also detail.
but It's not working.
and The following is error message.
[Vue warn]: Error compiling template:
name="title[{{index}}]": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.
8 | <label>Title:</label>
9 | <div>
10 | <input type="text" name="title[{{index}}]" required="" placeholder="here is title" v-model="item.title">
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 | <p class="alert-warning">{{item.title}}</p>
12 | </div>
If you have any ideas that to solve it.
Please let me know.
First, if you want to set some attribute dynamically in VueJS use :.
Second, even if you try :name="title[index]" it won't work because you don't have title array in your data. You have only items array. Add title and detail arrays in 'data' if you want to set names from them.
But I suppose that you want to generate names with title and detail properties of the corresponding item.
So, instead of this: name="detail[{{index}}]" and name="detail[{{index}}]"
use this: :name="item.title" and :name="item.detail".
And also save something to title and detail in addNewItemForm to see the changes, for example, like this:
addNewItemForm(){
this.items.push({
title:'title' + (this.items.length+1),
detail:'detail' + (this.items.length+1)
})
The issue you are facing here is that you want to dynamically set a tag attribute. In order to achieve that you need the : or v-bind: syntax. On top of that an html attribute is always a string. Hence, having these 2 things in mind what you are looking for is:
<input type="text" :name="`title[${index}]`"
required="" placeholder="here is title" v-model="item.title">
P.S.:Having that said, I would reconsider using that name as it feels you are trying to use a javascript object while it is purely a string.
php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
</head>
<body>
<div class="welcome">
<h1>test page</h1>
<form action="{{ URL::route('dis') }}" method="post">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit">
</form>
</div>
</body>
</html>
display.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>display page</title>
</head>
<body>
<div class="welcome">
<h1>test page for display</h1>
</div>
</body>
</html>
Routes.php
<?php
Route::get('/', array('uses'=>'HomeController#showWelcome', 'as' => 'home'));
Route::group(array('before'=>'csrf'),function()
{
Route::post('/dis', array('uses'=>'HomeController#display', 'as' => 'dis'));
});
HomeController.php
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function display()
{
return View::make('display');
}
}
there is a error in laravel 5,
when i try to submit the form hello.html following error appears.
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
One thing I see is you need a CSRF token in the form. If you also install illuminate\html (which is not included now in Laravel 5) you can get it by using Form::open instead of creating the <form> tag in HTML as you are.
Alternatively you can add the token to a form by just calling csrf_field function:
{!! csrf_field() !!}