CRUD laravel edit page 404 - laravel

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

Related

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".

LARAVEL - Property [id] does not exist on this collection instance

What's the problem? I have records in DB.
The ressource route is Route::resource('dylan_dog', 'DylanDogController');
I looped each $dylan_dog record, provided by the controller(further below), in index.blade.php like this:
#foreach ($dylan_dog as $ddog)
<tr>
<td>{{ $ddog -> id }}</td>
<td>{{ $ddog -> name }}</td>
<td>{{ $ddog -> title }}</td>
<td>{{ $ddog -> biography }}</td>
<td>Edit</td>
<td>
<form action="{{ route('dylan_dog.destroy', $dylan_dog->id) }}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
</tr>
#endforeach
What is the catch?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Dylan_dog;
class DylanDogController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$dylan_dog = Dylan_dog::all();
return view('dylan_dog.index', compact('dylan_dog'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('dylan_dog.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = new Dylan_dog([
'name' => $request->get('name'),
'title'=> $request->get('title'),
'biography'=> $request->get('biography')
]);
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Thanks for your contribution');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//return view('dylan_dog.index', ['name' => Dylan_dog::findOrFail($id)]);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$dylan_dog = Dylan_dog::find($id);
return view('dylan_dog.edit', compact('dylan_dog'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = Share::find($id);
$dylan_dog->name = $request->get('name');
$dylan_dog->title = $request->get('title');
$dylan_dog->biography = $request->get('biography');
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Character has been updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$dylan_dog = Share::find($id);
$dylan_dog->delete();
return redirect('/dylan_dog')->with('success', 'Character has been deleted successfuly');
}
}
Error message "ErrorException (E_ERROR) Property [id] does not exist
on this collection instance. (View:
C:\xampp\htdocs\miljan\resources\views\dylan_dog\index.blade.php)"
Just noticed you are using $dylan_dog->id. $dylan_dog is a Collection and doesn't have id as attribute.Change it to $ddog->id:
<td>Edit</td>
<td>
<form action="{{ route('dylan_dog.destroy', $ddog->id) }}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
You should change the route paramters aswell to $dogg.

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);

Laravel cannot display a selected product details

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();

Resources