Laravel route with variable - laravel

I have controller with show function:
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::findOrFail($id);
return View::make('posts/csspost1', compact('csstablepost'));
}
}
And my route:
Route::get('/css3/{id}', 'CssController#show' );
Everything is working fine and corect id is in route, but I want get $title in my route. I have title and id column in database. Why my route isn't working with title if i change all id to title, but works with id? Is there a way, to get my title in route name?

I'm assuming you are changing your query to CssTable::findOrFail($title); and findOrFail() looks for the PK on your table. You'll want to change this too something like CssTable::where('title', '=', $title)->firstOrFail();.
Full Example:
class CssController extends BaseController
{
public function show($title)
{
$csstablepost = CssTable::where('title', '=', $title)->firstOrFail();
return View::make('posts/csspost1', compact('csstablepost'));
}
}
Route::get('/css3/{title}', 'CssController#show');
See more documentation.

Try with following code-
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::where('id', '=', $id)->first();
return View::make('posts/csspost1')->with('csstablepost', $csstablepost);
}
}
Now go to route page-
Route::get('css3/{title}', 'CssController#show');
Hope, it will work.

Related

How to call model in Controller request $request

I have a query in my model, which I want to call in my controller (request $request). It's working fine when the controller parameter is controller($id). But how to pass it in $request controller.
teacher Model with Query:
class teacher extends Model
{
public static function teacher($id)
{
return DB::table('teachers')
->leftjoin('religions', 'teachers.religion_id', '=', 'religions.id')
->leftjoin('areas', 'teachers.area_id', '=', 'areas.id')
->select('teachers.*','religions.*','areas.*')
->where('teachers.id',$id)
->first();
}
Controller which calls this model perfectly fine passing direct id:
public function report1($id)
{
$teacher = Teacher::teacher($id);
return View('teachers.report1' ,compact('teacher'));
}
Controller where I want to call it:
public function printreports(Request $request)
{
$teachers = $request->get('select2');
return view('teachers.report1',compact('teachers'));
}
Note: select2 contains teacher ids where I want to run model query.
Supposing you are have an array of ids in your select2 request param, probably easiest way is to change query at teacher model as follows:
use Illuminate\Support\Arr;
class teacher extends Model
{
public static function teacher($id)
{
return DB::table('teachers')
->leftjoin('religions', 'teachers.religion_id', '=', 'religions.id')
->leftjoin('areas', 'teachers.area_id', '=', 'areas.id')
->select('teachers.*','religions.*','areas.*')
->whereIn('teachers.id', Arr::wrap($id))
->get();
}
}

Setting getKeyRouteName dependant on route (web or api)

Tried looking for the answer to this everywhere but having no luck so far...
Basically I want my web route to use a slug for its URL, but I want to use ID for the API route. So...
http://myurl.com/chapter/my-chapter-slug
and
http://myurl.com/api/chapter/1234
Have tried various combinations of things in the getRouteKeyName method (if(Request::route()->named('myapiroute'), if(Request::isJson() etc...) but I think these might be being checked against the page it's running on, rather than the route I'm trying to generate?
I'm thinking maybe I need to extend the base model to have a separate one to use with my API maybe?
So I'd have...
class Chapter extends Model
{
public function getRouteKeyName()
{
return 'slug';
}
....
}
and then...
class ApiChapter extends Chapter
{
public function getRouteKeyName()
{
return 'id';
}
....
}
But not sure how I'd structure this in the most "Laravel" way? Or is there a better/tidier solution?
define your route for example like
Route::get('chapters/{chapter}','ChapterController#show'); // find by slug
Route::get('api/chapters/{chapter}','ApiChapterController#show'); // find by id
for web controller
class ChapterController extends Controller
{
public function show(Request $request,$slug)
{
$instance = Model::whereSlug($slug)->first();
}
}
for api
class ApiChapterController extends Controller
{
public function show(Request $request,$id)
{
$instance = Model::find($id);
}
}
You can define 2 different routes for that but unfortunatelly you will not be able to use model binding and you will have to look for the model like:
public function show(Request $request,$slug) {
$instance = Model::whereSlug($slug)->first();
}
as shown below: https://stackoverflow.com/a/48115385/6525417

Table Bring all comments for one article using yajra/laravel-datatables

when try to get all comment for one Article by Article::first() but first()
Bring just the first article
i try use find() like
$comments = Article::find()-> commentsArticle()->with('articles');
return Datatables::of($comments)
i get error so how i can Pass a value to view all comments for one article
or
my be there is way without using find()
Article model
class Article extends Model{
public $table = 'articles';
public function commentsArticle() {
return $this->hasMany('App\Comment');
}
}
controller
enter code here
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use App\Article;
use App\Comment;
class CommentController extends Controller{
public function commentsForOne Article()
{
$comments = Article::all()->commentsArticle->with('articles');
return Datatables::of($comments)->make(true);
}
}
last error i get
ErrorException (E_DEPRECATED)
Non-static method Yajra\Datatables\Datatables::collection() should
not be called statically
I hope find any idea or example like that will help me to learn
You are trying to get the first articles with its comments.
public function commentsForOneArticle($id)
{
$article = Article::fine($id);
//check if article exists to avoid errors
if ( $article ) {
return Datatables::of(Comment::where('article_id', $article->id))->make(true);
}
return "no article with id" . $id;
}
This was just an illustration. But it seems you need to understand first how Eloquent works. Watch this free Laracast https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
For routes, you can define the route like this:
Route::get('comments/{article_id}', 'ArticleController#commentsForOneArticle');
And call it in Ajax like
$.ajax({url: "/comments/1",
success: function(result){
//do stuff here
},
error: function(error) {
console.log(error)
}
});
All this is just a guide and not THE solution.
Edit
To take data with the user in one go
$article = Article::with('user')->find($id);
//will include all the fields from user and article
Comments & author
To get the name of the comment author, you need to define the relationship in comment model
public function user() {
return $this->belongsTo(User::class);
}
Then get like this
if ( $article ) {
return Datatables::of(Comment::with('users')->where('article_id', $article->id))->make(true);
}

laravel 4 routing passing values to controller

I have a route as below
Route.php
Route::get('u/list/{type}', 'UsersController#listAll');
UsersController.php
class UsersController extends BaseController
{
public function listAll($id)
{
$users = User::all();
return Response::json( $users );
}
}
this works well, i can pass the type into the listAll() method
Now i have another url which is like this
Route::get('u/doctor', 'UsersController#listAll');
Route::get('u/receptionist', 'UsersController#listAll');
This is equivalent to
Route::get('u/list/1', 'UsersController#listAll');
Route::get('u/list/2', 'UsersController#listAll');
is there a way for the URL "u/doctor" to pass the number 1 to listall() method
Or else i will have to rewrite the function with a new name.
Use a closure instead of a string.
Route::get('u/doctor', function(){
return (new UsersController)->listAll(1);
});

Controller Routing with Parameters

got a little n00b problem with Laravel 4. I have following routes:
Route::get('search', 'MovieController#search');
Route::get('edit/{$id}', 'MovieController#edit');
Route::get('/', 'MovieController#index');
and the following controller:
class MovieController extends BaseController {
protected $layout = 'layouts.master';
public function index()
{
$movies = Movie::paginate(30);
return View::make('index')->with('movies', $movies);
}
public function search()
{
if(isset($_REQUEST['sq'])) {
Cache::forever('sq', $_REQUEST['sq']);
}
$movies = Movie::where('title', 'LIKE', '%'.Cache::get('sq').'%')->paginate(30);
return View::make('index')->with('movies', $movies);
}
public function edit($id) {
return View::make('edit')->with('id', $id);
}
}
Now a call like this won't work:
<a href="edit/{{ $movie->movie_id }}">
I get a "NotFoundHttpException". The URL seems right: laravel/public/edit/2 e.g.
If i remove all the $id stuff from the code, so I route only to edit, it works.
Hopefully I could express myself enough, so somebody can help me. It's driving me nuts.
regards
In your routes.php, it's not Route::get('edit/{$id} ... but Route::get('edit/{id}

Resources