Laravel Category + Slug of Article - php

I'm attempting to build a blog like application with the latest version of laravel. I'm trying to figure out how to add the category slug from the categories table and then add the article slug. Examples and pictures below.
localhost/test-category/howtomake
app/http/controllers/ArticlesController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::all();
return view('articles.index')->with('articles', $articles);
}
/**
* 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)
{
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($slug)
{
$articles = Article::where('slug', $slug)->first();
return view('articles.show')->with('articles', $articles);
}
/**
* 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)
{
//
}
}
app/Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'articles';
public $primaryKey = 'id';
public $timestamps = true;
}
routes/web.php
<?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');
});
Route::resource('articles', 'ArticlesController');
database table (articles)
database table (catorgeries)
Any help and suggestions will be appreciated thanks.

You can set the default key for a route in the model
public function getRouteKeyName()
{
return 'slug';
}

Related

Class 'App\Category' not found CategoriesController.php

I do not know how to solve this. I get an Error. I am trying to dynamically save the category in the database. But when I press create category I get an error. I am following a course that has a previous laravel version so I am not sure where the problem is.
Class 'App\Category' not found
CategoriesController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Models\Category as ModelsCategory;
class CategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('categories.index')->with('categories', Category::all());
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('categories.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:categories'
]);
$ncategory = new Category();
Category::create([
'name' => $request->name
]);
return redirect(route('categories.index'));
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = ['name'];
}
you have two different import for Category
use App\Category;
use App\Models\Category as ModelsCategory;
remove The top one. and change the second one as follow:
use App\Models\Category;
use App\Models\Category;
It should work

Class 'App\users' not found in Laravel when i create users.blade.php

I am getting the following error:
FatalErrorException in usercontroller.php line 21: Class 'APP\User' not found
usercontroller.php:
<?php
namespace App\Http\Controllers;
use APP\User;
use Illuminate\Http\Request;
use App\Http\Requests;
class usercontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::all();
return view('admin/users', compct('user'));
}
/**
* 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)
{
//
}
/**
* 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)
{
//
}
}
User.php (it's in App/User.php):
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
I create to users.blade.php to listing user in Admin Panel but at last then i m click to a user then they have an error on screen please say what can i do
Typo
Change use APP\User; to use App\User;
It's case sensitive so they are not the same thing.
please use User class in UserController with this namespace App/User NOT APP/user
Change use APP\User; to use App\User;
Change also compct to compact
public function index()
{
$user = User::all();
return view('admin/users', compact('user'));
}

Laravel NotFoundHttpException No query results for model

I have error NotFoundHttpException No query results for model [App\ThreadForum]
My web.php :
Route::get('/threads','ThreadForumController#index');
Route::post('/threads','ThreadForumController#store');
Route::get('/threads/create','ThreadForumController#create');
Route::get('/threads/{thread}','ThreadForumController#show')->name('threads.show');
My model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class ThreadForum extends Model
{
protected $fillable = [
'user_id','title','body'
];
public function path(){
return route('threads.show',$this->id);
}
public function replies(){
return $this->hasMany('App\Reply');
}
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
public function addReply($reply){
$this->replies()->create($reply);
}
}
My controller:
namespace App\Http\Controllers;
use App\ThreadForum;
use Illuminate\Http\Request;
class ThreadForumController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only('store');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = ThreadForum::all();
return view('threads.index', compact('threads'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('threads.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd('store');
/*$thread = ThreadForum::create([
'user_id'=>auth()->id(),
'title'=>$request['title'],
'body'=>$request['body']
]);
redirect($thread->path());*/
}
/**
* Display the specified resource.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function show(ThreadForum $thread)
{
return view('threads.show',compact('thread'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function edit(ThreadForum $threadForum)
{
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function update(Request $request, ThreadForum $threadForum)
{
}
/**
* Remove the specified resource from storage.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function destroy(ThreadForum $threadForum)
{
//
}
}
All functions work well, but when I run post('/threads') for store new record I get error.
I tried use dd('store') for debugging, but I dont see this text, only error.
How can I fix it?
Thanks.
I cannot comment, but to dump you have to use dd($request) not dd($store)
EDIT: I think that problem is in show method. You use compact, but there's no any variable above it. So you cannot pass anything to view.

Laravel 5.2 Route not working ERR_EMPTY_RESPONSE

When calling a resource route, the server dies. Resulting in GET http://localhost:8000/tag net::ERR_EMPTY_RESPONSE.
Serving via php artisan serve on localhost:8000 on a *nix box. Same results for ff and Chrome.
Ajax used is $.get
Routes:
Route::group(['middleware' => 'auth'], function(){
Route::resource('business', 'BusinessesController');
Route::resource('business.show', 'BusinessesController');
Route::resource('business.create', 'BusinessesController');
Route::resource('business.update', 'BusinessesController');
Route::get('order', ['as' => 'order.default', 'uses' => 'BusinessesController#create']);
Route::post('order', ['as' => 'order.post', 'uses' => 'BusinessesController#signUp']);
Route::resource('tag', 'TagController');
});
Route::auth();
All routes work, with the exception of 'tag.' Moreover, running php artisan route:list results in a segmentation fault.
The following works:
Route::get('tag', function(){
$tags = App\Tag::get()->toArray();
return $tags;
});
The controller:
<?php
namespace App\Http\Controllers;
use App\Http\Middleware\TagService;
use Illuminate\Http\Request;
use App\Http\Requests;
class TagController extends Controller
{
private $_ts;
public function __construct(TagService $tagService)
{
$this->_ts = $tagService;
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
dd('test');
return $this->_ts->get();
}
/**
* 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)
{
dd($request->all());
}
/**
* 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)
{
//
}
}
The request doesn't seem to make it to the controller.

Can't Assign Role to User

I tried to use spatie(laravel-permission) on my laravel apps but I can't assign role to the user. The error I facing is
Call to undefined method Illuminate\Database\Query\Builder::assignRole()
AdminsContoller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Traits\HasRoles;
class AdminsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
return view('admin.dashboard', compact('users'));
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$user = User::whereId($id)->firstOrFail();
// $user->hasAllRoles(Role::all());
return view('admin.show', compact('user'));
}
public function assignRole($id){
$user = User::whereId($id)->firstOrFail()->assignRole('Staff');
redirect('/dashboard');
}
/**
* 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)
{
$user = User::whereId($id)->firstOrFail();
$user->destroy();
redirect('/dashboard');
}
}
Add trait use HasRoles; in your user model.
Import it with use Spatie\Permission\Traits\HasRoles; at the top of the model file.
Add Spatie\Permission\Traits\HasRoles trait to your User model:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// …
}
Here is a complete example of Spatie Permissions

Categories