<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\customer;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('insert');
}
public function create()
{
}
public function store(Request $request)
{
$customer = new customer;
$customer->name = $request->name;
$customer->sex = $request->sex;
$customer->pob = $request->pob;
$customer->tel = $request->tel;
$customer->email = $request->email;
$customer->save();
}
public function show()
{
return view('show');
}
}
?>
I m getting error as Class 'App\Http\Controllers\customer' not found Laravel 5.2.
I have use App\customer;
use Inputs;
Why am I getting error ?
What is the problem in the code?
If your customer class is in App namespace, you need to use:
use App\customer;
instead of
use App\Http\Controllers\customer;
This is simple You need to understand. Make sure you have created a model Customer.php in app/Customer.php and another important thing you must create your table with customers that's all. After that you need to add this line before class like:-
use App\Customer;
Hope it helps :)
Related
I'm trying to save a document on the database in mongo usin a 'jenssegers/laravel-mongodb' model
this is the model
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model;
class Candidate extends Model
{
public function User(){
return $this->belongsTo(User::class);
}
}
and the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Candidate;
class CandidateController extends Controller
{
public function __construct(){
//$this->middleware('jwt');
}
public function create(Request $request){
//var_dump($request->all());
$candidate = new Candidate();
$candidate->name($request['name']);
$candidate->source($request['source']);
$candidate->save();
}
}
I get this error
BadMethodCallException: Call to undefined method App\Candidate::name() in file /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 50
The problem is clear but how I have to add a method to add the attributes on the model
Looking at docs it should be used same as in Eloquent so instead of
$candidate = new Candidate();
$candidate->name($request['name']);
$candidate->source($request['source']);
$candidate->save();
you can use:
$candidate = new Candidate();
$candidate->name = $request->input('name');
$candidate->source = $request->input('source');
$candidate->save();
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 have the following method in my controller:
public function store(Request $request){
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
When the controller runs i get the following error:
Now i do have the following modal class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notice extends Model {
protected $fillable = [
'provider_id',
'infringing_title',
'infringing_link',
'original_link',
'original_description',
'template',
'content_removed'
];
public static function open(array $attributes) {
return new static($attributes);
}
public function useTemplate($template) {
$this->template = $template;
}
}
Now why am i getting this error in laravel class not found , even though i have this class defined ??
Just use use keyword at the top of your controller class.
<?php
namespace App\Http\Controllers;
use App\Notice; // <------------------ use here
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function store(Request $request)
{
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
}
The use keyword helps php to recognize the class Notice within a certain namespace
Hope this helps!
Add this line to the top of a controller:
use App\Notice;
Or use full namespace:
$notice = \App\Notice::open($date);
I want to display the content saved on the database on my view page. I've applied relationship between user and account.
I have another relationship between user and post which is working perfectly.
The error I am getting is:
ErrorException in 0401a2c6f4dbf412d9f16ba1e4ced9e54c8bb622.php line 132:
Undefined variable: accounts (View: E:\wamp\www\gal\resources\views\myplace.blade.php)
my view code:
<form action="{{route('account')}}" id="acc" method="post">
<div class="col-lg-3"><label>Estado</label>
#foreach($accounts as $account)
<textarea style="display:none;" name="textfield4" id="textfield4">{{$account->estado}}</textarea></div>
<div class="col-lg-2 es" id="estado"></div>
#endforeach
my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}
Given this code...
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}
$account->estado = $request['textfield4']; should be $account->estado = $request->input('textfield4', 'some_default_value');
User::find(Auth::id())->accounts; does literally nothing as you don't save the result, and $accounts = Account::orderBy('created_at','desc')->get(); is sorting all accounts.
As for how none of your Accounts are being shown with orderBy is probably due to not having any query about what to select or conditions for it to be true. Default values may help, but if you have something funky it may be broken. There isn't much we can do without more code.
Another guess I have about this is return view('myplace',['accounts'=>$accounts]); should be return view()->make('myplace',['accounts'=>$accounts]);
EDIT:
His problem was he was navigating to /myplace which doesn't route to getaccount() it routes to getmyplace(), which didn't have the $accounts variable passed.
Instead of doing:
return view('myplace',['accounts'=>$accounts]);
You can use compact in your Controller:
return view('myplace')->with(compact('accounts'));
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;
}