Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Controller two functions
class AdminController extends Controller
{
public function login(Request $request){
if($request->is Method('post')){
$data =$request->input();
if(Auth::attempt(['email'=>$data,['email'],'password'=>$data['password'],'admin'=>'1'])){
//echo "Success"; exit;
return redirect('/admin/dashboard');
}else{
echo "Failed"; exit;
}
}
return view ('admin.admin_login');
}
and This is the error I get SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from users where email in (z6vCfv4iWVDd1mwdGv1gFc0DvP6E0d0ur1KtO1Em, johndoe#gmail.com, admin#1234) and 0 in (email) and admin = 1 limit 1)
The problem is that you put the whole $data array in the attempt.
Replace if(Auth::attempt(['email'=>$data,'email'],'password'=>$data['password'],'admin'=>'1'])) by
if(Auth::attempt(['email'=>$data['email'],'password'=>$data['password'],'admin'=>'1']))
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
when trying to view a specific blog article i get the error Trying to get property 'title' of non-object
here is my controller controller code
public function theblog(Request $req, $slug){
$blog = blog::find($slug);
DB::table("blogs")->where('slug', "$slug")->increment('views');
return view('myblog',compact('blog'));
}
public function theblog(Request $req, $slug){
$blog = blog::where('slug', $slug)->firstOrFail();
DB::table("blogs")->where('slug', $slug)->increment('views');
return view('myblog',compact('blog'));
}
Some tips.
your primary key is unlikely to be 'slug' so you need to use a where
don't quote php parameters
your classes should start with an uppercase, ie Blog not blog
you should probably only increment views once per user
public function theblog(Request $req, $slug){ $blog = blog::find($slug);
dd($slug);
DB::table("blogs")->where('slug', "$slug")->increment('views'); return view('myblog',compact('blog')); }
Check if there is value for $blog...if it is null(that is the problem) then you should check you DB?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to get the data of a Model and also the data of the foreign key in the Model
For Example:
A Post Model has a user_id as a foreign key. How can I write an Eloquent query so that I can get the data of a Post and in place of the user_id, I get the data of the user_id ?
Use with method when querying your post to include the user.
$post = Post::with('user')->first();
// or
$posts = Post::with('user')->get();
At Post model
public function user(){
return $this->belongsTo(User::class,'user_id');
}
Now at your PostController
Guess you have a method which is use for showing post
public function show($id){
$post = Post::query()->findOrFail($id);
}
Now if you want to access which user posted then there has two ways.
Way 1 :
public function show($id){
$post = Post::query()->with('user')->findOrFail($id);
}
Way 2 :
public function show($id){
$post = Post::query()->findOrFail($id);
echo $post->user->firstName;
}
Note :
If you just write $post->user then it will show/return User table according to user_id column.
If you want to access these user row column then . Simply $post->user->column_name;
Enjoy Coding.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
$data['customers'] = DB::table('master_customer')->where('ORG_ID', $user_org)->get();//customer table
$document_cat = DB::table('document_category')->get();//category table
foreach ($data['customers'] as $key => $value) {
foreach ($document_cat as $key1 => $value1) {
$docs = DB::table('master_documents')//docuements saved in this table
->where('cust_id', $value->id)
->get();
$document_cat[$key1]->doc = $docs;
}
$data['customers'][$key]->documents = $document_cat;
}
print_r($data);
die;
Why you dont update eloquent relationships in models? If you do so it could be much easier.
MasterCustemer Model
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument','cust_id');
}
MasterDocument Model
public function masterCustomer()
{
return $this->belongsTo('App\correctfoldertoothermodel\MasterCustomer','cust_id');
}
public function documentCategory()
{
return $this->belongsTo('App\correctfoldertoothermodel\DocumentCategory');
}
DocumentCategory Model
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument');
}
Of course you'll have to provide the correct table names in migration for auto implementation of fk or set tables in relationships too.
When it's done you could retrieve data like it:
$masterCustomers->with('masterDocuments.documentCategory')->get();
$masterCustomers->masterDocuments = $masterCustomers->masterDocuments->groupBy(function($item){
return $item->documentCategory->name;
});
The result will be a collection of customers with and property containing an another collection of categories with its documents.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My controller:
function index:
public function index(Request $request)
{
//there is error Undefined variable: jobs
return view('jobs.index')->with('jobs', $jobs);
}
Why
You need to add a $jobs variable in the method.
public function index(Request $request)
{
$jobs = // Some code here;
return view('jobs.index')
->with('jobs', $jobs);
}
the best way to achieve this is by doing something like this:
public function index(Request $request)
{
$data['jobs'] = DB::table('your-table')->get();
return view('jobs.index',$data);
}
with the above code snippet, you could be able to use the variable job in your blade like this:
#foreach($jobs as $j)
{{ $j->fieldName }}
#endforeach
the above is use when you use get() in your database query. Hope it helps!!!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to store current user's user_id in database in Laravel:
public function AddToCart(Request $request)
{
Cart::create([
'user_id'=>Auth::user()->id,
'product_id'=>$request->AddToCart
]);
return redirect('#');
}
Here user_id is an attribute and $userId is the user's id which I want to store in the database. But I am getting the following error:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
it's very simple with laravel. you can do it like this; also we need to check if the user it is actually authenticate d
if (Auth::user()) {
Cart::create([
'user_id' => Auth::user()->id,
'product_id' => $request->AddToCart
]);
}
don't forget to use Illuminate\Support\Facades\Auth; at the top of your class.
For the error that you are getting, one possible solution could be to create a new migration: php artisan make:migration modify_user_id_in_carts_tabel --table=carts and add the following: note that you may need to run composer require doctrine/dbal if you dont have it.
$table->unsignedInteger('user_id')->nullable()->change();