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.
Related
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
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
so here is my question, i have done everything and my senior request me to replace the parent_id now into description as the coder know what is the integer number represent but the users doesn't know. Here is the picture
My current view looks like !
As you can see inside the red column, there are two id :
1.( 999162, Testing3, Test3, 999161, active ) and
2.( 999163, testing4, test, 999162, active )
My desired output is the 1.( 999161 calls the 999161 description instead of id ).
Lets take 999163 as example : the desired output should be like 999163, testing4, test, test3, active.
I don't know how to call the description to replace the parent_id,can someone help ?
<div class="row">
<div class="col-md-12">
<br />
<h3 align="center">Category Data</h3>
<br />
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
<div align="right">
Add
<br />
<br />
</div>
<table class="table table-bordered table-striped">
<tr>
<th>Id</th>
<th>Code</th>
<th>Description</th>
<th>Parent</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
#foreach($category as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['code']}}</td>
<td>{{$row['description']}}</td>
<td>{{$row['parent_id']}}</td>
<td>{{$row['status']}}</td>
<td>Edit</td>
<td>
<form method="post" class="delete_form" action="{{action('categoryController#destroy',$row['id'])}}">
{{ csrf_field() }}
{{ method_field('DELETE')}}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
Here is my categoryController.php coding
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class categoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$category =Category::all()->toArray();
return view('category.index',compact('category'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'code' => 'required',
'description' => 'required',
'parent_id' => 'required',
'status' => 'required',
]);
$category = new Category([
'id' => $request->get('id'),
'code' => $request->get('code'),
'description' => $request->get('description'),
'parent_id' => $request->get('parent_id'),
'status' => $request->get('status'),
]);
$category->save();
return redirect()->route('category.create')->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)
{
$category = Category::find($id);
return view('category.edit', compact('category','id'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all
return view('category.edit',compact('parents'));
}
public function subRequest()
{
return view('subRequest');
}
public function subRequestPost()
{
$input = request()->all();
return response()->json(['success'=>'Got Submit Request.']);
}
/**
* 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,[
'code' =>'required',
'description' =>'required',
'parent_id'=>'required',
'status'=>'required'
]);
$category = Category::find($id);
$category->code =$request->get('code');
$category->description =$request->get('description');
$category->parent_id =$request->get('parent_id');
$category->status =$request->get('status');
$category->save();
return redirect()->route('category.index')->with('success','Data Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = Category::find($id);
$category->delete();
return redirect()->route('category.index')->with('success','Data Deleted');
}
}
Category.php picture
Category.php picture
Suppose you have a Category model, add blew code to your Category model class.
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id', 'id');
}
Then, replace {{$row['parent_id']}} with {{$row->parent->description}} in your code.
This is the answer
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tmp =Category::all()->toArray();
$category = array();
foreach ($tmp as $key => $row) {
$policy = Category::find($row['parent_id']);
$tmpResult = new Category();
$tmpResult-> id =$row['id'];
$tmpResult-> code =$row['code'];
$tmpResult-> description =$row['description'];
$tmpResult-> parent_id =$policy['description'];
$tmpResult-> status =$row['status'];
array_push($category, $tmpResult);
}
return view('category.index',compact('category'));
}
I want to update selected drop down value and save that into database
in this code i cant update my drop down list value from database
my controller code
public function edit($product_id)
{
$all_categories = DB::table('tbl_category')->get();
$all_manufactures = DB::table('tbl_manufacture')->get();
$product = DB::table('tbl_product')
->where('product_id',$product_id)
->get()->first();
return View('admin.edit-product')->with([
'product'=>$product,
'all_categories'=>$all_categories,
'all_manufactures'=>$all_manufactures,
]);
}
my view code
<select id="brand_name" name="brand_name" data-rel="chosen">
#foreach($all_manufactures as $manufacture)
#if($manufacture->manufacture_status)
<option value="{{$manufacture->manufacture_id}}"> {{$manufacture->manufacture_name}}</option>
#endif
#endforeach
</select>
The edit method displays a form for editing.
/**
* Show the form for editing the specified resource.
*
* #param \App\User $user
* #return \Illuminate\Http\Response
*/
public function edit(User $user)
{
$roles = Role::all();
return view('admin.users.form', [
'roles' => $roles,
'user' => $user
]);
}
You need update method
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\User $user
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$user = User::find($id);
$user->update($request->all());
$role = Role::findOrFail($request->roles);
$user->syncRoles([$role->id]);
return redirect()->route('users.index');
}
View
<form action="{{route('users.update', $user->id)}}" method="post">
#csrf
#method('PUT')
<select class="custom-select" name="roles">
#if ($roles)
#foreach($roles as $role)
<option {{ ((old('roles') == $role->id) or (isset($user) and $user->roles->pluck('id')->contains($role->id)) ) ? 'selected' : null }} value="{{$role->id}}">{{$role->display_name}}</option>
#endforeach
#endif
</select>
</form>
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".