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.
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?
When clicked submit button the page redirects from (localhost/cms/public/posts/create) to (localhost/posts) and then error page shows up. Was trying to display the input text using Controller. Below is my code
web.php
Route::resource('/posts','PostsController');
PostController.php
public function create()
{
//
return view ('posts.create');
}
public function store(Request $request)
{
//
return $request->get('title');
}
views/posts/create.blade.php
<html>
<head>
<title></title>
</head>
<body>
<h1>Form</h1>
<form method="post" action="/posts">
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
</body>
</html>
error page
Action should be the name of page ur are trying to show
Try <action ="PostController.php">
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`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);
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() !!}