I learning about laravel framework, I don't know how to access property in my controller.
public function uuid(Request $request)
{
if($request->get('uuid') == null) return abort(404);
$uuid = $request->get('uuid');
$users = DB::table('users')->select('*')->where('uuid', $uuid)->get();
$result = array([
'id' => $users['id'],
'username' => $users['username'],
'uuid' => $users['uuid'],
]);
return view ('dashboard')->with('username', $result['username']);
}
in my dashboard.blade.php
{{$username}}
When i come to dashboard it show error like this
ErrorException (E_NOTICE)
Undefined index: username
Use First() instead of get() you'll get object so access data like.
$users = DB::table('users')->select('*')->where('uuid', $uuid)->first();
$result = array([
'id' => $users->id,
'username' => $users->username,
'uuid' => $users->uuid,
]);
return view ('dashboard')->with('username', $result['username']);
Now sort way to do it.
$user = DB::table('users')->select('*')->where('uuid', $uuid)->first();
$username = '';
if(!empty($user)){
$username = $user->username
}
return view ('dashboard',compact('username'));
$users is a collection of users. So you cannot access a property of a user like $users['id'];
If you want to get one user object from the database, you need to call first() instead of get()
Possible Solution
$user = DB::table('users')->select('*')->where('uuid', $uuid)->first();
You can use firstOrFail();
$users = DB::table('users')->select('*')->where('uuid', $uuid)->firstOrFail();
$result = array([
'id' => $users->id,
'username' => $users->username,
'uuid' => $users->uuid,
]);
return view ('dashboard')->with('username', compact('username'));
A possible short version of your solution may be like following
public function uuid(Request $request)
{
$user = User::select('username')->where('uuid', $request->uuid)->firstOrFail();
return view ('dashboard')->with('username', $user->username);
}
Related
Assume the following relations:
Order:
public function messages(){
return $this->hasManyThrough('App\Models\Message', 'App\Models\Conversation', 'order_id', 'conversation_id', 'id', 'id');
}
Conversation:
public function messages()
{
return $this->hasMany('App\Models\Message')->latest();
}
I tried to create a new meesage from an order instance like:
//create message & assign message to conversation
$message = new Message([
'user_id' => Auth::id(),
'text' => $request->messageText,
]);
$order->messages()->save($message);
Result:
Call to undefined method Illuminate\Database\Eloquent\Relations\HasManyThrough::save()
Is there an easy way to create new instances of message directly through an order instance?
Update
I tried this too:
$order->messages->user_id = Auth::id();
$order->messages->text = $request->messageText;
$order->messages->is_admin = true;
$order->push();
$order = new Order;
$message = new Message([
'user_id' => Auth::id(),
'text' => $request->messageText,
]);
$order->messages()->add($message);
check with this thing will it work
I m try to insert data to db ! but function is working ok, it redirect to route assigned ! no error! but data is not getting added in db! I am not able to figure out ! anyhelp?
public function addfav($webmasterId,$id)
{
// Check Permissions
$data_sections_arr = explode(",", Auth::user()->permissionsGroup->data_sections);
if (Auth::user()->id =='1') {
return Redirect::to(route('NoPermission'))->send();
}
$dlt=DB::table('favads')->insertGetId([
['user_id' => Auth::user()->id, 'topic_id' => $id]
]);
//
// General for all pages
$GeneralWebmasterSections = WebmasterSection::where('status', '=', '1')->orderby('row_no', 'asc')->get();
// General END
//Webmaster Topic Details
$WebmasterSection = WebmasterSection::find($webmasterId);
if (!empty($WebmasterSection)) {
return redirect()->route('fav',['webmasterId'=>$webmasterId]);
} else {
return redirect()->route('NotFound');
}
}
Why are you having two [] ? Remove one of them, I think this is the problem
Change
$dlt=DB::table('favads')->insertGetId([
['user_id' => Auth::user()->id, 'topic_id' => $id]
]);
To
$dlt=DB::table('favads')->insertGetId(
['user_id' => Auth::user()->id, 'topic_id' => $id]
);
Check with try and catch, what error are you receiving
try{
$dlt=DB::table('favads')->insertGetId(
['user_id' => Auth::user()->id, 'topic_id' => $id]
);
dd($dlt);
}catch(\Exception $e){
dd($e->getMessage());
}
You have to use like this
$dlt = new Favad();
$dlt->user_id = Auth::user()->id;
$dlt->topic_id = $id;
$dlt->save();
Be sure that you are using the following code use App\Favad; in your controller.
You should insert data the following way:
$dlt = new Favad;
$dlt->user_id = Auth::user()->id;
$dlt->topic_id = $id;
$dlt->save();
This is my code:
public function getLists(Request $request)
{
$user = $request->user()->id;
$apikey = DB::table('apikey')->where('api_key', '=', $user);
if($apikey){
$mc = new MailChimp($apikey);
$mailchimp_ping = $mc->get('lists',['fields' =>
'lists.id,lists.name']);
return Response::json($mailchimp_ping, 200);
}
else
{
$errorResponse = [
'message' => 'Lists not found!',
'error' => '401'
];
return Response::json( $errorResponse);
}
}
I am trying to get mailchimp list based on logged in user id where i am doing wrong? is my where clause expects something else?
Any help would be highly appreciated!
Use the value() method to execute the query and get the key. For example, if a column with key is called apikey:
$apikey = DB::table('apikey')->where('api_key', $user)->value('apikey');
In my case this error came up when changed this:
Route::view('/report', '/main_pages/orders_parts/report')->name('guzorishi_order_1');
to
Route::get('/report/{order_id}', function ($order_id) {... })->name('guzorishi_order_1');
but forgot Route::view one
rename to Route::get
I am tring to make test for my project, could someone show me example how to write tests or give me some good video course to learn testing.
In my example I am tring to test this part:
public function getProjectsForUser(){
if(Auth::user() -> admin_id == NULL){
$this->id = Auth::user() -> id;
}else{
$this->id = Auth::user() -> admin_id;
}
$projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
$role = User::findOrFail(Auth::user() -> id)->roles;
$users = User::where('admin_id', '=', $this->id)->lists('name','id');
return array('projects' => $projects,'users' => $users,'roles' => $role );
}
It was my model Project.php
Here is my PorojectController:
public function project(Project $projects){
$this -> projects = $projects ->getProjectsForUser();
return view('projects',$this -> projects);
}
Here is my test to check if user logged...
public function testHome()
{
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
}
Now I need to check if I get right values inside array, for $project, $role, $users.
You can use assertViewHas() to assert values that should have been passed to the view. For example:
public function testHome(){
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
$this->assertViewHas('projects', 'expected value for projects');
$this->assertViewHas('users', 'expected value for users');
$this->assertViewHas('roles', 'expected value for roles');
}
By the way: If you pass no second argument the method will just assert that the variable exists but won't compare the value.
To accomplish the same with a bit a different syntax you can use assertViewHasAll() and pass everything as array:
$this->assertViewHasAll([
'projects' => 'expected value for projects',
'users' => 'expected value for users',
'roles' => 'expected value for roles'
]);
Hi help me,
login code
public function store()
{
$credentials = array(
'u_email' => Input::get('email'),
'password' => Input::get('password'));
if (Auth::attempt($credentials) ) {
$user = Auth::user()->toArray();
$userrole = with(new User)->get_user_role($user['u_id']);
$userobj['u_id'] = $user['u_id'];
$userobj['u_shortcode'] = $user['u_shortcode'];
$userobj['utype'] = $user['utype'];
$userobj['u_title'] = $user['u_title'];
$userobj['u_fname'] = $user['u_fname'];
$userobj['u_lname'] = $user['u_lname'];
$userobj['u_email'] = $user['u_email'];
$userobj['u_role'] = $userrole;
$userobj['id'] = Session::getId();
Session::put('admin', $userobj);
$value = Session::get('admin');
return Response::json([
'user' => $userobj ],
202
);
}else{
return Response::json([
'flash2' => 'Authentication failed'],
202
);
}
}
and my second controller is:
public function get_sessionobj()
{
var_dump(Session::all());
$value = Session::get('admin');
print_r($value);
exit();
}
when i am calling second controller after login then session data not printed. in login controller Session::get('admin') function returning data. and i am using file driver for session storage. I have seen my session file there was some data like this:
a:5:{s:6:"_token";s:40:"XrUgs7QLPlXvjvyzFaTdmDpqGL0aSZRzkJS0il9f";s:38:"login_82e5d2c56bdd0811318f0cf078b78bfc";s:1:"1";s:5:"admin";a:9:{s:4:"u_id";s:1:"1";s:11:"u_shortcode";s:5:"u1001";s:5:"utype";s:1:"1";s:7:"u_title";s:3:"Mr.";s:7:"u_fname";s:6:"Aristo";s:7:"u_lname";s:5:"Singh";s:7:"u_email";s:24:"chandan.singh#jetwave.in";s:6:"u_role";a:3:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";}s:2:"id";s:40:"cd074f7f61fcc88b3d92c482e57e8a12dc888958";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1410525787;s:1:"c";i:1410525787;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}
Call a function get_sessionobj() in store function
Example:
public function store(){
$this->get_sessionobj();
}