I want to show array of images in my blade view. I have a table which has some fields like name, address, images. where images are more than one how I can show multiple images in my blade-view.
<div class="business-gallery">
<div class="gallery">
{{-- {{dd($business->image)}} --}}
{{-- #json($business->image) --}}
<img src="{{$business['image'][0]}}" alt="Second image" />
</div>
</div>
for each loop etc doesn't work for me like this,I can even fetch/show single image on my view
#foreach($business['image'] as $image)
<image src="{{ $business['Name']}}" />
#endforeach
and my controller from where I save data to DB. there is no image model I am storing images directly to a business model
// Business insertion from user side
public function store(Request $request)
{
if ($request->hasFile('pictures')) {
foreach ($request->file('pictures') as $picture) {
$pictures[] = $fileName = time() . $picture->getclientOriginalName();
Storage::put('public/' . $fileName, file_get_contents($picture));
}
Business::create([
'image' => implode(',', $pictures),
'name' => $request->name,
'price' => $request->input('price'),
'description' => $request->input('description'),
'user_id' => $request->input('user'),
'category_id' => $request->input('category'),
'subCategory_id' => $request->input('subCategory'),
'province_id' => $request->input('province'),
'city_id' => $request->input('city'),
]);
}
return redirect('sell-business-form')->with('success', 'Your business is under review, Please wait for confirmation');
public function businessDetail($id)
{
$business = Business::findOrFail($id);
return view('front-end.business-detail', compact('business'));
}
please help me out
Related
The name of my controller is ProductController, This is my controller
public function store(Request $request)
{
$request->validate([
'product_name' => 'required',
'product_detail' => 'required',
'product_image' => 'required',
'admin_name' => 'required',
]);
$product_image = $request->file('product_image');
$new_name = rand().'.'.$product_image->getClientOriginalExtension();
$product_image->move(public_path('product_image'), $new_name);
$form_data = array(
'product_image' => $new_name,
);
// Product::create($form_data);
$product = Product::create([
'product_name' => $request->input('product_name'),
'product_detail' => $request->input('product_detail'),
'admin_name' => $request->input('admin_name'),
'product_image' => $new_name,
]);
return redirect('products')->with('success', 'Data Added successfully.');
}
This is my index page where I want to echo it, I have doubt in Product_detail
#foreach($products as $product)
<div class="col-lg-4 col-md-6 portfolio-item filter-Interior">
<div class="portfolio-wrap">
<div class="portfolio-info">
<h4>{{$product->product_name}}</h4>
<p>
<ul style="color: white">
<li>{{$product->product_detail}}</li>
</ul>
</p>
You can use php's inbuilt function nl2br() or you can explode by PHP_EOL and then run the for loop for unordered list.
<p>{!! nl2br($body) !!}<p>
<!-- OR -->
<ul>
#foreach (explode(PHP_EOL, $body) as $item)
<li>{{ $item }}</li>
#endforeach
</ul>
I have product and auction table. I want to add auction deadline on a specific table using form. when I submit the form the product_id in auction table is not populated and deadline shows time on which form is submited.
Here what I am trying:
I want that the create form get the deadline and store in auction table with product id so I can access it in show method of product.
create.blade.php
#extends('layouts.app')
#section('content')
<div class="mt-3" style="margin-left: 50px;">
<h2>Add new product</h2>
{!! Form::open(['action' => 'ProductsController#store', 'method' => 'POST', 'enctype' =>
'multipart/form-data', 'class' => 'w-50 py-3']) !!}
<div class="form-group">
{{Form::label('name', 'Product Name')}}
{{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Product Name'])}}
</div>
<div class="form-group">
{{Form::label('description', 'Product Description')}}
{{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Product Description', 'rows' => '4'])}}
</div>
<div class="form-group">
{!! Form::Label('category', 'Category') !!}
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
{{Form::label('price', 'Product Price')}}
{{Form::number('price', '', ['class' => 'form-control', 'placeholder' => 'Product Price'])}}
</div>
<div class="form-group">
{{Form::label('deadline', 'Auction Deadline')}}
{{Form::date('{{$auction->deadline}}', '', ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('image', 'Product Image')}}
{{Form::file('image', ['class' => '', 'placeholder' => 'Product Image'])}}
</div>
{{Form::submit('Upload Product', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
Product Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name', 'price', 'description', 'image',
];
public function category()
{
return $this->belongsTo('App\Category');
}
public function auction()
{
return $this->hasOne('App\Auction');
}
}
ProductsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Product;
use App\Category;
use App\Auction;
class ProductsController extends Controller
{
public function index()
{
$categories = Category::all();
$products = Product::with('category')->latest()->paginate(3);
return view('products.index' ,compact('categories', 'products'));
}
public function create()
{
$categories = Category::all(['id', 'name']);
return view('products.create', compact('categories',$categories));
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'category_id' => 'required',
'price' => 'required',
'image' => 'image|nullable',
]);
// Create Product
$product = new Product();
$product->name = request('name');
$product->description = request('description');
$product->category_id = request('category_id');
$product->price = request('price');
$product->image = $fileNameToStore;
$product->save();
$auction = new Auction();
$auction->deadline = request('deadline');
$auction->save();
return redirect('/products')->with('success', 'Product Created');
}
public function show($id)
{
$product = Product::find($id);
return view('products.show', compact('product'));
}
}
you are just creating auction, where is the relation? Delete $product->save(); line. After the $auction->save(); add this line:
$product->auction()->associate($auction);
$product->save();
Final Store method
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'category_id' => 'required',
'price' => 'required',
'image' => 'image|nullable',
]);
// Create Product
$product = new Product();
$product->name = request('name');
$product->description = request('description');
$product->category_id = request('category_id');
$product->price = request('price');
$product->image = $fileNameToStore;
$auction = new Auction();
$auction->deadline = request('deadline');
$auction->save();
$product->auction()->associate($auction);
$product->save();
return redirect('/products')->with('success', 'Product Created');
}
Update for Irrelevant Blade problem
This line is wrong:
{{Form::date('{{$auction->deadline}}', '', ['class' => 'form-control'])}}
You are using blade close string tag in blade string tag.
Should be:
{{Form::date('deadline', '', ['class' => 'form-control'])}}
If auction variable has been passing from controller this will work
I have a problem when import data xlsx using package fast excel.
I wanna input excel files include the Id from different model like following below
app/http/clustercontroller :
public function Import($id)
{
$model = Cluster::findOrFail($id);
return view('components.Admin.import', compact('model'));
}
public function StoreImport($id, Request $request)
{
//VALIDASI
$this->validate($request, [
'file' => 'required|mimes:xls,xlsx',
]);
if ($request->hasFile('file')) {
$file = $request->file('file'); //Get File
$collection = (new FastExcel)->import($file, function ($line) use ($id) {
return Soal::create([
'soal' => $line['Soal'],
'image' => $line['Image'],
'A' => $line['A'],
'B' => $line['B'],
'C' => $line['C'],
'D' => $line['D'],
'E' => $line['E'],
'kunci' => $line['Kunci'],
'cluster_id' => $id
]); //Import File
});
}
}
resource/admin/import.blade.php :
{!! Form::model($model, [
'route' => $model->exists ? ['cluster.soal.store', $model->id] : 'cluster.soal.create',
'method' => $model->exists ? 'POST' : 'POST',
'files' => true
]) !!}
<div class="form-group">
<label for="" class="control-label">Cluster</label>
{!! Form::text('cluster', null, ['class' => 'form-control', 'id' => 'cluster']) !!}
</div>
<div class="form-group">
<label for="" class="control-label">File .xlsx</label>
{!! Form::file('files') !!}
</div>
{!! Form::close() !!}
the code above displays the form, but when I click submit there is no response
You don't seem to return a response in your Controller method, so that's one reason I can think of. I assume your models are stored?
Things you could do to improve:
Return a view or, even better, redirection after finishing the import
Surround your code with try/catch blocks
I am trying to display 2 piechart in same view, this is my view code :
<div class="panel-body form-horizontal">
{{-- <div id="morris-donut-chart"></div> --}}
<div class="form-group">
<div id="chart-div"></div>
#piechart('tgt','chart-div')
</div>
<div class="form-group">
<div id="chart-div2"></div>
#piechart('capai','chart-div2')
</div>
</div>
both success display, but the second chart has no data.
Piechart
i think the variable of data table is not problem , when i use dd(), it's has data.
Data of Capai
my controller code :
public function statistik()
{
$lava = new Lavacharts;
$ket = array("totalTarget" => 0, "totalCapai" => 0);
$target_ = Target::select('grup')
->selectRaw("SUM(nilai) AS jumlah")
->groupBy('grup')
->get()
->toArray();
$data = \Lava::DataTable();
$data->addStringColumn('Target')
->addNumberColumn('Percent');
foreach($target_ as $row){
$data->addRow([$row['grup'], $row['jumlah'] ]);
$ket["totalTarget"] += $row['jumlah'];
}
\Lava::PieChart('tgt', $data, [
'title' => 'Target :',
'height' => '300',
'is3D' => true,
]);
$capai_ = Faktur::select('nama')
->selectRaw('SUM(qty) AS jumlah')
->groupBy('sub_group')
->get()
->toArray();
$data_capai = \Lava::DataTable();
$data_capai->addStringColumn('Pencapaian')
->addNumberColumn('Percent');
foreach($capai_ as $row_){
$data_capai->addRow([$row_['nama'], $row_['jumlah'] ]);
$ket["totalCapai"] += $row_['jumlah'];
}
// dd($data_capai);
\Lava::PieChart('capai', $data_capai, [
'title' => 'Pencapaian :',
'height' => '300',
'is3D' => true,
]);
return view('dashboard', compact('lava', 'ket'));
}
Is there something I miss?
I already fixed it.
All I need is to convert data to Float.
I am creating a CRUD of books and what I wanted to do is like the generated auth files that if someone registers and didn't input any in the textbox, a flash message will return. I am doing that now in my crud but I can only make a flash message when a book is successfully created. This is my store function
public function store(Request $request)
{
$this->validate($request, [
'isbn' => 'required|',
'title' => 'required',
'author' => 'required',
'publisher' => 'required'
]);
Session::flash('msg', 'Book added!');
$books = $request->all();
Book::create($books);
return redirect('books');
}
And in my home.blade.php
#if(Session::has('msg'))
<div class="alert alert-success">
{{ Session::get('msg') }}
</div>
#endif
This actually works but I want to show some ready generated error flash when someone didnt complete fields. How can I do that?
It's pretty simple, first there's a sweet nice feature that is the redirect()->with()
So your controller code could be:
public function store(Request $request)
{
$this->validate($request, [
'isbn' => 'required|',
'title' => 'required',
'author' => 'required',
'publisher' => 'required'
]);
if(Book::create($books)){
$message = [
'flashType' => 'success',
'flashMessage' => 'Book added!'
];
}else{
$message = [
'flashType' => 'danger',
'flashMessage' => 'Oh snap! something went wrong'
];
}
return redirect()->action('BooksController#index')->with($message);
}
Then on your view:
#if (Session::has('flashMessage'))
<div class="alert {{ Session::has('flashType') ? 'alert-'.session('flashType') : '' }}">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ session('flashMessage') }}
</div>
#endif
Bonus you can put this on your footer, so the alerts boxes will vanish after 3 seconds:
<script>
$('div.alert').delay(3000).slideUp(300);
</script>
You could get the individual errors for a field
{!! $errors->first('isbn'); !!}
or you can get all the errors
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
you can use try catch like this
try{
$books = $request->all();
Book::create($books);
Session::flash('msg', 'Book added!');
}
catch(Exception $e){
Session::flash('msg', $e->getmessage());
}