i tried assigning a value to a variable in an if condition
if ($hotel = Hotel::whereCode($this->hotelCode)->first() && $room = Room::whereRoomCode($value)->first()) {
if ($hotel->room_id == $room->id) {
return true;
}
}
I get this error
Attempt to read property "room_id" on bool
meanwhile $hotel variable is not a boolean
Your code can be processed by the compiler as
if ( $hotel = (
Hotel::whereCode($this->hotelCode)->first() && $room = Room::whereRoomCode($value)->first()
)
) {
//...
}
in that case you can see $hotel is a bool result from the && operator.
You should add parenthesis to counter the issue
if (
($hotel = Hotel::whereCode($this->hotelCode)->first()) &&
($room = Room::whereRoomCode($value)->first())
) {
if ($hotel->room_id == $room->id) {
return true;
}
}
or in a more clear way
$hotel = Hotel::whereCode($this->hotelCode)->first();
$room = Room::whereRoomCode($value)->first();
return $hotel && $room && ($hotel->room_id == $room->id);
Or using relation and using way less performance (using only a count query)
public function hasRoomByRoomCode($value)
{
return $this->room()->whereRoomCode($value)->count();
}
//this is the relation function if you did not set it yet inside Hotel::class
public function room()
{
return $this->belongsTo(Room::class);
}
I have three variables and I only want to add certain where clause if those variables are not null or blank.
$brand = $request->brand == 0 ? "" : $request->brand ;
$category = $request->category == 0 ? "" : $request->category ;
$subcategory = $request->subcategory == 0 ? "" : $request->subcategory;
$items = Item::select('items.*','brands.brand_name','category.category_name','subcategory.subcategory_name')
->leftJoin('brands','items.brand_id','=','brands.id')
->leftJoin('category','items.category_id','=','category.id')
->leftJoin('subcategory','items.subcategory_id','=','subcategory.id');
if($brand != ""){
$items->where('items.brand_id',$brand);
}
if($category != ""){
$items->where('items.category_id',$category);
}
if($subcategory != ""){
$items->where('items.subcategory_id',$subcategory);
}
$items->get();
Now I am getting below error
'Illuminate\Database\Eloquent\Builder could not be converted to
string'
$items is an Eloquent Builder instance so take output in other variable:
$items = Item::select('items.*','brands.brand_name','category.category_name','subcategory.subcategory_name')
->leftJoin('brands','items.brand_id','=','brands.id')
->leftJoin('category','items.category_id','=','category.id')
->leftJoin('subcategory','items.subcategory_id','=','subcategory.id');
if($brand != ""){
$items->where('items.brand_id',$brand);
}
if($category != ""){
$items->where('items.category_id',$category);
}
if($subcategory != ""){
$items->where('items.subcategory_id',$subcategory);
}
$data = $items->get();
Try to use this part as like this. assign your $items variable every condition.
$items = Item::select('items.*','brands.brand_name','category.category_name','subcategory.subcategory_name')
->leftJoin('brands','items.brand_id','=','brands.id')
->leftJoin('category','items.category_id','=','category.id')
->leftJoin('subcategory','items.subcategory_id','=','subcategory.id');
if($brand != ""){
$items = $items->where('items.brand_id',$brand);
}
if($category != ""){
$items = $items->where('items.category_id',$category);
}
if($subcategory != ""){
$items = $items->where('items.subcategory_id',$subcategory);
}
$items = $items->get();
$Items = DB::table('Item')
->join('brands','items.brand_id','=','brands.id')
->Join('category','items.category_id','=','category.id')
->Join('subcategory','items.subcategory_id','=','subcategory.id');
->select('items.*','brands.brand_name','category.category_name',
'subcategory.subcategory_name')
->get();
dd($Items);
I have a four different tables which have one of the fields as loc1, loc2, loc3, loc4. I am fetching data from database using a procedure from these tables which uses one parameter. I want that every column data when fetched must be saved in a different array i.e loc1 field data in $loc1 array, loc2 field data in $loc2 array and same for other fields. My code is as.
$sql = "call report_card_seven_five_three_one_two('" .id. "')";
$loc1 = array();
$loc2 = array();
$loc3 = array();
$loc4 = array();
if (mysqli_multi_query($connection, $sql)) {
while ($row =mysqli_fetch_row($result))
{
while($row =mysql_fetch_array($result))
{
}
}
mysqli_free_result($result);
while(mysqli_next_result($connection) && mysqli_more_results($connection));
}
Can any one help me on this part.
May be like:
while($row = mysql_fetch_array($result))
{
if(!empty($row['loc1']) || $row['loc1'] != NULL || $row['loc1'] != "")
{
array_push($loc1, $row['loc1']);
}
if(!empty($row['loc2']) || $row['loc2'] != NULL || $row['loc2'] != "")
{
array_push($loc2, $row['loc2']);
}
if(!empty($row['loc3']) || $row['loc3'] != NULL || $row['loc3'] != "")
{
array_push($loc3, $row['loc3']);
}
if(!empty($row['loc4']) || $row['loc4'] != NULL || $row['loc4'] != "")
{
array_push($loc4,$row['loc4']);
}
}
Is there any option to export only two columns in grocery-crud. I don't want to export all columns.
The correct way to do it is to use the getState() method
$state = $crud->getState();
if ($state == 'export' || $state == 'print') {
$crud->columns('first_name','last_name','email');
} else {
$crud->columns('first_name','last_name','email','phone','city','country');
}
Below you can find a full code example :
function example1()
{
$crud = new grocery_CRUD();
$crud->set_table('customers');
$crud->set_subject('Customer');
$crud->required_fields('first_name', 'last_name','email');
$state = $crud->getState();
if ($state == 'export' || $state == 'print') {
$crud->columns('first_name','last_name','email');
} else {
$crud->columns('first_name','last_name','email','phone','city','country');
}
$output = $crud->render();
$this->_example_output($output);
}
I have a setup where it favourites and retweets a specific tweet. For some reason, the code works for the favourites however retweets do not. Can anyone see the issue?
$method = 'statuses/retweet/'.$url[3];
$amt = "26";
$sub = rand(1,3);
$amt1 = $amt-$sub;
if($_POST['favorite'] == "true" || $_POST['favorite'] == "1"){
for ($x1=1; $x1<=$amt1; $x1++)
{
$content = $connection[$x1]->post('favorites/create', array('id' => $url[3]));
}
}
if($_POST['retweet'] == "true" || $_POST['retweet'] == "1"){
for ($x2=1; $x2<=$amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
}
have you tried declaring
$amt = 26
instead of
$amt="26"
EDIT:
for ($x2=1; $x2<=amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
you have used amt instead of $amt in the loop condition