Recently I started programming using Laravel as a framework. Everything goes fine, but I tried to write a create script with the following message at runtime:
Sorry, the page you are looking for could not be found.
That's after posting Accept in the Create form, I already have created Index, Show, Update and delete functions successfully. So the route to my controller is correct, the file exist ... I'm totally stuck and can not see what incorrect.
Please help.
I didn't redirect the public folder so I'm still using the full path with no problem at the other modules (/gymmgr/public/lockers).
I send you the code:
index.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Lockers</a>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-sm-1 d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Acciones</span>
<a class="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle"></span>
</a>
</h6>
<ul class="nav flex-column">
<li class="nav-item">
<span data-feather="home"></span>
<span class="sr-only"></span>
</li>
<li class="nav-item">
<a class="nav-link" href="/gymmgr/public/lockers/create">
<span data-feather="file"></span>
Nuevo
</a>
</ul>
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
<h1 class="h5">Catálogo</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button class="btn btn-sm btn-outline-secondary">Exportar</button>
</div>
</div>
</div>
<table class="table">
<thead class="thead-light">
<tr>
<th>Clave del locker</th>
<th>Ubicación</th>
</tr>
</thead>
<tbody>
#foreach($lockers as $locker)
<tr>
<td> {{ $locker->strClaveLocker }} ></td>
<td>{{ $locker->strUbicacion }}</td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
#endguest
#endsection
LockersController.php
<?php
namespace App\Http\Controllers;
use App\Locker;
use Illuminate\Http\Request;
class LockersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$lockers = Locker::all();
return view('lockers.index', ['lockers'=>$lockers]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('lockers.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
/*
if (Auth::check())
{*/
$locker = Locker::create(
['strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')
]
);
if($locker)
{
return redirect()->route('lockers.index')->with('success','Locker creado con éxito');
}
// }*/
}
/**
* Display the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function show(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
//$locker = Locker::where('idLocker', $locker->idLocker)->first();
//echo e($locker->idLocker);
return view('lockers.show', ['locker'=>$locker]);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function edit(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
if ($locker)
{
return view('lockers.edit', ['locker'=>$locker])->with('success', 'Locker encontrado');
};
return view('lockers.edit', ['locker'=>$locker]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Locker $locker)
{
//
$lockerUpdate = Locker::where('idLocker', $locker->idLocker)->update([
'strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')]);
if ($lockerUpdate)
{
return redirect()->route('lockers.show',['lockers'=>$locker->idLocker])
->with('success', 'Locker actualizado correctamente');
}
return back()->withInput();
}
/**
* Remove the specified resource from storage.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function destroy(Locker $locker)
{
//
$findLocker = Locker::find($locker->idLocker);
if($findLocker->delete())
{
return redirect()->route('lockers.index')
->with('success','Locker borrado exitosamente');
}
return back()->withInput()->with('error','El locker no pudo borrarse');
}
}
create.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Locker') }}</div>
<div class="card-body">
</form>
<form method="post" action="{{ route('lockers.create') }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label for="locker-clave">Clave del locker<span class="required">*</span></label>
<input placeholder="Clave del locker"
id="locker-clave"
required
name="strClaveLocker"
spellcheck="false"
class="form-control"
/>
</div>
<div class="form-group">
<label for="locker-ubicacion">Ubicación del locker</label>
<input placeholder="Ubicación del locker"
id="locker-ubicacion"
required
name="strUbicacion"
class="form-control"
/>
</div>
<div class="form-group">
<input type="submit"
class="btn btn-primary"
value="Aceptar"
/>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endguest
#endsection
The route
web.php
/*
|--------------------------------------------------------------------------
| 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('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('lockers','LockersController');
Your create form has:
<form method="post" action="{{ route('lockers.create') }}">
...
<input type="hidden" name="_method" value="put">
So you are sending a PUT request to your create route. As the docs describe, the .create route the resource declaration sets up is expecting a GET (check the table of actions it sets up). So that request will fail with a 404 as there is no matching route set up.
Also, though confusingly named, the create route is for displaying the create form, not saving data you've entered into that form. So you should not be submitting data to your create route.
You need to:
Change your form action to point to your store route: ... action="{{ route('lockers.store') }}
Remove the form method spoofing, store expects POST which is what you'll get by default without any spoofing.
Related
is it possible to pass collections to a component like this:
class projectAccordion extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
$projects = Project::with('subservice', 'partner')->orderByDesc('id')->limit(4)->get();
return view('components.front.project-accordion', compact('projects'));
}
}
the component itself looks like this:
#foreach($projects as $project)
<div class="info-list-wrapper">
<div class="row">
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-title">
<div class="date">
<span>{{ date('d F, Y', strtotime($project->created_at)) }}</span>
</div>
<h4>
{{ $project->title }}
</h4>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-txt">
<p>
{{ $truncated = Str::of($project->descr)->limit(100) }}
</p>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-img">
<img src="{{ $project->partner->file_url }}" alt="">
</div>
</div>
</div>
</div>
#endforeach
this is how i call the component in homeage.blade.php
<x-front.project-accordion></x-front.project-accordion>
This method does not work on hosting, but everything works on the local version.Is this the right approach?
I try to create blog web page in laravel. While creating the post, the contents of the tag and category button are not visible.
My post.blade.php;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/select2/select2.min.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Forms</li>
<li class="active">Editors</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Titles</h3>
</div>
#include('includes.messages')
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{ route('post.store') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Title">
</div>
<div class="form-group">
<label for="subtitle">Post Sub Title</label>
<input type="text" class="form-control" id="subtitle" name="subtitle" placeholder="Sub Title">
</div>
<div class="form-group">
<label for="slug">Post Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Slug">
</div>
</div>
<div class="col-lg-6">
<br>
<div class="form-group">
<div class="pull-right">
<label for="image">File input</label>
<input type="file" name="image" id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="status" value="1"> Publish
</label>
</div>
</div>
<br>
<div class="form-group" style="margin-top:18px;">
<label>Select Tags</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="tags[]">
#foreach ($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
#endforeach
</select>
</div>
<div class="form-group" style="margin-top:18px;">
<label>Select Category</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="categories[]">
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box">
<div class="box-header">
<h3 class="box-title">Write Post Body Here
<small>Simple and fast</small>
</h3>
<!-- tools box -->
<div class="pull-right box-tools">
<button type="button" class="btn btn-default btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
</div>
<!-- /. tools -->
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea name="body" style="width: 100%; height: 500px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" id="editor1"></textarea>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-primary">
Back
</div>
</form>
</div>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/select2/select2.full.min.js') }}"></script>
<script src="{{ asset('admin/ckeditor/ckeditor.js') }}"></script>
<script>
$(function () {
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('editor1');
});
</script>
<script>
$(document).ready(function() {
$(".select2").select2();
});
</script>
#endsection
I am stuck because everytime I submit a post with tags and category.
My edit blade.php file;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/select2/select2.min.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Forms</li>
<li class="active">Editors</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Titles</h3>
</div>
#include('includes.messages')
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{ route('post.update',$post->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="subtitle">Post Sub Title</label>
<input type="text" class="form-control" id="subtitle" name="subtitle" placeholder="Sub Title" value="{{ $post->subtitle }}">
</div>
<div class="form-group">
<label for="slug">Post Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Slug" value="{{ $post->slug }}">
</div>
</div>
<div class="col-lg-6">
<br>
<div class="form-group">
<div class="pull-right">
<label for="image">File input</label>
<input type="file" name="image" id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="status" value="1" #if ($post->status == 1)
{{'checked'}}
#endif> Publish
</label>
</div>
</div>
<br>
<div class="form-group" style="margin-top:18px;">
<label>Select Tags</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="tags[]">
#foreach ($tags as $tag)
<option value="{{ $tag->id }}"
#foreach ($post->tags as $postTag)
#if ($postTag->id == $tag->id)
selected
#endif
#endforeach
>{{ $tag->name }}</option>
#endforeach
</select>
</div>
<div class="form-group" style="margin-top:18px;">
<label>Select Category</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="categories[]">
#foreach ($categories as $category)
<option value="{{ $category->id }}"
#foreach ($post->categories as $postCategory)
#if ($postCategory->id == $category->id)
selected
#endif
#endforeach
>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box">
<div class="box-header">
<h3 class="box-title">Write Post Body Here
<small>Simple and fast</small>
</h3>
<!-- tools box -->
<div class="pull-right box-tools">
<button type="button" class="btn btn-default btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
</div>
<!-- /. tools -->
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea name="body" style="width: 100%; height: 500px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" id="editor1">{{ $post->body }}</textarea>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-primary">
Back
</div>
</form>
</div>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/select2/select2.full.min.js') }}"></script>
<script src="{{ asset('admin/ckeditor/ckeditor.js') }}"></script>
<script>
$(function () {
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('editor1');
//bootstrap WYSIHTML5 - text editor
$(".textarea").wysihtml5();
});
</script>
<script>
$(document).ready(function() {
$(".select2").select2();
});
</script>
#endsection
My show blade php file;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/datatables/dataTables.bootstrap.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Blank page
<small>it all starts here</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Examples</li>
<li class="active">Blank page</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Title</h3>
<a class='col-lg-offset-5 btn btn-success' href="{{ route('post.create') }}">Add New</a>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="box">
<div class="box-header">
<h3 class="box-title">Data Table With Full Features</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>Title</th>
<th>Sub Title</th>
<th>Slug</th>
<th>Creatd At</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($posts as $post)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->subtitle }}</td>
<td>{{ $post->slug }}</td>
<td>{{ $post->created_at }}</td>
<td><span class="glyphicon glyphicon-edit"></span></td>
<td>
<form id="delete-form-{{ $post->id }}" method="post" action="{{ route('post.destroy',$post->id) }}" style="display: none">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
<a href="" onclick="
if(confirm('Are you sure, You Want to delete this?'))
{
event.preventDefault();
document.getElementById('delete-form-{{ $post->id }}').submit();
}
else{
event.preventDefault();
}" ><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Title</th>
<th>Sub Title</th>
<th>Slug</th>
<th>Creatd At</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
Footer
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('admin/plugins/datatables/dataTables.bootstrap.min.js') }}"></script>
<script>
$(function () {
$("#example1").DataTable();
});
</script>
#endsection
My PostController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\category;
use App\Model\user\post;
use App\Model\user\tag;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = post::all();
return view('admin.post.show',compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$tags =tag::all();
$categories =category::all();
return view('admin.post.post',compact('tags','categories'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
return $request->all();
$this->validate($request,[
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
]);
$post = new post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = $request->status;
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect(route('post.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)
{
$post= post::where('id',$id)->first();
$tags =tag::all();
$categories =category::all();
return view('admin.post.edit',compact('tags','categories','post'));
}
/**
* 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,[
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
]);
$post = post::find($id);
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
post::where('id',$id)->delete();
return redirect()->back();
}
}
My tagcontroller.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\tag;
use Illuminate\Http\Request;
class TagController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tags = tag::all();
return view('admin.tag.show',compact('tags'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.tag.tag');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'slug' => 'required',
]);
$tag = new tag;
$tag->name = $request->name;
$tag->slug = $request->slug;
$tag->save();
return redirect(route('tag.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)
{
$tag= tag::where('id',$id)->first();
return view('admin.tag.edit',compact('tag'));
}
/**
* 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,[
'name' => 'required',
'slug' => 'required',
]);
$tag = tag::find($id);
$tag->name = $request->name;
$tag->slug = $request->slug;
$tag->save();
return redirect(route('tag.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
tag::where('id',$id)->delete();
return redirect()->back();
}
}
My categorycontroller.php;
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$categories = category::all();
return view('admin.category.show',compact('categories'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.category.category');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'slug' => 'required',
]);
$category = new category;
$category->name = $request->name;
$category->slug = $request->slug;
$category->save();
return redirect(route('category.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)
{
$category= category::where('id',$id)->first();
return view('admin.category.edit',compact('category'));
}
/**
* 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,[
'name' => 'required',
'slug' => 'required',
]);
$category = category::find($id);
$category->name = $request->name;
$category->slug = $request->slug;
$category->save();
return redirect(route('category.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
category::where('id',$id)->delete();
return redirect()->back();
}
}
How can I resolve this issue? And furthermore how can i do it?
Help me, i am trying put some pagination in my page, but keep showing this error Call to undefined method App\Product::link()
my Code
Blade:
#extends('layouts.index')
#section('content')
<!-- TOP Navbar -->
<div class="w-100 shadow d-flex position-relative position-fixed" style="z-index: 2; background-color: white; ">
<div class="d-flex justify-content-center py-3 mr-auto" style="width: 200px">
<img src="/images/logo.svg" alt="logo icon">
</div>
<div class="pt-4 bd-highlight"><a href="/">
<a href="/create">
<button type="button" class="btn font p-auto rounded-pill text-white font-weight-bold "
style="background-color: #28AD47; width: 178px; height: 40px">New Product
</button>
</a>
</a>
</div>
<div class=" bd-highlight">
<div class="dropdown px-5" style="padding-top: 24px">
<a class=" align-middle font dropdown-toggle" href="#" role="button" id="dropdownMenuLink"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
style="color: #2c3539"> {{ Auth::user()->name }}
</a>
<div class="dropdown-menu mt-4" aria-labelledby="dropdownMenuLink" x-placement="bottom-start"
style="position: absolute; transform: translate3d(-20px, 38px, 0px); top: 0px; left: 0px; will-change: transform;">
<a class="dropdown-item" href="{{ url('/profile') }}"><i class="fa fa-btn fa-user"></i>Profile</a>
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</div>
</div>
</div>
</div>
<!-------------------------->
<div style="background-color: #F8F8F8; z-index: 0;" class="d-flex justify-content-start ">
<div style="width: 200px; height: calc(100vh - 70px);margin-top: 73px;z-index: 1"
class="bg-white shadow position-fixed ">
<div class="d-flex justify-content-center">
<a href="/create">
<p class="font my-5" style="color: #525252"><img src="/images/plus.svg" class="mr-3" alt="plus icon"
width="16px" height="16px">
New Product
</p>
</a>
</div>
<div class="d-flex justify-content-center">
<p class="font my-5" style="color: #525252"><img src="/images/edit.svg" class="mr-3" alt="edit icon"
width="16px" height="16px">
Edit Product
</p>
</div>
</div>
<div class="w-100 " style="margin-top: 73px;margin-left: 200px">
<div class="d-flex justify-content-center">
<h1 class="font p-5">All products</h1>
</div>
<!-- <div style="height: calc(100vh - 202px);background-color: #676767">-->
<div class=" d-flex justify-content-center flex-wrap">
#foreach($products as $product)
<div class="product shadow m-4">
<div class="d-flex justify-content-center mt-3">
<img src="/images/car-example.png" alt="car photo" width="190px" height="140px">
</div>
<div>
<p class="font text-white pt-4 pl-5" style="font-size: 16px">
{{$product->name}} <img src="/images/info.svg" alt="info icon" width="20px"
height="15px" class="pl-2 mb-1 red-tooltip"
data-toggle="tooltip"
data-placement="right" title="{{$product->description}} ">
</p>
</div>
<div class="d-flex bd-highlight" style="margin-top: 40px">
<div class="pl-5 mt-auto bd-highlight">
<h3 class="font text-white">${{$product->price}}</h3>
</div>
<div class="bd-highlight ml-auto mt-3 pr-5">
<a href="/">
<button type="button"
class="btn font rounded-pill text-white font-weight-light "
style="font-size: 18px;background-color: #28AD47; width: 99px; height: 41px">
Buy
</button>
</a>
</div>
</div>
</div>
#endforeach
</div>
<div class="w-100" style="background-color: white; z-index: 0;">
<div class=" d-flex justify-content-center pt-2">
{{ $product->link() }}
</div>
</div>
</div>
</div>
<!-- Bootstrap Link -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
<!-- Hover -->
#endsection
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::orderBy('name')->paginate(10);
return view('show')->with('products', $products);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' =>'required | unique:product|max:64',
'description' => 'required',
'price' => 'required',
]);
print_r($request->input());
$product = new Product;
$product->name = $request->name;
$product->description = $request->description;
$product->price = $request->price;
echo $product->save();
return redirect('/home');
}
/**
* 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)
{
//
}
}
web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('home', 'ProductController');
Route::get('profile', 'UserController#profile')->middleware('auth');
Route::post('profile', 'UserController#update_avatar')->name('profile');
Route::view('create', 'create')->middleware('auth');
Route::post('submit', 'ProductController#store');
You need use products and links in plural
<div class="w-100" style="background-color: white; z-index: 0;">
<div class=" d-flex justify-content-center pt-2">
{{ $products->links() }}
</div>
</div>
I am trying to pass a foreign directly to my create method
In the project I am working on, I have two different users. The first one is a farm who can create an animal
The second one is the clinic who can attach to an animal some clinic details showing if the animal was vaccinated or not. In my clinic details table, I have the animal as a foreign key but I do not know how I will pass that key to the create method
Here is my Clinic controller
<?php
namespace App\Http\Controllers;
use App\Animal;
use App\Clinic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ClinicController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = Auth::user();
$animal = Animal::all();
return view('clinic.index', compact('user', 'animal'));
}
/**
* Show the form for creating a new resource.
*
* #param $id
* #return void
*/
public function create($id)
{
$user = Auth::user();
$animal = Animal::query()->findOrFail($id);
$clinic = new Clinic();
return view('clinic/create', compact('user', 'animal', 'clinic'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$animal = Animal::query()->findOrFail($id);
return view('clinic.show', compact('animal'));
}
/**
* 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 clinic index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h3>Clinic Details Dashboard</h3></div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in! {{ $user->name}}
<hr>
<center><h3>Animals</h3></center>
<hr>
#foreach($animal as $animal)
<div class="row">
<div class="col-2">{{ $animal->id }}</div>
<div class="col-4">{{ $animal->type->category }}</div>
<div class="col-2">{{ $animal->user->name }}</div>
<div class="col-4">{{ $animal->created_at }}</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
my clinic show.blade.php
This is where I would like to pass the animal id to the create method but I do not know how.
Even my button link is not going to the create view
#extends('layouts.app')
#section('title', 'Details for animal ')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<center><h3>Details for the animal</h3></center>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $animal->id }}</p>
<p><strong>Animal: </strong>{{ $animal->type->category }}</p>
<p><strong>Farm: </strong>{{ $animal->user->name }}</p>
<p><strong>Gender: </strong>{{ $animal->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $animal->placeOfBirth }}</p>
<p><strong>Date: </strong>{{ $animal->created_at }}</p>
</div>
<div class="col-md-8 offset-md-4">
<a href="create">
<button type="button" class="btn btn-primary">
Attach Clinic Detail
</button>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
clinic create.blade.php
This view is still somehow empty
#extends('layouts.app')
#section('title', 'Add Clinical Details')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New Animal</div>
<div class="card-body">
<form action="{{ url('clinic') }}" method="POST">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
You could simply add a parameter to your method like this:
public function create($id, $foreignKeyId) {
// ... to something
}
For that you would need to pass id as a hidden input.
You could also try to create a relation, that way you do not need to add the foreignKey as a parameter. You could simply access the foreign_key using the specified ORM Model.
You can use insertGetId() method for retrieving last created id after store method.
https://laravel.com/api/5.0/Illuminate/Database/Query/Builder.html#method_insertGetId
$id = DB::table('posts')->insertGetId(
array('title' => 'my post title', 'body' => 'my post body')
);
Trying to get property 'id' of non-object
how to fix the bug, please explain to me, someone
edit.blade.php
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header text-center">Edit Post : {{$posts->title}}</div>
<div class="card-body">
#if(count($errors)>0)
<ul class="list-group alert">
#foreach($errors->all() as $error)
<li class="list-group-item text-danger">
{{$error}}
</li>
#endforeach
</ul>
#endif
<form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
</div>
<div class="form-group">
<label for="image">Featured Image</label>
<input type="file" name="image" class="form-control">
</div>
<div class="form-group">
<label for="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
#foreach($categories as $cat)
<option value="{{$cat->id}}"
#if($posts->cat->id== $cat->id)
selected
#endif
>{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="tag">Select Tags</label>
#foreach($tag as $tags)
<div class="checkbox">
<label><input type="checkbox" name="tags[]" value="{{$tags->id}}"
#foreach($posts->tags as $t)
#if($tags->id==$t->id)
checked
#endif
#endforeach
>{{$tags->tag}}</label>
</div>
#endforeach
</div>
<div class="form-group">
<label for="content">Description</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
PostController.php
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header text-center">Edit Post : {{$posts->title}}</div>
<div class="card-body">
#if(count($errors)>0)
<ul class="list-group alert">
#foreach($errors->all() as $error)
<li class="list-group-item text-danger">
{{$error}}
</li>
#endforeach
</ul>
#endif
<form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
</div>
<div class="form-group">
<label for="image">Featured Image</label>
<input type="file" name="image" class="form-control">
</div>
<div class="form-group">
<label for="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
#foreach($categories as $cat)
<option value="{{$cat->id}}"
#if($posts->cat->id== $cat->id)
selected
#endif
>{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="tag">Select Tags</label>
#foreach($tag as $tags)
<div class="checkbox">
<label><input type="checkbox" name="tags[]" value="{{$tags->id}}"
#foreach($posts->tags as $t)
#if($tags->id==$t->id)
checked
#endif
#endforeach
>{{$tags->tag}}</label>
</div>
#endforeach
</div>
<div class="form-group">
<label for="content">Description</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
Trying to get property 'id' of non-object when I select category its not selected and show id is non-object
PostController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Post;
use App\Tag;
use Session;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.posts.index')->with('posts',Post::all());
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$categories=Category::all();
if($categories->count()==0){
Session::flash('info','You must have some categories before attempt post.');
return redirect()->back();
}
return view('admin.posts.create')->with('category',$categories)->with('tags',Tag::all());
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required|max:255',
'image'=>'required|image',
'content'=>'required',
'category_id'=>'required',
'tags'=>'required'
]);
$images=$request->image;
$image_new_name=time().$images->getClientOriginalName();
$images->move('uploads/posts',$image_new_name);
$post=Post::create([
'title'=>$request->title,
'image'=>$request->image,
'content'=>$request->content,
'image'=>'uploads/posts/'.$image_new_name,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
]);
$post->tags()->attach($request->tags);
Session::flash('success','Post Created Successfully');
return redirect()->back();
}
/**
* 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)
{
$post=Post::find($id);
return view('admin.posts.edit')->with('posts',$post)
->with('categories',Category::all())
->with('tag',Tag::all());
}
/**
* 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,[
'title'=>'required',
'content'=>'required',
'category_id'=>'required',
]);
$post=Post::find($id);
if ($request->hasFile('image'))
{
$featured=$request->image;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts',$featured_new_name);
$post->image='uploads/posts/'.$featured_new_name;
}
$post->title=$request->title;
$post->content=$request->content;
$post->category_id=$request->category_id;
$post->save();
$post->tags()->sync($request->tags);
Session::flash('success','Your Post Updated Successfully');
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post=Post::find($id);
$post->delete();
Session::flash('success','Post was just trashed');
return redirect()->back();
}
public function trashed(){
$post=Post::onlyTrashed()->get();
return view('admin.posts.trashed')->with('posts',$post);
}
public function kill($id){
$post=Post::withTrashed()->where('id',$id)->first();
$post->forceDelete();
Session::flash('success','Post deleted permanently');
return redirect()->back();
}
public function restore($id){
$post=Post::withTrashed()->where('id',$id)->first();
$post->restore();
Session::flash('success','Post Restore ');
return redirect()->route('posts');
}
}
Make sure that categories, tags are not empty, also the Postrelationships are true.