I am trying to fetch data from database in Laravel using Model binding in Controller but it returns empty $task array instead
here is my routes.php :
<?php
Route::get('/tasks','TasksController#index');
Route::get('/tasks/{task}','TasksController#show');
and this the TasksController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
class TasksController extends Controller
{
public function show(Task $task)
{
return $task;
}
}
Route::get('/tasks/{id}','TasksController#show');
in route model binding you can pass the id in the route and handle it in the show function
public function show($id)
{
$task = Task::find($id);
return $task;
}
This will directly return the task
Without seeing other code it's hard to answer however your function index() should look similar to this:
public function index(Task $task)
{
$tasks = $task->all();
return view('page', compact('tasks'));
}
Now, assuming you're gathering the ID's via a foreach loop your controller code should look like this:
public function show($id, Task $task)
{
$task = $task->find($id);
return view('somePage', compact('task'));
}
This will then pass $task back to a page of your choosing to display the specific task you are getting.
Your Routing is fine providing you're passing the:
Something
back within the blade.
Related
I am trying to execute simple CRUD operation using laravel. but it gives an error code 500 , when I try to fetch data from table either by laravel framework as well as with plain PHP.
Here is my controller class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = DB::select('select * from book');
$data = BookModel::all();
echo($data);
return compact('data');
}
}
axiom , which is been used. ---> "https://unpkg.com/axios/dist/axios.min.js"
Model Class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BookModel extends Model {
protected $table = "book";
public $timestamps = false;
}
It is returning no result from the table.
It worked after I changed my controller to
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return $data;
}
}
But it was giving null result when I was doing something like below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin')->withTasks($data);
}
}
Can someone explain why it was not working earlier?
Anyways , Thanks everyone for your help.
Cheeeeeeeeerrsssss!!!!!!!
You need to add response() to return data without view.
Like below the code returns the JSON data from your BookModel that sometimes we need through the ajax request like axios.
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return response()->toJson($data);
}
}
With view you can do the following:
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin', compact('data'));
}
}
Where you can access the BookModel data through $data variable in your admin.blade.php
I want to fetch user based on id but it's returning null but User::all() is working correctly.
index and show methods in UsersController :-
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use DB;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth',['except'=>['index','show']]);
}
public function index()
{
$users=User::all();
return view('dev.index')->with('users',$users);
}
public function show(User $user)
{
return view('dev.show')->with('user',$user);
}
}
Route:-
Route::resource('devs','UsersController');
On view I have {{dd($user->name)}} and it's returning null on url public/devs/{dev}.
but working fine on index on url public/devs
This is because you are defining your base route like this:
Route::resource('devs', 'UserController');
This means that laravel will format the show method as follows:
Route::get('devs/{dev}', 'UserController#show');
Laravel will try to solve this dependency doind Implicit Model Binding and given that {dev} doesn't match any of your defined model classes, it will indeed return null.
So to solve it, define this match explicitly doing Explicit Binding. To accomplish this, go to your:
App/Providers/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::model('dev', App\User::class);
}
So now wherever Laravel reads the {dev} route parameter, will match it with the User model.
You don't initialize users for your entire controller.
Each function uses their own variables
First of all, I would reconfigure your route like so:
Route::get('devs', 'UsersController#show')->name('showUsers')
In your function show I would do the following
public static function show(){
$id = 1;
$users = User::where('id', $id')
return view('dev.show', compact('users'));
}
I just starting to learn laravel and i don't know how to pass data on controller
this is my routes.php
Route::get('/', 'view#index');
Route::get('/create', 'view#create');
Route::get('/store', 'view#store');
Route::post('/destroy', 'view#destroy');
and this is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Request;
class view extends Controller {
public function index()
{
echo $data = User::all();
}
public function create()
{
$user = User::create(['first_Name'=>'abc']);
echo "id =".$user->id." is created";
}
public function store()
{
echo "store";
}
/* here i want to get id using post request */
public function destroy(Request $request)
{
$id = $request->input('id');
echo $id;
}
}
now I want to pass 'id' using postman in post request. how can i perform this. my get request working perfectly
In Postman, you can fill the request body:
The id key corresponds to the 'id' argument inside $request->input('id');
Note: My example uses DELETE, which is common practice for destroy/delete actions. In your case, it should be POST. I do, however, advice you to use DELETE.
I want to call a function of CommonController in PostsController.
PostsController is in Posting namespace
PostController: App\Http\Controllers\Posting\PostsController
CommonController: App\Http\Controllers\CommonController
I tried this code but it did not work in PostingController, it is a small code from my PostingController
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\CommonController;
class PostsController extends Controller
{
public function myFunction(Request $request, $id){
$commonControllerObj = new CommonContoller;
$result = $commonControllerObj->commonCallingFunction($id);
}
}
but it did not work, its giving error
Class 'App\Http\Controllers\Posting\CommonContoller' not found
The first your namespace is wrong
namespace App\Http\Controllers\Posting;
The second you can call another Controller like this
app('App\Http\Controllers\CommonController')->commonCallingFunction();
This will work, but this is bad in terms of code organisation
You can extends Controller like this
use App\Http\Controllers\CommonContoller;
class PostsController extends CommonContoller
{
public function myFunction(Request $request, $id){
$result = $this->commonCallingFunction($id);
}
}
i use this code for return articles but not run this code and return blank,
how to solve this problem?
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
first time:
for next record:
Try this:
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
Update
You're getting empty result seconds time, because first time you're sending info through request, so $request has some data. If you reload the page, you do not send any info, that's why it's empty. It's just how it works.
Usually, when you're using $request data, you want to store it in DB or somehting and then redirect somewhere, for example, return $redirect->back(); instead of your return clause.
Try this
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
add this line in your class
use Illuminate\Http\Request;
You need to inject the Request into your method:
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->input();
return $name;
}