I have a lot of products from a database. I display them with a loop. When a user clicks "more", I want to display its details. The problem is, for example: I have four products A, B, C, and D. When a user clicks A's details, it displays D's details or randomly. How can I fix this? This is my code:
<form action="details" method="post" enctype="multipart/form-data">
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img class="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
<button type="submit" value="Submit" name="more" class="btn btn-primary ">More</button>
</div>
</div>
</div>
#endforeach
</form>
route
Route::post("details",'UserController#detail');
controller
public function detail(Request $req)
{
$productname = $req->input('imagename');
return $productname;
}
There is no need of using a form to accomplish what you want.
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img clas="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
More
</div>
</div>
</div>
#endforeach
Route:
Route::get("details/{imagename}",'UserController#detail');
Controller:
public function detail(Request $req)
{
$productname = $req->imagename;
return $productname;
}
UPDATE
The reason why is taking the last product is because you are sending all products through the form. and it takes the last one that sends, not the one you clicked.
view
#foreach($images as $image)
{{ $product->name }}
#endforeach
web.php
Route::get("details/{id}",'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return $product
}
Related
i have littel problem in my code, i dont know write mastake but i check my code is good
This ShopController
`
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show');
}
`
this my route
`
Route::get('/shop/detail/{id}', 'ShopController#show');
`
this my view
`
<div class="container">
<h2 class="title">{{$product->name}}</h2>
<hr>
<div class="row">
<div class="wrapper">
<div class="col-lg-4" id="picture">
<img src="{{asset($product->image)}}" alt="" height="200" width="200">
</div>
</div>
<div class="col-lg-4 desc">
<h4 id="description">Description</h4>
<p>{{$product->desc}}</p>
</div>
<div class="col-lg-4">
<div class="kartu">
<p>Harga</p>
<h2>Rp {{number_format($product->price)}}</h2>
<form action="" method="POST">
#csrf
<input type="hidden" value="" name="item_id">
<input type="submit" class="btn btn-primary" value="Add to Cart">
</form>
</div>
</div>
</div>
</div>
`
I have checked my code and there are no errors
You don't pass the product to the view. You need to compact the variable like this in the controller:
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show', compact('product'));
}
you are getting product from database in $product variable, but not passing that variable to your view. There are several ways to pass variables to view.
return view('shop.show', compact('product'));
OR
return view('shop.show', ['product' => $product]);
OR
return view('shop.show', get_defined_vars());
get_defined_vars() is built-in php function, by using this function any numbers of variables declared in method, all will be passed to view
I am trying to use the search box in my Laravel project but I keep getting the same error since morning
Here is my Search controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function index()
{
return view('search.index');
}
public function Search(Request $request)
{
$serial_number= $request->input('search');
$results =DB::table('animals')->where(function ($query) use ($serial_number) {
$query->where('serial_number','LIKE',"%$serial_number%");
})->latest()->get();
return view('search',compact('results'));
}
}
My routes
Route::get('/search','SearchController#index')->name('search');
Route::post('/search','SearchController#search')->name('search');
and finally my view
#extends('layouts.app')
#section('content')
<form action="{{ route('search') }}" method="POST">
<div class="p-1 bg-light rounded rounded-pill shadow-sm mb-4">
<div class="input-group">
<input type="search" name="search" placeholder="Here the animal serial number..." aria-describedby="button-addon1" class="form-control border-0 bg-light">
<div class="input-group-append">
<button id="button-addon1" type="submit" class="btn btn-link text-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</form>
#if($results)
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h3>Details for the animal</h3>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $results->id }}</p>
<p><strong>Animal: </strong>{{ $results->type->category }}</p>
<p><strong>Gender: </strong>{{ $results->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $results->user->address->city }}</p>
<p><strong>Farm: </strong>{{ $results->user->name }}</p>
<p><strong>Date: </strong>{{ $results->created_at }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
#endsection
For me, I think that the problem is in the controller on that line $result which makes the view give the error
419|Page Expired or "Facade\Ignition\Exceptions\ViewException
Undefined variable: results (View:
/Users/macair13/MeatracProject/resources/views/search/index.blade.php)"
You need to include the CSRF token or exclude that URL from the CSRF token check.
<form ...>
#csrf
...
</form>
Laravel 6.x Docs - CSRF
Also you are not passing a results variable to your 'search' view from the Controller's index method. You will need to check if $results isset in your view or pass it to your view.
public function view (Request $request)
{
$search=$request['search'] ?? "";
if($search !=""){
//where
$data=File::where('id','like','%'.$request->search.'%')
->orwhere('name','like','%'.$request->search.'%')
->orwhere('file','like','%'.$request->search.'%')->get();
}
else{
$data = File::all();
}
$p = compact('data','search');
return view('upload_data')->with($p);
// $data = File::all();
// return view('upload_data',compact('data'));
}
I want to show a sidebar that displays data from the database across all my pages in Laravel, but I keep getting this error:
Undefined variable: products (View:
C:\xampp\htdocs\shop\resources\views\pages\sidebar.blade.php) (View:
Sidebar
#extends('layouts.app')
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
Controller
public function index()
{
$product = Product::get();
return view('pages.sidebar', ['products' => $product]);
}
Route
Route::resource('sidebar','SidebarController');
app.blade.php
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
You can either do #include() or your can return it via a controller, you cannot do both. I think you might be a little mixed up with the structure of your project.
If you are using #include it isn't going to hit a controller. So in theory, you would need to include ['products' => $products] on every controller method.
Here is how I would do it:
sidebar.blade.php:
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
app.blade.php:
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
Create a new file (or use an existing one) for a home page inside the pages folder, we will call it home.blade.php
home.blade.php:
#extends('layouts.app')
Change the view you are returning in the controller to new home.blade.php file.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
$product = Product::get();
return view('pages.home', ['products' => $product]);
}
}
I finally have to use view composer to solve this problem. Thank you
My controller contain single function with multiple API calling when I returned the controller function to my view api data displayed automatically but i need to display the data while submit the form. Please suggest any solution.
My Controller Code
public function domaincheck(Request $request)
{
ini_set('max_execution_time', 300);
<!-- Domain Availability Check -->
$sld = $request['sld'];
$tld = $request['tld'];
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);
<!--Domain Name Suggestions -->
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&uid=resellid&pw=resellpw&SearchTerm='. $sld .'&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);
}
My view Code
<form class="form-horizontal" method="get">
<div class="form-group">
<div class=" col-lg-2"></div>
<div class="col-lg-8">
<div class="input-group m-b">
<span class="input-group-addon" style="padding-left:10px; background-color: #999;" class='unclickable'>www</span>
<input type="text" name="sld" class="form-control">
<span class="input-group-addon">
<select class="form-control" name="tld" style="width: 100px;">
<?php $j = 0; ?>
#foreach($final_data2['tldlist']['tld'] as $value)
<?php $j++; ?>
#endforeach
#for ($i = 0; $i < $j-1;)
<option value="{{($final_data2['tldlist']['tld'][$i]['tld'])}}">{{($final_data2['tldlist']['tld'][$i]['tld'])}}</option>
<?php $i++; ?>
#endfor
</select>
</span>
<span class="input-group-addon">
<button type="submit" class="btn btn-sm btn-success">Submit</button>
</span>
</div>
<p class="text-center text-dark customFont" >
#foreach($final_data as $key=>$value)
#if($key=='DomainName')
<b>{{$value}}</b> <b>-</b>
#endif
#if($key=='RRPText')
<b>{{$value}}</b>
#endif
#endforeach
</p>
{{print_r($final_data1)}}
When submit the form (Response) it shows the domain name is available or not available. Response1 is shows the domain name suggestions but it shows the data with out submit the form.Please suggest any solutions
I want to insert advertisements after each X rows returned from database.
My Controller looks like this:
class Home extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('parser');
$this->load->helper('url');
$this->load->helper('download');
$this->load->model('Categorii');
$this->load->model('Comentarii');
}
public function index($pages = ""){
$c_ar = $this->Categorii->iaCategorii();
$sql = (!empty($pages)) ? "SELECT * FROM bancuri WHERE categorie = '" . $c_ar[$pages] . "'" : "SELECT * FROM bancuri";
$dbQ = $this->db->query($sql);
$renderData = array("bancuri" => $dbQ->result_array(), "ad" => "<img src='http://placehold.it/350x150'>"); //Se stocheaza datele intr-o matrice
$this->parser->parse('pages/bancuri',$renderData);
}
And view looks like this:
<div class="col-lg-8 col-sm-12 col-xs-12">
{bancuri}
{ad}
<div class="corgi_feed_well">
<div class="individual_feed_item">
<div class="feed_item">
<div class="feed_body">
<div class="row">
<!--
<audio controls>
<source src="<?php echo $this->config->item('base_url'); ?>resources/mp3/{mp3}" type="audio/mpeg">
</audio>
-->
</div>
<div class="row">
<div class="feed_profile_pic">
<span class="label label-info">Rating: {rating}</span>
<div class="clearfix"> </div>
<button type="button" class="btn btn-success">Like</button>
</div>
<div class="feed_text">
<p class="well">{banc}</p>
</div>
</div>
</div>
<div class="comment_area">
<p>Adauga un comentariu</p>
<form>
<textarea rows="3" class="span6"></textarea><br/>
<input type="submit" class="btn" />
</form>
</div>
<hr class="feed_hr" />
<div class="bottom_meta">
<div class="row">
<div class="bottom_left">
<div class="share_wrapper">
<div class="share"><i class="icon-heart"></i></div>
<div class="share_hidden">
<ul class="hover_heart">
<span class="internal_heart"><i class="icon-heart"></i> Trimite bancul pe</span>
<div class="social_links">
<li><span><i class="icon-twitter"></i> Twitter</span></li>
<li><span><i class="icon-facebook"></i> Facebook</span></li>
<li><span><i class="icon-pinterest"></i> Pinterest</span></li>
</div>
</ul>
</div>
</div>
</div>
<div class="bottom_left">
Descarca <span class="label label-primary">{descarcari} descarcari</span>
{comentarii} comentarii<br>
</div>
<div class="bottom_right">
{autor} <span>|</span> Adauga comentariu <span>|</span> {data}
</div>
</div>
</div>
</div>
</div>
</div>
{/bancuri}
The problem is that at the moment, it displays the ad (simple image atm) for every database row.
Regardless of the code you posted, In short, you just add a counter and use modulo.
so something like
$arr = range('a','z');
$n = 0;
foreach ($arr as $item)
{
if($n % 4 ==0) // 4 equals to the interval between ads
echo 'print banner';
$n++ ;
}
this will print an ad, or whatever you want, every 4 lines, play around with the numbers as you see fit and add a zero check if you don't like ads to appear in the first line.