Laravel cannot display a selected product details - laravel

maybe someone can help me. I have done a product list from my database. It shows product image, title, content, price
When I select on one of the products, it opens in the new window. And I cant see any product details (no image, no title, no content) , just empty page.
See all attachments:
my BrackController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\file;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Collection;
class BracController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('insertBrac');
}
public function showBrac()
{
$user=file::all();
return view('apyrankes', compact('user'));
}
public function view_brac()
{
$object = \DB::table('braclets')->where('BrackID' , request('brackId'))->first();
return view('view_object', array('user' => $object));
}
/**
* 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 = new file;
$user->Title= Input::get('title');
$user->Content= Input::get('content');
$user->Price= Input::get('price');
if (Input::hasFile('image'))
{
$file=Input::file('image');
$file->move(public_path(). '/uploads', $file->getClientOriginalName());
$user->Image = $file->getClientOriginalName();
}
$user->save();
return redirect( "insertBrac" );
}
}
/**
* 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)
{
//
}
}
My product list:
apyrankes.blade.php
<div class="header">
#include('header')
</header>
<body>
<div class="content-products">
#foreach($user as $users)
<div id="myDiv">
<div class="products">
<img src="{{ $users->Image}}" alt="{{ $users->Title}}" width="200px" height="200px"><br>
{{ $users->Title}}<br>
{{ $users->Content}}<br>
{{ $users->Price}}€<br>
Plačiau
</div>
</div>
#endforeach
</div>
</body>
And when I select one of the product from product list on apyrankes.blade.php
it opens in view_object.blade.php , but I cannot see there any details of the select product, just empty page:
My view_object.blade.php
<div class="header">
#include('header')
</header>
<body>
<Br>
<br>
<Br>
<div class="content-products">
<div id="myDiv">
<div class="products">
#if($user)
<img src="{{ $user->Image}}" alt="{{ $user->Title}}" width="200px" height="200px"><br>
{{ $user->Title}}<br>
{{ $user->Content}}<br>
{{ $user->Price}}€<br>
#endif
</div>
</div>
</div>
</body>

Because you did
Plačiau</div>
The name of the variable you need to fetch is product
$object = \DB::table('braclets')->where('BrackID' , request('product'))->first();

Related

CRUD laravel edit page 404

i am trying to edit my categories via crud edit,
I think I did everything right both in the controller and in the routes but I keep getting error 404 page not found, as soon as I click on the button to change the category
this is my controller
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$category=Category::all();
return view('categorie.categorie', compact('category'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('categorie.crea');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $req)
{
$category = Category::create([
'nome'=>$req->nome
]);
return redirect()->back()->with('message', 'Categoria inserita correttamente');
}
/**
* Display the specified resource.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function edit(Category $category,$id)
{
$category=Category::findOrFail($id);
dd($category);
return view('categorie.editcat', compact('category'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
{
$category=Category::updated([
'nome'=>$request->nome,
]);
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
this is route
Route::get('/categorie',[CategoryController::class,'index'])->name('categorie.categorie');
Route::get('/categorie/create',[CategoryController::class,'create'])->name('categorie.crea');
Route::post('/categorie/store',[CategoryController::class,'store'])->name('categorie.store');
Route::get('/categorie/store/{$id}',[CategoryController::class,'edit'])->name('categorie.edit');
Route::post('/categorie/update/{$id}',[CategoryController::class,'update'])->name('categorie.update');
and this is blade with form for edit
<x-layout>
<div class="container text-center">
<div class="row justify-content-center">
<div class="col-12 col-md-12 justify-content-center">
<h1>Categorie</h1>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">NOME</th>
<th scope="col">STATO</th>
<th scope="col">AZIONI</th>
</tr>
</thead>
<tbody>
#foreach ($category as $cat)
<tr>
<th scope="row">{{$cat->id}}</th>
<th>{{$cat->nome}}</th>
<th>--</th>
<th><i class="fas fa-plus-circle"></i>
<form action="{{route('categorie.edit',$cat->id)}}" method="GET">
<button type="submit"><i class="fas fa-plus-circle"></i></button>
</form></th>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</x-layout>
any solution?
you have to change this uri '/categorie/store/{$id}' to '/categorie/store/{id}'
and do that in update route

Laravel - Return ID to database when selecting a name from dropdown

I have a student notes view where a teacher can enter notes for a specific student. On this view there is a dropdown box for the students name and a text field for the note. I currently am able to view the students names from the students table but am unsure how to return the ID number linked to the name (The student ID) to the notes table?
Notes View:
#section('content')
<section>
<h1>Student Notes</h1>
<form action="{{ route('notes.store') }}" method="POST">
#csrf
<div class="form-col">
<div class="input-group">
<label for="student_name">Student Name:</label>
<select id="student_name" name="student_name" required>
<option value="">--- Select Name ---</option>
#foreach ($student as $key => $value)
<option value="{{ $key }} "> {{ $value->first_name}} {{ $value->last_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-col">
<div class="input-group">
<label for="note">Notes:</label>
<textarea id="note" name="note" spellcheck="true" placeholder="Enter notes here"></textarea>
</div>
<div class="input-group">
<input id="saveForm" name="saveForm" type="submit" value="Submit" class="submit-btn">
</div>
</div>
</form>
</section>
#endsection
Notes Controller:
<?php
namespace App\Http\Controllers;
use App\Models\Note;
use App\Models\Student;
use Illuminate\Http\Request;
class NotesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$student=Student::all();
//return $student;
return view('pages.notes.Notes', ['student'=>$student]);
}
/**
* 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)
{
Note::create($request->all());
return redirect('notes');
}
/**
* Display the specified resource.
*
* #param \App\Models\Note $note
* #return \Illuminate\Http\Response
*/
public function show(Note $note)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Note $note
* #return \Illuminate\Http\Response
*/
public function edit(Note $note)
{
return view('pages.notes.Notes_edit', compact('note'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Note $note
* #return \Illuminate\Http\Response
*/
public function update($id, Request $request)
{
$notes = Note::query();
if ($notes->where('id', $id)->exists()) {
$note = $notes->find($id);
$note->student_name = is_null($request->student_name) ? $note->student_name : $request->student_name;
$note->note = is_null($request->note) ? $note->note : $request->note;
$note->save();
return redirect('notes');
}
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Note $note
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$notes = Note::query();
if($notes->where('id', $id)->exists()) {
$note = $notes->find($id);
$note->delete($id);
return redirect('notes');
}
}
}
You should us your either or Data Table,
Example:
If you does have iStudentID , iStudentFName, iStudentLName in your Data Model,
you could call the ID with code as below:
#foreach ($student as $value)
option value="{{ $value->iStudentID }} "> {{ $value->iStudentFName}} {{ $value->iStudentLName}}</option>
#endforeach

How to allow guest users to view posts, without having to login

I am trying to make Guests users i.e users that are not logged in to view some posts.
I have tried to add Guest Auth but it is not working. Also i will love to get the URL link to the post as slug instead of ID
E.g localhost:8000/news/1 should be localhost:8000/news/post-title
<?php
namespace App\Http\Controllers;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
function __construct()
{
$this->middleware('auth', ['except' => ['index']]);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$news= News::paginate(15);
return view('categories.news',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)
{
//validate
$this->validate($request,[
'subject'=>'required|min:10',
'body' => 'required|min:20'
]);
//store
auth()->user()->news()->create($request->all());
//redirect
$news= News::paginate(15);
return view('categories.news', compact('news'));
}
/**
* Display the specified resource.
*
* #param \App\News $news
* #return \Illuminate\Http\Response
*/
public function show(News $news)
{
return view('news.single', compact('news'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\News $news
* #return \Illuminate\Http\Response
*/
public function edit(News $news)
{
return view('news.edit', compact('news'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\News $news
* #return \Illuminate\Http\Response
*/
public function update(Request $request, News $news)
{
if(auth()->user()->id !== $news->user_id){
abort(401, "Please Login");
}
$this->validate($request,[
'subject'=>'required|min:10',
'body' => 'required|min:20'
]);
$news->update($request->all());
return redirect()->route('news.show', $news->id)->withMessage('News Updated');
}
/**
* Remove the specified resource from storage.
*
* #param \App\News $news
* #return \Illuminate\Http\Response
*/
public function destroy(News $news)
{
if(auth()->user()->id !== $news->user_id){
abort(401, "Please Login");
}
$news->delete();
$news= News::paginate(15);
return view('categories.news', compact('news'));
}
}
This is the single post blade that i will like guest users to be able to view.
<div class="main-body">
<div class="clearfix">
<div class="main-body-content text-left">
<h1 style="font-size: 20px;font-weight: 800;" class="post-title">{{$news->subject}}</h1>
<h5>By {{ Auth::user()->name }}</h5>
<div class="post-body">
<p>{!! $news->body !!}</p>
</div>
#if(auth()->user()->id==$news->user_id)
<div class="all-edit">
<div class="post-body">
(Edit)
</div>
<div class="delete">
<form action="{{route('news.destroy',$news->id)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<input type="submit" value="Delete">
</form>
</div>
</div>
#endif
</div>
</div>
</div>
you have to remove this route from middleware of "auth".

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'zoneintervention_id' in 'field list'

I've already made an connection many to many. The code below explains what I did. I have retrieved the intervention zones in a multiple choice combobox and now I'm trying to insert them in the database.When I try to choose more than one zone the error message (in the title) is displayed. I'd be grateful if someone could help me with this.
Mode 1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class technicien extends Model
{
public function zoneintervention()
{
return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','z
oneintervention_id');
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
Model 2
namespace App;
use Illuminate\Database\Eloquent\Model;
class zoneintervention extends Model
{
public function techniciens()
{
return $this->belongsToMany('App\technicien','technicien_zone','zoneintervention_id','technicien_id');
}
}
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\technicien;
use App\zoneintervention;
class TechnicienController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listzone=zoneintervention::orderBy('code_postal')->get();
$Listtechnicien=technicien::all();
return view('technicien.index',['technicien'=>$Listtechnicien]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$zoneintervention = Zoneintervention::orderBy('id', 'desc')->get();
return view('technicien.create')->with('zoneintervention', $zoneintervention);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$technicien = new technicien();
$technicien ->actif =$request->input('actif');
$technicien ->moyenne_avis =$request->input('moyenne_avis');
$technicien ->zoneintervention_id= $request->input('zoneintervention_id');
$technicien->save();
return redirect('technicien');
}
/**
* 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)
{
//
}
}
View
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('technicien') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="zoneintervention">zoneintervention</label>
<select name="zoneintervention_id"
id="zoneintervention" class="form-control" multiple="multiple">
#foreach($zoneinterventions as
$zoneintervention)
<option value="{{ $zoneintervention->id }}">
{{$zoneintervention->code_postal}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-
control"value="{{old('moyenne_avis')}}">
</div>
<div class="form-group">
<label for="">Etat</label>
<input type="text" name ="actif" class="form-
control"value="{{old('actif')}}">
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
technicien_zone
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTechnicienZone extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('technicien_zone', function (Blueprint $table){
$table->integer('technicien_id')->unsigned();
$table->foreign('technicien_id')->references('id')->on('techniciens');
$table->integer('zoneintervention_id')->unsigned();
$table->foreign('zoneintervention_id')->references('id')->on('zoneinterventions');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('technicien_zone');
}
}
My Data Base]1
With many to many relationship you can't do this:
$technicien->zoneintervention_id= $request->input('zoneintervention_id');
Use the attach() method instead to attach an object to other one:
$technicien = new technicien();
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request->zoneintervention_id);
return redirect('technicien');

Blocking error Laravel Invalid argument supplied for foreach()

I have two tables, a table "métier" and a table "tâche" with one to many connection, a "métier" has several "tâche". In my form "ajouter tâches" I would like to have a combobox from which I choose the "métier" associated with the "tâche". These are my tables and how I proceeded and the error I got.
Model1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
public function metier()
{
return $this->belongsTo(Metier::class);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
Model2
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}
Controller1
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listtache=tache::all();
return view('tache.index',['tache'=>$Listtache]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('id', 'desc')->get();
return view('tache.create')-> with('metiers');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id =$request->input('metier_id');
$tache->save();
return redirect('tache');
}
/**
* 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)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}
/**
* 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)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
Controller2
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Metier;
class MetierController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listmetier=metier::all();
return view('metier.index',['metier'=>$Listmetier]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('metier.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$metier = new Metier();
$metier ->libelle_metier =$request->input('libelle_metier');
$metier->save();
return redirect('metier');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(metier $id)
{
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$metier =Metier::find($id);
$metier->delete();
return redirect('metier');
}
}
View
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('tache') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Libelle Tache</label>
<input type="text" name ="libelle_tache" class="form-
control"value="{{old('libelle_tache')}}">
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-
control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<label for="metier">metier</label>
<select name="metier_id" id="metier">
#foreach($metiers as $metier)
<option value="{{$metiers}}">
{{$metiers->libelle_metier}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
#endsection
You're getting the error because you do not pass $metiers to the view. So, change the code to:
return view('tache.create')->with('metiers', $metiers);

Resources