I have a another problem in my view.
This condition #if doesn't work:
#if( ! empty($news))
<div class="container">
<div class="row justify-content-center">
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
</div>
</div>
#else
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Last News :</h1>
<div class="col-md-8">
<div class="row">
#foreach($news as $new)
<div class="card" style="width: 18rem; margin-left:30px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#if(#admin)
Edit</td>
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
#endif
</div>
</div>
#endforeach
</div>
</div>
#endif
I want to show Any News ! when I don't have news and Last News ! when I have news
Can you help me?
Additional Info :
Model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
//
}
Controller :
<?php
namespace App\Http\Controllers;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$news=News::all();
return view('home',compact('news'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('news.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$news = new News();
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news')->with('success', 'Les Informations ont bien été ajoutées.');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$news = News::findOrFail($id);
return view('news');
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$news = News::find($id);
return view('news.edit',compact('news','id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$news= News::find($id);
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$news = News::find($id);
$news->delete();
return redirect('news')->with('success','La News à bien été supprimée.');
}
}
View :
#extends('layouts.app')
#section('content')
#if(empty($news))
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
#else
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
#foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#admin
Editer
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
#endadmin
</div>
</div>
#endforeach
</div>
</div>
#endif
#endsection
Routes :
<?php
/*
* Route Resource
*/
Route::resource('news', 'NewsController');
/*Route HOME*/
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::get('/news', 'NewsController#index')->name('news');
You can try:
#if($news->count())
//enter your code here if you have news
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
#foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
#if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
#else
{{$new['content']}}
#endif
</p>
More.
#admin
Editer
<form action="{{action('NewsController#destroy', $new['id'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
#endadmin
</div>
</div>
#endforeach
</div>
</div>
#else
//enter your code here if you don't have news
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
#endif
I hope it works for you
Related
Here this is the error
![Here this is the error][1]
====
I'm confused to make the page go directly to the shopping list. the error here says the shop variable is not defined in the loop to display items that have been added to the cart. but the data is already entered into the database
cart.blade.php
<div class="site-section">
<div class="container">
<div class="row mb-5">
<form class="col-md-12" method="post">
<div class="site-blocks-table">
<table class="table table-bordered">
<thead>
<tr>
<th class="product-thumbnail">Image</th>
<th class="product-name">Product</th>
<th class="product-price">Price</th>
<th class="product-quantity">Quantity</th>
<th class="product-total">Total</th>
</tr>
</thead>
<tbody>
#foreach( $shop as $customer )
<tr>
<th class ="row">{{ $loop->iteration }}</th>
<td>{{ $customer->image }}</td>
<td>{{ $customer->product }}</td>
<td>{{ $customer->price }}</td>
<td>{{ $customer->quantity }}</td>
<td>{{ $customer->total_price_prdt }}</td>
<td>
X
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</form>
</div>
<div class="row">
<div class="col-md-6">
<div class="row mb-5">
<div class="col-md-6 mb-3 mb-md-0">
<button class="btn btn-primary btn-sm btn-block">Update Cart</button>
</div>
<div class="col-md-6">
<button class="btn btn-outline-primary btn-sm btn-block">Continue Shopping</button>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label class="text-black h4" for="coupon">Coupon</label>
<p>Enter your coupon code if you have one.</p>
</div>
<div class="col-md-8 mb-3 mb-md-0">
<input type="text" class="form-control py-3" id="coupon" placeholder="Coupon Code">
</div>
<div class="col-md-4">
<button class="btn btn-primary btn-sm">Apply Coupon</button>
</div>
</div>
</div>
<div class="col-md-6 pl-5">
<div class="row justify-content-end">
<div class="col-md-7">
<div class="row">
<div class="col-md-12 text-right border-bottom mb-5">
<h3 class="text-black h4 text-uppercase">Cart Totals</h3>
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<span class="text-black">Subtotal</span>
</div>
<div class="col-md-6 text-right">
<strong class="text-black"></strong>
</div>
</div>
<div class="row mb-5">
<div class="col-md-6">
<span class="text-black">Total</span>
</div>
<div class="col-md-6 text-right">
<strong class="text-black"></strong>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary btn-lg py-3 btn-block"
onclick="window.location='checkout.html'">Proceed To Checkout</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
cart contorller
<?php
namespace App\Http\Controllers;
use App\addcart;
use Illuminate\Support\Facades\DB;
use App\cart;
use Illuminate\Http\Request;
use Auth;
use App\User;
class cartController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// return view('customers/singleshop');
}
public function addToCart(Request $request)
{
// $product = DB::select('select * from add_cart where id='.$id);
// $user = Auth::user();
// $ac = new addcart();
// $ac->product = $request->product;
// $ac->product_price = $request->price;
// $ac->item_model = $request->model;
// $ac->quantity = $request->qty;
// $ac->save();
// // $shop_id = $ac->id;
// return redirect('/cart');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$user = Auth::user();
$ac = new addcart();
$ac->product = 'tanktop';
// $ac->product = !empty($request->product) ? $request->product : 'default value';
$ac->product_price = 500.000;
$ac->item_model = $request->model;
$ac->quantity = $request->qty;
$ac->save();
// $shop_id = $ac->id;
return redirect('cart', $shop);
}
/**
* Display the specified resource.
*
* #param \App\cart $cart
* #return \Illuminate\Http\Response
*/
public function show(cart $cart)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\cart $cart
* #return \Illuminate\Http\Response
*/
public function edit(cart $cart)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\cart $cart
* #return \Illuminate\Http\Response
*/
public function update(Request $request, cart $cart)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\cart $cart
* #return \Illuminate\Http\Response
*/
public function destroy(cart $cart)
{
//
}
}
[1]: https://i.stack.imgur.com/3ukSj.png
Your cart controller will need to render the view or redirect to the route that does render the view with the shop variable. I expected this to be your index. As your index is not defined, you are able to add the shop variable to the view and bind it as below. I am to believe that your shop is the $ac variable. Also, I assume that your blade view is /resources/views/customers.cart.blade.php based on your follow up comment. If $ac is not your list of products, please replace $ac with that expected list.
public function store(Request $request)
{
$user = Auth::user();
$ac = new addcart();
$ac->product = 'tanktop';
// $ac->product = !empty($request->product) ? $request->product : 'default value';
$ac->product_price = 500.000;
$ac->item_model = $request->model;
$ac->quantity = $request->qty;
$ac->save();
// $shop_id = $ac->id;
return view('customers/cart',['shop'=>$ac]);
}
When you want redirect to other page and you want use a new varaible, you MOST make the variable available for this you most use compact, with methods.
my proposal is:
In your Controller/#store
return redirect()->route('Merchant view')->with( ['shop => $shop] );
It will work
I have really been with something for hours that apparently according to what I have read is easy but I cannot realize it. I pass my code structure to you so that you can see it and at the end I tell you what the problem is and the error it throws.
Controller: RoleController
<?php
namespace App\Http\Controllers;
use App\Models\Role;
use App\Http\Requests\Role\StoreRequest;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
class RoleController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
echo 'Vista index de Roles';
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('theme.backoffice.pages.role.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(StoreRequest $request, Role $role)
{
$role = $role->store($request);
return redirect()->route('backoffice.role.show'.$role);
}
/**
* Display the specified resource.
*
* #param \App\Models\Role $role
* #return \Illuminate\Http\Response
*/
public function show(Role $role)
{
return view('theme.backoffice.pages.role.show', [
'role' => $role,
]);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Role $role
* #return \Illuminate\Http\Response
*/
public function edit(Role $role)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Role $role
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Role $role)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Role $role
* #return \Illuminate\Http\Response
*/
public function destroy(Role $role)
{
//
}
}
web.php/routes
Auth::routes(['verify' => true]);
//BACKOFFICE
Route::group(['middleware'=>['auth'], 'as'=>'backoffice.'], function(){
Route::resource('role', 'App\Http\Controllers\RoleController');
});
create.blade.php
#extends('theme.backoffice.layouts.admin')
#section('title','Crear rol')
#section('head')
#endsection
#section('content')
<div class="section">
<p class="caption">Introduce los datos para crear un nuevo rol</p>
<div class="divider"></div>
<div id="basic-form" class="section">
<div class="row">
<div class="col s12 m8 offset-m2">
<div class="card-panel">
<h4 class="header2">Crear rol</h4>
<div class="row">
<form class="col s12" method="post" action="{{ route('backoffice.role.store') }}">
{{ csrf_field() }}
<div class="row">
#error('name')
<span style="color:red" class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<div class="input-field col s12">
<input id="name" type="text" name="name">
<label for="name">Nombre</label>
</div>
</div>
<div class="row">
#error('description')
<span style="color:red" class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<div class="input-field col s12">
<textarea name="description" class="materialize-textarea" id="description" cols="30" rows="10"></textarea>
<label for="email">Descripcion</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<button class="btn waves-effect waves-light right" type="submit">Guardar
<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts-foot')
#endsection
show.blade.php
#extends('theme.backoffice.layouts.admin')
#section('title','Clinic')
#section('head')
#endsection
#section('content')
<div class="section">
<p class="caption"><strong>Rol: </strong>{{$role->name}}</p>
<div class="divider"></div>
<div id="basic-form" class="section">
<div class="row">
<div class="col s12 m8 offset-m2">
<div class="card-panel">
<h4 class="header2">Usuarios con el rol de {{$role->name}}</h4>
<div class="row">
<ul>
<li>Raul</li>
<li>Tomas</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts-foot')
#endsection
I tell you what my code does.
1-we create a new role in the CREATE view and we give it save.
2-This travels to the store method and saves it in the database
3- the store method returns a redirection to the route of the SHOW method so that the new created role is shown in said view.
Error:: Missing required parameter for [Route: backoffice.role.show] [URI: role/{role}] [Missing parameter: role].
I don't understand what I'm doing wrong since I'm passing the required parameter to the path name in the store method when it does the redirect. This is about looking for a different resolution or do I have an error which I am not realizing? . thank you very much for your attention.
The problem is the $role is not received for backoffice.role.show.
You can pass the expecting role like (problem was you were appending with '.' instead of ','):
return redirect()->route('backoffice.role.show', $role);
If you want to expliclity define role, you can use alias like:
return redirect()->route('backoffice.role.show', ['role' => $role]);
Edit For Store Method
You could use the Resource Controller in routes/web.php.
public function store(Request $request){
// Validation
$this->validate($request, ['name' => 'required']);
// Storing
$role = Role::create($request);
return redirect()->route('backoffice.role.show', $role);
}
For more information, you can visit Laravel Generating Url to Named Route
N.B: I'm using laravel 7.x,
I want to insert photo to my database but the error says, General error: 1364 Field 'banner_photo' doesn't have a default value
Here is my index.blade.php file
#extends('admin.layout.master')
#section('content')
<div class="container-scroller">
<!-- partial:partials/_navbar.html -->
#include('admin.layout.nav')
<!-- partial -->
<div class="container-fluid page-body-wrapper">
<!-- partial:partials/_sidebar.html -->
#include('admin.layout.sidebar')
<!-- partial -->
<div class="main-panel">
<div class="content-wrapper">
<!-- Page Title Header Starts-->
<div class="row page-title-header">
<div class="col-12">
<div class="page-header">
<h4 class="page-title">Dashboard</h4>
</div>
</div>
</div>
<!-- Page Title Header Ends-->
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 grid-margin">
<div class="card">
<div class="card-body">
<div class="d-flex justify-content-between">
<h4 class="card-title mb-0">Banner</h4>
<small>Show All</small>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est quod cupiditate esse fuga</p>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>H4 Title</th>
<th>H2 Title</th>
<th>Paragraph</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach($banners as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->h4_title}}</td>
<td>{{$row->h2_title}}</td>
<td>{{$row->banner_paragraph}}</td>
<td>{{$row->banner_image}}</td>
<td><button type="submit" class="btn btn-primary"><i class="fas fa-edit"></i>EDIT</button></td>
<td><button type="submit" class="btn btn-danger"><i class="far fa-trash-alt"></i>DELETE</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
<!-- partial:partials/_footer.html -->
#include('admin.layout.footer')
<!-- partial -->
</div>
<!-- main-panel ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->
#endsection
Here is admin panel bannercustomize.blade.php
#extends('admin.layout.master')
#section('content')
<div class="container-scroller">
#include('admin.layout.nav')
<div class="container-fluid page-body-wrapper">
#include('admin.layout.sidebar')
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-md-12 d-flex align-items-stretch grid-margin">
<div class="row flex-grow">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Banner Customize</h4>
#if(count($errors) >0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
#if(\Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<p>{{\Session::get('success')}}</p>
</div>
#endif
<form method="post" action="{{route('store.banner')}}" class="form-sample" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="exampleInputEmail1">H4 Title</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter H4 Title" name="h4_title">
</div>
<div class="form-group">
<label for="exampleInputEmail1">H2 Title</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter H2 Title" name="h2_title">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Paragraph</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter Paragraph" name="banner_paragraph">
</div>
<div class="form-group">
<input type="file" id="exampleInputEmail1" name="banner_photo">
</div>
<button type="submit" class="btn btn-success">Submit</button>
<button type="submit" class="btn btn-light">Cancle</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#include('admin.layout.footer')
</div>
</div>
</div>
#endsection
Here is my route web.php file
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('index');
});
Route::get('/contact-us', function () {
return view('contactus');
});
Route::get('/tours', function () {
return view('tours');
});
// Admin panel Pages Routes
Route::get('/admin', function () {
return view('admin/index');
});
Route::get('/admin/bannercustomize', function () {
return view('admin/layout/bannercustomize');
});
// Controller routes
// Route::resource('banners', 'BannerController#index');
// // Route::get('/', 'BannerController#index');
Route::post('store/banner','BannerController#store')->name('store.banner');
Route::get('/admin','BannerController#index')->name('admin.index');
Here is my controller BannerController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Banner;
class BannerController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $banners = Banner::all()->toArrary();
// return view('admin.index', ['banners' =>$banners]));
$banners = Banner::all();
return view('admin.index',compact('banners'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.layout.bannercustomize');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'h4_title' => 'required',
'h2_title' => 'required',
'banner_paragraph' => 'required',
]);
$banner = new Banner([
'h4_title' => $request->get('h4_title'),
'h2_title' => $request->get('h2_title'),
'banner_paragraph' => $request->get('banner_paragraph'),
]);
if ($request->has('banner_image')) {
$image = $request->file('banner_image');
$name = hexdec(uniqid()).'_'.time();
$folder = '/image/banner_image/';
$filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
$this->uploadOne($image, $folder, 'public', $name);
$banner->banner_image = $filePath;
}
$banner->save();
return redirect()->back()->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Please help me to find the error & the solution of this error
You have to change
banner_image
to
banner_photo
Change
if ($request->has('banner_photo')) {
$image = $request->file('banner_photo');
....
}
I need to get the values selected for a location form and add it's value to a cart. All the other information in the form is gathered correctly. Here is the code in the form:
<section class="cards">
#foreach($rentals as $rental)
<article class="cards-item">
<figure class="card product">
<div class="card-images">
<img src="{{ asset('img/rentals' . $rental->image) }}" alt="" >
</div>
<figcaption class="card-content">
<h1 class="card-title">{{ $rental->title }}</h1>
<h2 class="card-title product_title">{{ $rental->name }}</h2>
<p class="card-text" style="text-align:left;">{{ $rental->description }}
{{-- Download Spec Sheet --}}
</p>
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bkf">Bakersfield</option>
<option value="ch">Chico</option>
<option value="fsn">Fresno</option>
<option value="hyw">Hayward</option>
<option value="mtc">Manteca</option>
<option value="oak">Oakley</option>
<option value="rwc">Redwood City</option>
<option value="sac">Sacramento</option>
<option value="sal">Salinas</option>
<option value="sj">San Jose</option>
<option value="sjf">San Jose Fusion</option>
<option value="sr">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
{{-- <input type="hidden" name="pickup-date" value="pickupDate">
<input type="hidden" name="return-date" value="returnDate"> --}}
<input type="hidden" name="location" value="{{ $rental->location }}">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
</figcaption>
</figure>
</article>
</section>
</article>
#endforeach
And here is the code in the cart view:
<section class="main-content">
<section class="content">
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{$item->pickupDate}} </p>
<p class="rtn-date">Return Date: {{$item->returnDate}}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
I my controller I have these values set:
namespace App\Http\Controllers;
use App\Rental;
use Illuminate\Http\Request;
use Session;
use Gloudemans\Shoppingcart\Facades\Cart;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('cart.cart');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location)
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
Cart::remove($id);
Session::flash('success', 'The item has been removed!');
return redirect()->route('cart.index');
}
My routes are as follows:
Route::get('rental_equipment', 'PageContentController#rental_equipment')->name('rental equipment');
Route::resource('rentals','RentalController');
Route::get('/cart', 'CartController#index')->name('cart.index');
Route::post('/cart', 'CartController#store')->name('cart.store');
Route::delete('/cart/{id}', 'CartController#destroy')->name('cart.destroy');
Not sure what I am doing wrong here. Any help is much appreciated.
I want to make a button on my main index to redirect to another view.
index.blade.php :
#extends('layouts.default')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Meniu management locatari</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('itemCRUD.create') }}"> Creaza intrare noua</a>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{URL::to('itemCRUD/finalu') }}"> Insert excel</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>no</th>
<th>apartament</th>
<th>nume</th>
<th>persoane</th>
<th>mp</th>
<th>comentariu</th>
<th width="280px">Actiune</th>
</tr>
#foreach ($items as $key => $item)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $item->apartament }}</td>
<td>{{ $item->nume }}</td>
<td>{{ $item->persoane }}</td>
<td>{{ $item->mp }}</td>
<td>{{ $item->comentariu }}</td>
<td>
<a class="btn btn-info" href="{{ route('itemCRUD.show',$item->id) }}">Arata</a>
<a class="btn btn-primary" href="{{ route('itemCRUD.edit',$item->id) }}">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['itemCRUD.destroy', $item->id],'style'=>'display:inline']) !!}
{!! Form::submit('Sterge', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
{!! $items->render() !!}
#endsection
But when I press the Insert excel button to redirect me to this view
finalu.blade.php :
#extends('layouts.default')
#section('content')
<h1>Helooo</h1>
#endsection
I will get this ErrorException:
ErrorException in bcebe4d151d0a3c5c8008307bf179799c7fba86b.php line 20:
Trying to get property of non-object (View: /var/www/html/admintmhelp/resources/views/ItemCRUD/show.blade.php)
Also this is my show.blade.php :
#extends('layouts.default')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Arata intrarea</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('itemCRUD.index') }}"> Inapoi</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>apartament:</strong>
{{ $item->apartament }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>nume:</strong>
{{ $item->nume }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>persoane:</strong>
{{ $item->persoane }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>mp:</strong>
{{ $item->mp }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>comentariu:</strong>
{{ $item->comentariu }}
</div>
</div>
</div>
#endsection
And this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Item;
class ItemCRUDController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$items = Item::orderBy('id','DESC')->paginate(5);
return view('ItemCRUD.index',compact('items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('ItemCRUD.create');
}
public function finalu()
{
return view('ItemCRUD.finalu');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'apartament' => 'required',
'nume' => 'required',
'persoane' => 'required',
'mp' => 'required',
]);
Item::create($request->all());
return redirect()->route('itemCRUD.index')
->with('success','Intrare inserata cu succes');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$item = Item::find($id);
return view('ItemCRUD.show',compact('item'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$item = Item::find($id);
return view('ItemCRUD.edit',compact('item'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'apartament' => 'required',
'nume' => 'required',
]);
Item::find($id)->update($request->all());
return redirect()->route('itemCRUD.index')
->with('success','Intrare actualizata cu succes');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
Item::find($id)->delete();
return redirect()->route('itemCRUD.index')
->with('success','Item deleted successfully');
}
}
Route is set like :
Route::resource('itemCRUD','ItemCRUDController');
What can be the reason for that ErrorException, and how I can avoid that ?
Because you are using a resource controller itemCRUD/finalu is always going to route to itemCRUD/show/{id}
To get around this, you may need to add in another route.
Route::get('itemCRUD.finalu','ItemCRUDController#finalu');
Route::resource('itemCRUD','ItemCRUDController');